Skip to content

Commit

Permalink
Fix KaTeX.
Browse files Browse the repository at this point in the history
  • Loading branch information
aterenin committed May 3, 2024
1 parent a453e82 commit 6274e55
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion scripts/katex.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const katex = require('katex');
function renderFile(p) {
const t = fs.readFileSync(p, {encoding: 'utf8', flag: 'r'});
const u = t.replaceAll(/<code>\$([^\$]*)\$<\/code>/g, (_, m) => katex.renderToString(m));
const v = u.replaceAll(/<pre class=z-code><code><span class="z-text z-plain">\s*\$\$([^\$]*)\$\$\s*<\/span><\/code><\/pre>/g, (_, m) => katex.renderToString(m.replaceAll('<span class="z-text z-plain">', '').replaceAll('</span>', ''), {displayMode: true}));
const v = u.replaceAll(/<pre class=z-code><code><span class="z-text z-plain">\s*\$\$([^\$]*)\$\$\s*<\/span><\/code><\/pre>/g, (_, m) => '<p>' + katex.renderToString(m.replaceAll('<span class="z-text z-plain">', '').replaceAll('</span>', ''), {displayMode: true}) + '</p>');
fs.writeFileSync(p, v);
}

Expand Down
12 changes: 6 additions & 6 deletions templates/includes/katex.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
e.parentNode.replaceChild(s,e);
}
});
document.querySelectorAll('pre.z-code code span.z-text.z-plain').forEach(e=>{
let t = e.innerText.trim();
if(t.startsWith('$$')&&t.endsWith('$$')){
let c = e.parentNode.parentNode;
document.querySelectorAll('pre.z-code code:not([class])').forEach(e=>{
if(e.firstChild.innerText.trim().startsWith('$$')&&e.lastChild.innerText.trim().startsWith('$$')){
let t = Array.from(e.childNodes).slice(1,e.childElementCount-1).map(n=>n.innerText).join(' ');
let z = e.parentNode;
let p = document.createElement('p');
katex.render(t.slice(2,-2),p,{displayMode:true});
c.parentNode.replaceChild(p,c);
katex.render(t,p,{displayMode:true});
z.parentNode.replaceChild(p,z);
}
});
});
Expand Down

0 comments on commit 6274e55

Please sign in to comment.