All files index.js

100% Statements 35/35
100% Branches 50/50
100% Functions 4/4
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 7915x                             2192x   274x 6x 12x 12x       268x 16x 1x       15x 2x   13x 2x           263x             263x 11x 252x         263x   4x   4x   4x         259x     263x 3x         263x     15x  
import parser from "./parser";
 
export function ascii2mathml(
  asciimath,
  {
    annotate = false,
    bare = false,
    display = "inline",
    standalone = false,
    dir = "ltr",
 
    decimalMark = ".",
    colSep = decimalMark === "," ? ";" : ",",
    rowSep = colSep === ";" ? ";;" : ";"
  } = {}
) {
  // Curry
  if (typeof asciimath === "object") {
    return (str, options2) => {
      const opts = { ...asciimath, ...options2 };
      return ascii2mathml(str, opts);
    };
  }
 
  if (bare) {
    if (standalone) {
      throw new Error(
        "Can't output a valid HTML without a root <math> element"
      );
    }
    if (display && display.toLowerCase() !== "inline") {
      throw new Error("Can't display block without root element.");
    }
    if (dir && dir.toLowerCase() !== "ltr") {
      throw new Error(
        "Can't have right-to-left direction without root element."
      );
    }
  }
 
  const parse = parser({
    decimalMark,
    colSep,
    rowSep
  });
  let out;
 
  const math = bare
    ? expr => expr
    : expr =>
        `<math${display !== "inline" ? ` display="${display}"` : ""}${
          dir !== "ltr" ? ` dir="${dir}"` : ""
        }>${expr}</math>`;
 
  if (annotate) {
    // Make sure the all presentational part is the first element
    const parsed = parse(asciimath.trim(), "");
    const mathml =
      parsed === parser.getlastel(parsed) ? parsed : `<mrow>${parsed}</mrow>`;
 
    out = math(
      `<semantics>${mathml}<annotation encoding="application/AsciiMath">${asciimath}</annotation>` +
        `</semantics>`
    );
  } else {
    out = math(parse(asciimath.trim(), ""));
  }
 
  if (standalone) {
    out =
      `<!DOCTYPE html><html><head><title>${asciimath}</title></head>` +
      `<body>${out}</body></html>`;
  }
 
  return out;
}
 
export default ascii2mathml;