-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkatex-header.html
74 lines (70 loc) · 3.44 KB
/
katex-header.html
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
<!DOCTYPE html>
<!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-MlJdn/WNKDGXveldHDdyRP1R4CTHr3FeuDNfhsLPYrq2t0UBkUdK2jyTnXPEK1NQ" crossorigin="anonymous">
<!-- The loading of KaTeX is deferred to speed up page rendering -->
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-VQ8d8WVFw0yHhCk5E8I86oOhv48xLpnDZx5T9GogA/Y84DcCKWXDmSDfn13bzFZY" crossorigin="anonymous"></script>
<!-- To automatically render math in text elements, include the auto-render extension: -->
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
renderMathInElement(document.body, {
delimiters: [
{left: "$$", right: "$$", display: true},
{left: "\\(", right: "\\)", display: false},
{left: "$", right: "$", display: false},
{left: "\\[", right: "\\]", display: true}
]
});
});
</script>
<script>
"use strict";
document.addEventListener("DOMContentLoaded", function () {
// display blocks
document.querySelectorAll('pre.language-math > code').forEach((el) => {
let p = document.createElement("p");
katex.render(el.innerText, p, {displayMode: true, throwOnError: false});
el.parentNode.parentNode.replaceChild(p, el.parentNode);
});
// inline blocks
document.querySelectorAll(':not(pre) > code').forEach((el) => {
let text = el.innerText;
if (!text.startsWith('$') || !text.endsWith('$')) {
return;
}
let span = document.createElement("span");
katex.render(text.substr(1, text.length - 2), span, {displayMode: false, throwOnError: false});
el.parentNode.replaceChild(span, el);
});
// comment in code
document.querySelectorAll('pre span.comment').forEach((el) => {
let html = el.innerText;
let children = [];
let offset = 0;
[...html.matchAll(/(?:[^\$]|^)(\$(?:\\.|[^\$])+\$)(?!\$)/g)].forEach((match) => {
let textBefore = html.substring(offset, match.index + 1);
children.push(document.createTextNode(textBefore));
let math = match[1].substring(1, match[1].length - 1);
let span = document.createElement("span");
katex.render(math, span, {displayMode: false, throwOnError: false});
children.push(span);
offset = match.index + match[0].length;
});
if (offset == 0) {
return;
}
let textAfter = html.substring(offset);
if (textAfter.length > 0) {
children.push(document.createTextNode(textAfter));
}
while (el.firstChild) { el.firstChild.remove(); }
children.forEach((child) => el.appendChild(child));
});
});
</script>
</head>
...
</html>