rxliuli / joplin-utils

Joplin-based community tool
https://joplin-utils.rxliuli.com/
MIT License
235 stars 36 forks source link

feat: add math rendering to notes [joplin-search-integration] #105

Closed jannikgohr closed 4 weeks ago

jannikgohr commented 4 weeks ago
rxliuli commented 4 weeks ago

I have never used KaTex. Could you provide an example of what content needs to be supported for rendering? Or add a unit test?

jannikgohr commented 4 weeks ago

I am not exactly sure how to best integrate unit tests here, but I can give a few examples on what KaTeX renders here.

KaTeX is used to render math expressions like: $$f'_{3}(x)=4$$ So they are rendered like that: $$f'_{3}(x)=4$$

There are 2 types of math expressions

  1. Inline (fenced in singe '$', like $x^2$)
  2. and Block expressions (fenced by double $ like in the example above that take up a whole line)

Joplin uses KaTeX internally as well for that.

But default KaTeX rules were slightly altered when implemented in Joplin:

Since I wanted the extension to render by the same rules, I had to make a few modifications.

escapeJoplinMathExceptions() is used to eliminate 'potential' math expressions start and end marker that wouldn't be valid in Joplin This was done by replacing $ with its Unicode representation $ After the math rendering was done, I undid that with the help of unescapeJoplinMathExceptions()

The rendering itself is done with KaTeX. I had to reference to the original Markdown content of the node to confirm whether the math expression is supposed to be inline or a block, since mdast-util-math sadly doesn't differentiate this.

Hope I could clear up what I wanted to achieve with this Pull Request!

rxliuli commented 4 weeks ago

Released 0.10.2. ref: https://github.com/rxliuli/joplin-utils/releases/tag/joplin-search-integration%400.10.2