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

[Bug report]: MathJax (LaTeX notation) inline value is NOT rendered with Highlighting ON #2084

Closed
marchank0 opened this issue Oct 5, 2023 · 2 comments · Fixed by #2098
Closed
Labels
bug Something isn't working.

Comments

@marchank0
Copy link

What happened?

Inline field with brackets (curly as well) is NOT rendered with Highlighting ON
image

Source view:
image

Reading view:
image

Table view works properly:
image

DQL

NA

JS

NA

Dataview Version

0.5.59

Obsidian Version

1.4.14

OS

MacOS

@marchank0 marchank0 added the bug Something isn't working. label Oct 5, 2023
@RyotaUshio
Copy link
Contributor

RyotaUshio commented Oct 8, 2023

I want to mention that [complexity:: $O(n^2)$] is correctly rendered in live preview.

And I've finally identified the root cause of this bug!
It happens because Obsidian API's MarkdownRenderer.renderMarkdown cannot handle rendered MathJax HTML source code like this:

<span class="math math-inline is-loaded"><mjx-container class="MathJax" jax="CHTML"><mjx-math class="MJX-TEX"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D442 TEX-I"></mjx-c></mjx-mi><mjx-mo class="mjx-n"><mjx-c class="mjx-c28"></mjx-c></mjx-mo><mjx-msup><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D45B TEX-I"></mjx-c></mjx-mi><mjx-script style="vertical-align: 0.363em;"><mjx-mn class="mjx-n" size="s"><mjx-c class="mjx-c32"></mjx-c></mjx-mn></mjx-script></mjx-msup><mjx-mo class="mjx-n"><mjx-c class="mjx-c29"></mjx-c></mjx-mo></mjx-math></mjx-container></span>

More details: Let's take an inline field [complexity:: $O(n^2)$] as an example.

Before Dataview's MarkdownPostProcessor tries to render the value part ($O(n^2)$) of this field,
the raw value string $O(n^2)$ is first converted to the above HTML code, and then Dataview's MarkdownPostProcessor parses the resulting HTML to find inline fields:

let inlineFields = extractInlineFields(init.container.innerHTML);

As a result, [complexity:: $O(n^2)$] results in the following InlineField object:

{
    key: "complexity", 
    value: "<span class="math math-inline is-loaded"><mjx-container class="MathJax" jax="CHTML"><mjx-math class="MJX-TEX"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D442 TEX-I"></mjx-c></mjx-mi><mjx-mo class="mjx-n"><mjx-c class="mjx-c28"></mjx-c></mjx-mo><mjx-msup><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D45B TEX-I"></mjx-c></mjx-mi><mjx-script style="vertical-align: 0.363em;"><mjx-mn class="mjx-n" size="s"><mjx-c class="mjx-c32"></mjx-c></mjx-mn></mjx-script></mjx-msup><mjx-mo class="mjx-n"><mjx-c class="mjx-c29"></mjx-c></mjx-mo></mjx-math></mjx-container></span>", 
    ....
}

That is, the value is not the original raw latex $O(n^2)$, but a MathJax HTML code.

Then, the resulting HTML code is passed to RawLit(), and then RawMarkdown(), and finally Obsidian API's MarkdownRenderer.renderMarkdown() function.

MarkdownRenderer.renderMarkdown() handles "standard" HTML codes like <strong>Bold</strong> well, but it turned out that it cannot handle MathJax's output HTML like the above.
When I passed the above MathJax HTML to it, its output was

<div><p><span class="math math-inline is-loaded"></span></p></div>

So it removes all the HTML tags defined by MathJax (e.g. <mjx-container>).

On the other hand, in live preview, inline fields are parsed from the original raw text (not HTML!).
As a result, [complexity:: $O(n^2)$] results in the following InlineField object:

{
    key: "complexity", 
    value: "$O(n^2)$", 
    ....
}

whose value is just a raw markdown string and thus can be correctly processed by MarkdownRenderer.renderMarkdown.
This is why [compoxity:: $O(n^2)$] is properly rendered in live preview, unlilke in the reading view.

@RyotaUshio
Copy link
Contributor

RyotaUshio commented Oct 8, 2023

Updated the above comment based on a new observation.

blacksmithgu pushed a commit that referenced this issue Oct 9, 2023
…ot HTML) (#2098)

Co-authored-by: RyotaUshio <ushio@ms.k.u-tokyo.ac.jp>
reply2za added a commit to reply2za/obsidian-dataview that referenced this issue Jun 19, 2024
blacksmithgu pushed a commit that referenced this issue Jun 20, 2024
* Fix inline rendering in reading view (#2216)

* fix: add math inline reading view rendering (#2084)

* refactor: update field semantic

* refactor: cleanup inline-field code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants