-
Notifications
You must be signed in to change notification settings - Fork 1
/
highlight-tex.ts
42 lines (38 loc) · 953 Bytes
/
highlight-tex.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
import $ from "jquery";
import katex from "katex";
const beginsWithTex = /^\\\\\(/;
const endsWithTex = /\\\)$/;
function testTex(text: string): boolean {
return beginsWithTex.test(text) && endsWithTex.test(text);
}
export function highlightTex() {
$(`
.md code,
.md code.language-tex
`).each(function() {
const el = $(this);
const text = el.text()
.trim()
.replace(/[&]amp[;]/g, '&')
.replace(/[/][/][/][/]/g, '//');
if (
!el.hasClass("language-tex")
&& !testTex(text)
) {
return;
}
if (text.length > 0) {
try {
const output = katex.renderToString(text, {
displayMode: el.hasClass("language-tex"),
output: 'mathml',
});
$(output).insertAfter(this);
$(this).remove();
// $(pre).find('span.mspace.newline').remove();
} catch (e) {
console.warn('unable to render', text, e);
}
}
});
}