Skip to content

Commit a126471

Browse files
committed
fix(theme): enable KaTeX auto-render and add robust math shortcodes; partially convert /blog/katex-bug/ examples
1 parent 56c16ba commit a126471

File tree

5 files changed

+71
-32
lines changed

5 files changed

+71
-32
lines changed

content/blog/katex_bug.md

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,41 @@ tags = ["demo"]
1111
> Display exceptions, such as inconsistent trailing commas and local restrictions, being pushed to one line, or not being displayed
1212
1313
```markdown
14-
$$\begin{cases} f(x) \equiv V_1(x)^2 \pmod{U_1(x)} \\ f(x) \equiv V_2(x)^2 \pmod{U_2(x)} \\ f(x) \equiv V_3(x)^2 \pmod{U_3(x)} \end{cases}$$
14+
{% katex_block() %}
15+
\\begin{cases}
16+
f(x) \\equiv V_1(x)^2 \\pmod{U_1(x)} \\\\
17+
f(x) \\equiv V_2(x)^2 \\pmod{U_2(x)} \\\\
18+
f(x) \\equiv V_3(x)^2 \\pmod{U_3(x)}
19+
\\end{cases}
20+
{% end %}
1521
```
1622

1723
$$\begin{cases} f(x) \equiv V_1(x)^2 \pmod{U_1(x)} \\ f(x) \equiv V_2(x)^2 \pmod{U_2(x)} \\ f(x) \equiv V_3(x)^2 \pmod{U_3(x)} \end{cases}$$
1824

1925
---
2026

2127
```markdown
22-
$(\text{order\_candidate} / p_i) \cdot G$
28+
{% katex() %}(\\text{order\\_candidate} / p_i) \\cdot G{% end %}
2329
```
2430

2531
$(\text{order\_candidate} / p_i) \cdot G$
2632

2733
---
2834

2935
```markdown
30-
$$\text{order\_candidate} \leftarrow \frac{\text{order\_candidate}}{p_i}$$
36+
{% katex_block() %}
37+
\\text{order\\_candidate} \\leftarrow \\frac{\\text{order\\_candidate}}{p_i}
38+
{% end %}
3139
```
3240

3341
$$\text{order\_candidate} \leftarrow \frac{\text{order\_candidate}}{p_i}$$
3442

3543
---
3644

3745
```markdown
38-
$$
39-
\text{flag\_chocolate}= (a^m+b^m)\bmod p ,
40-
$$
46+
{% katex_block() %}
47+
\\text{flag\\_chocolate}= (a^m+b^m)\\bmod p ,
48+
{% end %}
4149
```
4250

4351
$$
@@ -47,9 +55,9 @@ $$
4755
---
4856

4957
```markdown
50-
$$
51-
q = p_{\text{crypto}}-1\; / \; 2 = 85\,414\,812\,699\,185\,126\,250\,990\,381\,881\,994\,204\,791 .
52-
$$
58+
{% katex_block() %}
59+
q = p_{\\text{crypto}}-1\\; / \\; 2 = 85\\,414\\,812\\,699\\,185\\,126\\,250\\,990\\,381\\,881\\,994\\,204\\,791 .
60+
{% end %}
5361
```
5462

5563
$$
@@ -59,16 +67,12 @@ $$
5967
---
6068

6169
```markdown
62-
$$
63-
B=
64-
\begin{pmatrix}
65-
2 & & & & & d_{1,6} \\
66-
& 2 & & & & d_{1,7} \\
67-
& & \ddots & & & \vdots\\
68-
& & & 2 & & d_{17,11}\\
69-
& & & & 1 & -M
70-
\end{pmatrix},
71-
$$
70+
{% katex_block() %}
71+
B=\\begin{pmatrix}
72+
2 & & & & & d_{1,6} \\\\
73+
& 2 & & & & d_{1,7} \\\\
74+
& & \\ddots & & & \\vdots\\\\n+ & & & 2 & & d_{17,11}\\\\n+ & & & & 1 & -M\\end{pmatrix},
75+
{% end %}
7276
```
7377

7478
$$
@@ -85,9 +89,9 @@ $$
8589
---
8690

8791
```markdown
88-
$$
89-
S=S_{0}+\sum_{i,k}b_{i,k}\,d_{i,k} - K\,M,
90-
$$
92+
{% katex_block() %}
93+
S=S_{0}+\\sum_{i,k}b_{i,k}\\,d_{i,k} - K\\,M,
94+
{% end %}
9195
```
9296

9397
$$
@@ -97,9 +101,9 @@ $$
97101
---
98102

99103
```markdown
100-
$$
101-
\text{idek\{ \ldots \}} .
102-
$$
104+
{% katex_block() %}
105+
\\text{idek\\{ \\ldots \\}} .
106+
{% end %}
103107
```
104108

105109
$$
@@ -109,17 +113,15 @@ $$
109113
---
110114

111115
```markdown
112-
$$
113-
m_{\text{min}} = \underbrace{0x20\cdots20}_{20\text{ 个空格}}
114-
\qquad
115-
m_{\text{max}} = \underbrace{0x7E\cdots7E}_{20\text{ 个波浪号}}
116-
$$
116+
{% katex_block() %}
117+
m_{\\text{min}} = \\underbrace{0x20\\cdots20}_{20\\text{ 个空格}} \\\\
118+
\\qquad \\\\
119+
m_{\\text{max}} = \\underbrace{0x7E\\cdots7E}_{20\\text{ 个波浪号}}
120+
{% end %}
117121
```
118122

119123
$$
120124
m_{\text{min}} = \underbrace{0x20\cdots20}_{20\text{ 个空格}}
121125
\qquad
122126
m_{\text{max}} = \underbrace{0x7E\cdots7E}_{20\text{ 个波浪号}}
123127
$$
124-
125-

static/js/katex_autorender.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/shortcodes/katex.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{# Inline/block KaTeX rendering via client-side katex.render #}
2+
{%- set display = display | default(value=false) -%}
3+
<span class="math-katex" data-display="{{ display }}">{{ body | safe }}</span>
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{# Block KaTeX rendering via client-side katex.render #}
2+
<div class="math-katex" data-display="true">{{ body | safe }}</div>
3+

templates/tabi/extend_body.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{# Local KaTeX auto-render for standard delimiters and robust shortcode rendering #}
2+
{%- if macros_settings::evaluate_setting_priority(setting="katex", page=page | default(value=""), section=section | default(value=""), default_global_value=false) == "true" -%}
3+
<script defer src="{{ get_url(path='js/katex_autorender.min.js', trailing_slash=false) | safe }}"></script>
4+
<script>
5+
document.addEventListener("DOMContentLoaded", function() {
6+
try {
7+
if (typeof renderMathInElement === 'function') {
8+
renderMathInElement(document.body, {
9+
delimiters: [
10+
{left: "$$", right: "$$", display: true},
11+
{left: "\\(", right: "\\)", display: false},
12+
{left: "\\[", right: "\\]", display: true}
13+
],
14+
ignoredTags: ["script","noscript","style","textarea","pre","code","option"],
15+
});
16+
}
17+
} catch (e) { /* no-op */ }
18+
19+
// Fallback: render shortcodes that avoid Markdown interference
20+
if (window.katex) {
21+
document.querySelectorAll('.math-katex').forEach(function(el){
22+
try {
23+
window.katex.render(el.textContent, el, { displayMode: (el.dataset.display === 'true') });
24+
} catch (e) { /* ignore parse errors */ }
25+
});
26+
}
27+
});
28+
</script>
29+
{%- endif -%}

0 commit comments

Comments
 (0)