Skip to content

Commit b0832af

Browse files
committed
fix(katex): add cachebust to script URLs; add final text-node walker replacing parent span when sole child
1 parent f27e27c commit b0832af

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

static/js/katex_init.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,35 @@
107107
document.querySelectorAll('.e-content.body p, .e-content.body li, .e-content.body h1, .e-content.body h2, .e-content.body h3, .e-content.body h4, .e-content.body blockquote, .e-content.body td, .e-content.body th, .e-content.body dd, .e-content.body dt, .e-content.body figcaption').forEach(function(el){
108108
if (safeContainer(el) && el.getAttribute('data-katex-rebuilt') !== '1') rebuildWithKatex(el);
109109
});
110+
111+
// Final pass: walk text nodes to catch inline math without delimiters, even if wrapped in spans/em/strong
112+
var root = document.querySelector('.e-content.body');
113+
if (root) {
114+
var walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null);
115+
var nodes = [];
116+
var n;
117+
while ((n = walker.nextNode())) {
118+
if (!n.parentElement.closest('pre, code')) nodes.push(n);
119+
}
120+
nodes.forEach(function(tn){
121+
var s = tn.textContent.trim();
122+
if (!s) return;
123+
// Heuristic: looks like TeX (has a macro or \cdot or \frac or \text)
124+
if (/\\(text|frac|cdot|sum|int|alpha|beta|gamma)\b/.test(s) || /^\(.*\)$/.test(s)){
125+
try {
126+
var target = (tn.parentElement && tn.parentElement.childNodes.length === 1 && !tn.parentElement.querySelector('.katex')) ? tn.parentElement : null;
127+
var container = target || tn;
128+
var span = document.createElement('span');
129+
window.katex.render(escapeUnderscoresInTextCommand(s), span, {displayMode:false, strict:'ignore'});
130+
if (target) {
131+
target.replaceWith(span);
132+
} else {
133+
tn.parentNode.replaceChild(span, tn);
134+
}
135+
} catch(_) {}
136+
}
137+
});
138+
}
110139
}
111140
}
112141
if (document.readyState === 'loading') {

templates/tabi/extend_body.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{# Local KaTeX auto-render for standard delimiters and robust shortcode rendering #}
22
{%- 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 defer src="{{ get_url(path='js/katex_init.js', trailing_slash=false) | safe }}"></script>
3+
<script defer src="{{ get_url(path='js/katex_autorender.min.js', trailing_slash=false, cachebust=true) | safe }}"></script>
4+
<script defer src="{{ get_url(path='js/katex_init.js', trailing_slash=false, cachebust=true) | safe }}"></script>
55
{%- endif -%}

0 commit comments

Comments
 (0)