-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab-katex.js
94 lines (80 loc) · 2.88 KB
/
gitlab-katex.js
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
// This script assumes that katex and katex/contrib/auto-render have already been included,
// specifically that the 'katex.render()' and 'renderMathInElement()' functions are in scope.
// This function grovels 'elem' looking for
// <pre class="language-math"><code class="language-math"> ... </></>
// which results from conversion of GitLab markup fenced math block, and replaces
// it with result of katex.render() call.
const renderFencedMathBlock = function(elem)
{
const children = elem.childNodes;
const nchildren = children.length;
for (let i = 0; i < nchildren; ++i)
{
const child = elem.childNodes[i];
if (child.nodeType != 1)
continue;
switch (child.nodeName.toLowerCase())
{
case "script":
case "noscript":
case "style":
case "textarea":
case "code":
// Skip
continue;
case "pre":
break;
default:
// recurse into
renderFencedMathBlock(child);
continue;
}
if (child.getAttribute("class").trim() == "language-math" &&
child.childNodes.length == 1)
{
const grandchild = child.childNodes[0];
if (grandchild.nodeName.toLowerCase() == "code" &&
grandchild.getAttribute("class").trim() == "language-math" &&
grandchild.childNodes.length == 1 &&
grandchild.childNodes[0].nodeType == 3)
{
// Replace text content with KaTeX render result.
const fragment = document.createDocumentFragment();
const math = grandchild.childNodes[0].textContent;
try
{
const span = document.createElement("span");
katex.render(math, span, {displayMode: true});
fragment.appendChild(span);
elem.replaceChild(fragment, child)
}
catch (e)
{
if (!(e instanceof katex.ParseError)) {
throw e;
}
console.error("GitLab KaTeX: Failed to parse `" + math + "`:", e);
fragment.appendChild(document.createTextNode(math));
}
}
}
}
};
const renderGitlabKatex = function() {
options = {
delimiters: [
{left: "$$", right: "$$", display: true},
{left: "\\[", right: "\\]", display: true},
{left: "$`", right: "$`", display: false},
{left: "\\(`", right: "`\\)", display: false},
{left: "\\(", right: "\\)", display: false},
// LaTeX uses this, but it ruins the display of normal `$` in text:
// {left: "$", right: "$", display: false},
],
ignoredTags: [
"script", "noscript", "style", "textarea", "pre", "code",
],
};
renderMathInElement(document.body, options);
renderFencedMathBlock(document.body, options);
}