-
Notifications
You must be signed in to change notification settings - Fork 356
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
Fence KaTeX math expressions #1576
Conversation
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.
Ok, this doesn't work. |
If you just disable the math related markdown-parse-flexmark extensions, does it work then?
|
It does work, at least outside of code blocks. So all the examples in the two issues render correctly. The problem is that |
This reverts commit b620407. Works fine outside of code blocks, but not inside. Needs another approach instead.
Stripped down and modified flexmark-ext-gitlab to support KaTeX. Works: math inline mode. Doesn't work: math display mode.
Messing with regular expressions just doesn't work here. I think the correct approach is to implement a flexmark extension that handles math environments where it is appropriate, and just leaves them alone where it is not (e.g. within code blocks). Fortunately the GitLab Flavoured Markdown extension already has great support for both inline and display math modes. I stripped the extension of everything that's not needed for math rendering and adopted it to support KaTex inline It looks good on my notes so far. What doesn't work:
<script>
(function () {
document.addEventListener("DOMContentLoaded", function () {
var mathElems = document.getElementsByClassName("katex");
var elems = [];
for (const i in mathElems) {
if (mathElems.hasOwnProperty(i)) elems.push(mathElems[i]);
}
elems.forEach(elem => {
katex.render(elem.textContent, elem, { throwOnError: false, displayMode: elem.nodeName !== 'SPAN', });
});
});
})();
</script> The math font size needed a bit adjusting as well: <style>
.katex { font-size: 105%; }
</style> |
yeah it's a bit tricky, as both KaTex (at view/javascript-time) and Markdown parser (at generator-time) are greedy for everything they can get, and mess up the other one :D. Generally don't want to mess too much with the (KaTex) dependencies, it took quite some effort to make users somewhat happy. If now the one or other big chunk isn't working anymore guess bugreports are flooded again ... instead of these 2 small ones :D. Creating a new Flexmark extension is something I currently wouldn't prefer todo. We are kind of forced to an older version currently, and upgrading the version requires much rewrites then again. This is due the flexmark version Markor currently uses .. works fine with older Java and thus Android version. Newer versions require newer Java version, which would mean I need to drop 🔪 many users and their devices .
Is there something in the way when directly enabling the GitLab extension, or that part? I think copy-pasting some sections of it might just make it hard to maintain, and gets left behind when flexmark themself might have things fixed already Preferably, if turned out we kinda fork the flexmark katex code ... please combine it to one file, with nested/inner classes:
|
What about adding a toggle in the settings to switch between current and new math mode handling? If something breaks, users have an option to switch back.
GitLab Flavoured Markdown does not support
Switching from current to GFM would have each user adjust their documents. Unfortunately the math environments are hardcoded in flexmark-ext-gitlab, that's why I duplicated, stripped down and modified the extension.
Yes, it's a pain. If not right now, then later.
I can have a go at it. If you think it's not worth it, or too much hassle in the long run to maintain a separate KaTeX extension, I can fully understand it. So, what do you think? Should I continue? |
I could not figure out how to make the JS work from `JS_KATEX`, so I placed it in a separate file instead.
In addition to `$...$` and `$$...$$`, math rendering can now be enabled with `\(...\)` for inline and `\[...¸]` for display mode.
This reverts commit 9ece193. Does not work reliably.
The problematic examples render correctly. I did lots of testing and couldn't find any problems. It fixes an additional issue with dollar signs in text: currently, both "$foo $bar" and "\$foo \$bar" are interpreted as math mode in Markor. Instead, with this PR applied, the escaped dollar signs prevent math mode. The issue with I tried to consolidate the source files into one using nested classes and failed. I need to do some reading first, as I have no prior knowledge of Java. It will take time. I could ask the flexmark devs if they would accept a math extension. Maybe that would ease things in the long run. |
The divs are from me. They were there to mark an explicit section where markdown parser should not do its job, so at JS "runtime" the katex-js can pick up the text. |
I zipped the files a bit up .. to just one file |
c4f83d5
to
74c7bb8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you feel this is safe enough to give to users? so it not breaks existing rules? 😄
otherwise guess it's gonna be in soon
It really does make a difference. Current Markor v2.8.5 from f-droidMordor without
|
what about font-size: inherit? like |
a818d5b
to
757efd4
Compare
You are right, inherit makes sense. |
ok so it works? good, then it scales also fine with global font-size rules something else remaining? |
Seems good from my side, nothing remains so far. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flexmark would parse and corrupt the contents of KaTeX math expressions.
This commit circumvents the problem by fencing the math expressions.
Fixes #1389 and #1393.
Could use more testing.