fix: protect special characters in math from markdown table parsers (#1462)#8000
fix: protect special characters in math from markdown table parsers (#1462)#8000hugogu wants to merge 3 commits into
Conversation
Wiki.js uses markdown-it-attrs which interprets curly braces inside
inline math ($...$) as attribute directives, stripping them from the
formula. Additionally, markdown table parsers split cells at both `|`
and `&` characters, breaking formulas containing those symbols.
This fix replaces `{`, `}`, `|`, and `&` inside math expressions with
Unicode Private Use Area placeholders during markdown parsing, then
restores them before passing to KaTeX/MathJax for rendering.
- `<E000>` / `<E001>`: temporary replacements for `{` / `}`
- `<E002>`: temporary replacement for `|` (table cell delimiter)
- `<E003>`: temporary replacement for `&` (table cell delimiter in
multiline tables, used by LaTeX cases/arrays)
The placeholder approach was chosen over HTML escaping because it
preserves LaTeX environments like `\begin{array}` that were broken
by the previous `{{}}` escaping method.
Fixes requarks#1581
Fixes requarks#1462
Co-authored-by: Claude <noreply@anthropic.com>
AI-model: kimi-for-coding/k2p6
Unclosed $ delimiters would span across multiple lines, causing protectMathPipes() to corrupt table cell delimiters in unrelated content. Limit inline math matching to the same line to prevent false matches. Co-authored-by: Claude <noreply@anthropic.com> AI-model: kimi-for-coding/k2p6
|
Hi hugogu, thx for the PR! I implement it and it indeed solves the problem! But I found it also breaks the math rendering in preview. Now I can only see a bunch of latex syntex in preview, which makes latex debugging impossible until 'save and render'. |
Hi vaagee, I think the fix also works for the preview pane, I'll check it up later. |
HI hugogu, no worries. It's my own problem. The function "restoreBraces" was somehow lost when merging your code to mine, leading to the rendering bugs. |
Change Summary
Wiki.js uses markdown-it-attrs which interprets curly braces inside inline math ($...$ ) as attribute directives, stripping them from the formula. Additionally, markdown table parsers split cells at both
|and&characters, breaking formulas containing those symbols.This fix replaces
{,},|, and&inside math expressions with Unicode Private Use Area placeholders during markdown parsing, then restores them before passing to KaTeX/MathJax for rendering.<E000>/<E001>: temporary replacements for{/}<E002>: temporary replacement for|(table cell delimiter)<E003>: temporary replacement for&(table cell delimiter in multiline tables, used by LaTeX cases/arrays)The placeholder approach was chosen over HTML escaping because it preserves LaTeX environments like
\begin{array}that were broken by the previous{{}}escaping method.Fixes #1581
Fixes #1462
Test
The latest build by now (2.5.314) render it as

and
After the fix all latex text will be rendered correctly:
and