MCPcopy
hub / github.com/KaTeX/KaTeX / buildHTML

Function buildHTML

src/buildHTML.ts:320–411  ·  view source on GitHub ↗
(tree: AnyParseNode[], options: Options)

Source from the content-addressed store, hash-verified

318 * nodes.
319 */
320export default function buildHTML(tree: AnyParseNode[], options: Options): DomSpan {
321 // Strip off outer tag wrapper for processing below.
322 let tag = null;
323 if (tree.length === 1 && tree[0].type === "tag") {
324 tag = tree[0].tag;
325 tree = tree[0].body;
326 }
327
328 // Build the expression contained in the tree
329 const expression = buildExpression(tree, options, "root");
330
331 let eqnNum;
332 if (expression.length === 2 && expression[1].hasClass("tag")) {
333 // An environment with automatic equation numbers, e.g. {gather}.
334 eqnNum = expression.pop();
335 }
336
337 const children = [];
338
339 // Create one base node for each chunk between potential line breaks.
340 // The TeXBook [p.173] says "A formula will be broken only after a
341 // relation symbol like $=$ or $<$ or $\rightarrow$, or after a binary
342 // operation symbol like $+$ or $-$ or $\times$, where the relation or
343 // binary operation is on the ``outer level'' of the formula (i.e., not
344 // enclosed in {...} and not part of an \over construction)."
345
346 let parts = [];
347 for (let i = 0; i < expression.length; i++) {
348 parts.push(expression[i]);
349 if (expression[i].hasClass("mbin") ||
350 expression[i].hasClass("mrel") ||
351 expression[i].hasClass("allowbreak")) {
352 // Put any post-operator glue on same line as operator.
353 // Watch for \nobreak along the way, and stop at \newline.
354 let nobreak = false;
355 while (i < expression.length - 1 &&
356 expression[i + 1].hasClass("mspace") &&
357 !expression[i + 1].hasClass("newline")) {
358 i++;
359 parts.push(expression[i]);
360 if (expression[i].hasClass("nobreak")) {
361 nobreak = true;
362 }
363 }
364 // Don't allow break if \nobreak among the post-operator glue.
365 if (!nobreak) {
366 children.push(buildHTMLUnbreakable(parts, options));
367 parts = [];
368 }
369 } else if (expression[i].hasClass("newline")) {
370 // Write the line except the newline
371 parts.pop();
372 if (parts.length > 0) {
373 children.push(buildHTMLUnbreakable(parts, options));
374 parts = [];
375 }
376 // Put the newline at the top level
377 children.push(expression[i]);

Callers 2

buildTreeFunction · 0.85
buildHTMLTreeFunction · 0.85

Calls 6

makeSpanFunction · 0.90
makeEmFunction · 0.90
buildHTMLUnbreakableFunction · 0.85
buildExpressionFunction · 0.70
hasClassMethod · 0.65
setAttributeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…