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

👌 Improve parsing of nested amsmath #119

Merged
merged 2 commits into from
Sep 7, 2024
Merged

👌 Improve parsing of nested amsmath #119

merged 2 commits into from
Sep 7, 2024

Conversation

chrisjsewell
Copy link
Member

@chrisjsewell chrisjsewell commented Sep 6, 2024

the previous logic was problematic for amsmath blocks nested in other blocs (such as blockquotes)

the parsing code now principally follows the logic in markdown_it/rules_block/fence.py
(see also https://spec.commonmark.org/0.30/#fenced-code-blocks),
except that:

  1. it allows for a closing tag on the same line as the opening tag, and
  2. it does not allow for an opening tag without closing tag (i.e. no auto-closing)

fixes #117

the previous logic was problematic for amsmath blocks nested in other blocs (such as blockquotes)

the parsing code now principally follows the logic in markdown_it/rules_block/fence.py,
except that:
(a) it allows for closing tag on same line as opening tag
(b) it does not allow for opening tag without closing tag (i.e. no auto-closing)
@chrisjsewell
Copy link
Member Author

cc @tovrstra

Copy link

codecov bot commented Sep 6, 2024

Codecov Report

Attention: Patch coverage is 90.47619% with 2 lines in your changes missing coverage. Please review.

Project coverage is 92.74%. Comparing base (d11bdaf) to head (8f5cf97).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
mdit_py_plugins/amsmath/__init__.py 90.47% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #119      +/-   ##
==========================================
- Coverage   92.80%   92.74%   -0.07%     
==========================================
  Files          31       31              
  Lines        1835     1833       -2     
==========================================
- Hits         1703     1700       -3     
- Misses        132      133       +1     
Flag Coverage Δ
pytests 92.74% <90.47%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 202 to 216
> \begin{matrix}
> -0.707 & 0.408 & 0.577 \\
> -0.707 & -0.408 & -0.577 \\
> -0. & -0.816 & 0.577
> \end{matrix}
.
<blockquote>
<div class="math amsmath">
\begin{matrix}
-0.707 &amp; 0.408 &amp; 0.577 \\
-0.707 &amp; -0.408 &amp; -0.577 \\
-0. &amp; -0.816 &amp; 0.577
\end{matrix}
</div>
</blockquote>
Copy link

@tovrstra tovrstra Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If compatibility with Jupyter is of importance, then the HTML should be different here:

<blockquote>
<div class="math amsmath">
\begin{matrix}
&gt; -0.707 &amp;  0.408 &amp;  0.577 \\
&gt; -0.707 &amp; -0.408 &amp; -0.577 \\
&gt; -0.    &amp; -0.816 &amp;  0.577
&gt; \end{matrix}
</div>
</blockquote>

The markdown of this example is rendered in Jupyter Lab as:

jupyterlab

Or should this be treated as a Jupyter issue? In Jupyter, one would write the Markdown as:

> \begin{matrix}
-0.707 &  0.408 &  0.577 \\
-0.707 & -0.408 & -0.577 \\
-0.    & -0.816 &  0.577
\end{matrix}

Given that Jupyter is so widespread, it seems prudent to also include this example in the tests.

Copy link
Member Author

@chrisjsewell chrisjsewell Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should this be treated as a Jupyter issue?

very much so, that is definitely very wrong 😅

I'm interested in "correct parsing", not mimicking bugs of other programs 😬

If compatibility with Jupyter is of importance

As you note, what you are actually talking about here is only the jupyter lab implementation,
which relies on things like markedjs and mathjax,
and is known to have a bunch of issues with math rendering (jupyterlab/jupyterlab#8645, jupyterlab/jupyterlab#14570, etc)

by contrast, try this in https://code.visualstudio.com/docs/datascience/jupyter-notebooks and you will get perfectly correct math 😄

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems prudent to also include this example in the tests

I've added it for completeness, but to be clear, this is just a case of block quote lazy continuation: https://spec.commonmark.org/0.30/#example-233

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened a Jupyter issue: jupyterlab/jupyterlab#16755

@chrisjsewell chrisjsewell merged commit 3f7fcc6 into master Sep 7, 2024
13 checks passed
@chrisjsewell chrisjsewell deleted the fix-amsmath branch September 7, 2024 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infinite loop in amsmath extension for a simple input
2 participants