Skip to content

Commit 5a18f4f

Browse files
committed
Switch online book math rendering from MathJax to KaTeX.
MathJax 2 preset config ignored $...$ delimiters; KaTeX auto-render matches IDE Preview $/$$ syntax on GitHub Pages.
1 parent e7f3ab7 commit 5a18f4f

5 files changed

Lines changed: 42 additions & 43 deletions

File tree

book.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ build-dir = "mdbook-out"
1010
create-missing = false
1111

1212
[output.html]
13-
mathjax-support = true
13+
mathjax-support = false
1414
default-theme = "light"
1515
preferred-dark-theme = "navy"
1616
git-repository-url = "https://github.com/fooSynaptic/deepseek-tech-notes"
1717
site-url = "/deepseek-tech-notes/"
1818
additional-css = ["theme/mdbook.css"]
19-
additional-js = ["theme/mathjax-typeset.js"]
19+
additional-js = ["theme/katex-render.js"]
2020

2121
[output.html.search]
2222
enable = true

scripts/fix_mdbook_paths.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,16 @@
2020
SKIP_PREFIXES = ("http://", "https://", "//", "mailto:", "javascript:", "data:")
2121
ATTR_RE = re.compile(r'(href|src)="([^"]+)"')
2222
PATH_TO_ROOT_RE = re.compile(r'var path_to_root = "[^"]*";')
23-
MATHJAX_RE = re.compile(r"<!-- MathJax -->\s*<script async src=\"[^\"]+\"></script>")
24-
MATHJAX_BLOCK = """<!-- MathJax: enable $...$ (IDE / VS Code Preview delimiters) -->
25-
<script type="text/x-mathjax-config">
26-
MathJax.Hub.Config({
27-
tex2jax: {
28-
inlineMath: [['$','$'], ['\\\\(','\\\\)']],
29-
displayMath: [['$$','$$'], ['\\\\[','\\\\]']],
30-
processEscapes: true,
31-
processEnvironments: true,
32-
skipTags: ['script','noscript','style','textarea','pre','code']
33-
}
34-
});
35-
</script>
36-
<script async src="https://cdn.jsdelivr.net/npm/mathjax@2.7.9/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>"""
23+
MATHJAX_RE = re.compile(
24+
r"<!-- MathJax[^>]* -->[\s\S]*?"
25+
r"<script async src=\"[^\"]*(?:MathJax|mathjax)[^\"]*\"></script>\s*",
26+
re.IGNORECASE,
27+
)
28+
KATEX_HEAD = """<!-- KaTeX: $...$ / $$...$$ (IDE / VS Code Preview delimiters) -->
29+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css">
30+
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js"></script>
31+
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js"></script>
32+
"""
3733

3834

3935
def read_site_url() -> str:
@@ -110,7 +106,11 @@ def repl(m: re.Match[str]) -> str:
110106

111107

112108
def patch_mathjax(text: str) -> str:
113-
return MATHJAX_RE.sub(MATHJAX_BLOCK, text)
109+
if "<!-- KaTeX:" in text:
110+
return text
111+
if MATHJAX_RE.search(text):
112+
return MATHJAX_RE.sub(KATEX_HEAD, text, count=1)
113+
return text.replace("</head>", KATEX_HEAD + " </head>", 1)
114114

115115

116116
def write_redirect_index() -> None:

theme/katex-render.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// KaTeX auto-render: $...$ / $$...$$ (matches IDE Preview Markdown math).
2+
document.addEventListener("DOMContentLoaded", function () {
3+
if (typeof renderMathInElement !== "function") {
4+
return;
5+
}
6+
var root = document.querySelector("#content main");
7+
if (!root) {
8+
return;
9+
}
10+
renderMathInElement(root, {
11+
delimiters: [
12+
{ left: "$$", right: "$$", display: true },
13+
{ left: "$", right: "$", display: false },
14+
{ left: "\\[", right: "\\]", display: true },
15+
{ left: "\\(", right: "\\)", display: false },
16+
],
17+
ignoredTags: [
18+
"script", "noscript", "style", "textarea", "pre", "code",
19+
],
20+
throwOnError: false,
21+
});
22+
});

theme/mathjax-typeset.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

theme/mdbook.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
margin: 0.35em 0;
1111
}
1212

13-
/* Inline math spacing */
14-
.mjx-chtml {
15-
font-size: 109% !important;
13+
/* Inline math spacing (KaTeX) */
14+
.katex {
15+
font-size: 1.09em;
1616
}

0 commit comments

Comments
 (0)