deno.land / x / lume@v2.1.4 / deps / katex-auto-render / auto-render.ts

auto-render.ts
نووسراو ببینە
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { katex, KatexOptions } from "../../deps/katex.ts";import splitAtDelimiters from "./splitAtDelimiters.js";
/* Note: optionsCopy is mutated by this method. If it is ever exposed in the * API, we should copy it before mutating. */const renderMathInText = function ( document: Document, text: string, optionsCopy: KatexOptions,) { const data = splitAtDelimiters(text, optionsCopy.delimiters); if (data.length === 1 && data[0].type === 'text') { // There is no formula in the text. // Let's return null which means there is no need to replace // the current text node with a new one. return null; }
const fragment = document.createDocumentFragment();
for (let i = 0; i < data.length; i++) { if (data[i].type === "text") { fragment.appendChild(document.createTextNode(data[i].data)); } else { const span = document.createElement("span"); let math = data[i].data; // Override any display mode defined in the settings with that // defined by the text itself optionsCopy.displayMode = data[i].display; if (optionsCopy.preProcess) { math = optionsCopy.preProcess(math); } const rendered = katex.renderToString( math, optionsCopy, ); const div = document.createElement("div"); div.innerHTML = rendered.trim();
span.appendChild(div.firstChild!); fragment.appendChild(span); } }
return fragment;};
function renderElem(elem: Element, optionsCopy: KatexOptions) { for (let i = 0; i < elem.childNodes.length; i++) { const childNode = elem.childNodes[i]; if (childNode.nodeType === 3) { // Text node let textContentConcat = childNode.textContent || ""; let sibling = childNode.nextSibling; let nSiblings = 0; while (sibling && (sibling.nodeType === 3)) { textContentConcat += sibling.textContent; sibling = sibling.nextSibling; nSiblings++; } const frag = renderMathInText( elem.ownerDocument!, textContentConcat, optionsCopy, ); if (frag) { // Remove extra text nodes for (let j = 0; j < nSiblings; j++) { childNode.nextSibling?.remove(); } i += frag.childNodes.length - 1; elem.replaceChild(frag, childNode); } else { // If the concatenated text does not contain math // the siblings will not either i += nSiblings; } } else if (childNode.nodeType === 1) { // Element node const el = childNode as Element; const className = " " + el.className + " "; const shouldRender = optionsCopy.ignoredTags!.indexOf( el.nodeName.toLowerCase(), ) === -1 && optionsCopy.ignoredClasses!.every( (x) => className.indexOf(" " + x + " ") === -1, );
if (shouldRender) { renderElem(el, optionsCopy); } } // Otherwise, it's something else, and ignore it. }}
export function renderMathInElement( elem: Element | undefined, options: KatexOptions,) { if (!elem) { throw new Error("No element provided to render"); }
renderElem(elem, Object.assign({}, options));}
lume

Version Info

Tagged at
7 months ago