Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance LaTeX rendering in Markdown with support for environments #269

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions fasthtml/js.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from fastcore.utils import *
from fasthtml.xtend import Script,jsd,Style,Link

Expand All @@ -12,16 +13,41 @@ def MarkdownJS(sel='.marked'):
src = "proc_htmx('%s', e => e.innerHTML = marked.parse(e.textContent));" % sel
return Script(marked_imp+src, type='module')

def KatexMarkdownJS(sel='.marked', katex_tags='$'):
right_tags = '\\$' if katex_tags=='$' else '\\]'
def KatexMarkdownJS(sel='.marked', inline_delim='$', display_delim='$$', math_envs=None):
math_envs = math_envs or ['equation', 'align', 'gather', 'multline']
env_list = ','.join(f"'{env}'" for env in math_envs)

src = """
rian-dolphin marked this conversation as resolved.
Show resolved Hide resolved
import katex from "https://cdn.jsdelivr.net/npm/katex/dist/katex.mjs";
const renderMath = tex => katex.renderToString(tex, {throwOnError: false, displayMode: false});

const renderMath = (tex, displayMode) => {
return katex.renderToString(tex, {
rian-dolphin marked this conversation as resolved.
Show resolved Hide resolved
throwOnError: false,
displayMode: displayMode,
output: 'html',
trust: true
});
};
const processLatexEnvironments = (content) => {
return content.replace(/\\\\begin{(\\w+)}([\\s\\S]*?)\\\\end{\\1}/g, (match, env, innerContent) => {
rian-dolphin marked this conversation as resolved.
Show resolved Hide resolved
if ([%s].includes(env)) {
rian-dolphin marked this conversation as resolved.
Show resolved Hide resolved
return `%s${match}%s`;
}
return match;
});
};
proc_htmx('%s', e => {
e.innerHTML = marked.parse(e.textContent).replace(/%s{1,2}\\n*(.+?)\\n*%s{1,2}/g, (_, tex) => renderMath(tex));
let content = processLatexEnvironments(e.textContent);
// Handle display math (including environments)
content = content.replace(/%s([\\s\\S]+?)%s/gm, (_, tex) => renderMath(tex.trim(), true));
// Handle inline math
content = content.replace(/(?<!\\w)%s([^%s\\s](?:[^%s]*[^%s\\s])?)%s(?!\\w)/g, (_, tex) => renderMath(tex.trim(), false));
e.innerHTML = marked.parse(content);
});
""" % (sel, "\\"+katex_tags, right_tags)
""" % (env_list, re.escape(display_delim), re.escape(display_delim),
sel, re.escape(display_delim), re.escape(display_delim),
re.escape(inline_delim), re.escape(inline_delim), re.escape(inline_delim),
re.escape(inline_delim), re.escape(inline_delim))

return (Script(marked_imp+src, type='module'),
Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css"))

Expand Down
Loading