Skip to content

Commit

Permalink
Fence KaTeX math expressions
Browse files Browse the repository at this point in the history
Flexmark would parse and corrupt the contents of KaTeX math expressions.
This commit circumvents the problem by fencing the math expressions.

Fixes gsantner#1389 and gsantner#1393.
  • Loading branch information
Florian Tham committed Feb 3, 2022
1 parent 79317e3 commit b620407
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ public String convertMarkup(String markup, Context context, boolean isExportInLi
if (appSettings.isMarkdownMathEnabled() && markup.contains("$")) {
head += HTML_KATEX_INCLUDE;
onLoadJs += JS_KATEX;
markup = markup.replaceAll("(?ms)^([$]{2}.*?[$]{2})$", "<div>\n$1\n</div>");
// Temporarily fence math expressions by masking them as code enclosed in math tags.
markup = markup.replaceAll("(?ms)(?<![[:graph:]])(([$]{1,2}).*?\\2)", "<math>`$1`</math>");
}

// Enable View (block) code syntax highlighting
Expand Down Expand Up @@ -239,6 +240,15 @@ public String convertMarkup(String markup, Context context, boolean isExportInLi
}
}

// After render changes: Math
if (converted.contains("<math><code>$") || converted.contains("<math>`$")) {
converted = converted.replaceAll("(?ms)<math><code>(([$]{1,2}).*?\\2)</code></math>", "$1");
// Math tags semm to be fenced when found at the beginning of a line. In this case, the backticks
// were not converted into code tags.
converted = converted.replaceAll("(?ms)<math>`(([$]{1,2}).*?\\2)`</math>", "$1");

}

// Deliver result
return putContentIntoTemplate(context, converted, isExportInLightMode, file, onLoadJs, head);
}
Expand Down

0 comments on commit b620407

Please sign in to comment.