Skip to content

[Fix] Improve math parsing validation#6030

Merged
nathanlesage merged 3 commits into
Zettlr:developfrom
benniekiss:math-parser
Dec 27, 2025
Merged

[Fix] Improve math parsing validation#6030
nathanlesage merged 3 commits into
Zettlr:developfrom
benniekiss:math-parser

Conversation

@benniekiss

Copy link
Copy Markdown
Collaborator

Description

This PR fixes two issues with inline single-delimiter math parsing I experienced while drafting a snippet in the main editor:

  1. When a line contains a dollar sign, and the next line starts with a dollar sign, the content between is interpreted as math text.

  2. When the closing delimiter is followed by a digit, the content is still interpreted as math despite the delimiter being invalid, according to pandoc docs.

It also better aligns the block math parser with pandoc so that blank lines are no longer valid within math blocks.

Changes

The whitespace tests were moved so that the tests are only performed when needed. This reduces unnecessary calculations.

The following changes are from what I understand of the pandoc docs on the delimiters:

The whitespace checks were refactored to a regex test for any whitespace character using \s. I changed this because I interpreted the pandoc docs, must have a non-space character, to mean any whitespace.

An additional test was added for closing delimiters to ensure they are not followed by a digit.

The whitespace and following-digit tests are ignored if the delimiter is for display math, $$, which do not have limitations on preceding or following characters.

The math block parser had an additional check added to make sure that no lines within the block are blank.

Additional information

Tested on: macOS 26

@benniekiss

Copy link
Copy Markdown
Collaborator Author

After doing some tests with pandoc, there's some inconsistent behavior. Newlines after the opening delimiter invalidate the inline math, but newlines before the closing delimiter do not. For example:

Invalid:

$
x^2+y*(3+2)=z^2$

Valid:

$x^2+y*(3+2)=z^2
$

Maybe this is a pandoc bug? I feel like the newline before the closing delimiter should also be invalid.

@nathanlesage

Copy link
Copy Markdown
Member

@tarleb Do you have more insight on this behavior? I think it makes sense to align the Zettlr parser as much as possible to the Pandoc behavior.

@tarleb

tarleb commented Dec 1, 2025

Copy link
Copy Markdown

Huh, interesting. That's probably a bug, as newlines before the closing $ should be forbidden, too.

@benniekiss

Copy link
Copy Markdown
Collaborator Author

FWIW I opened an issue here jgm/pandoc#11311

@benniekiss benniekiss force-pushed the math-parser branch 2 times, most recently from 63360a8 to c01cc07 Compare December 8, 2025 15:50
@benniekiss

Copy link
Copy Markdown
Collaborator Author

The pandoc issue has been cleared up! This implementation now matches the pandoc output (once a new version is released)

@nathanlesage nathanlesage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just had a question, because the logic you've used seems a bit more involved, and just want to make sure I understand it!

Comment thread source/common/modules/markdown-editor/parser/math-parser.ts
Comment thread source/common/modules/markdown-editor/parser/math-parser.ts
@nathanlesage

Copy link
Copy Markdown
Member

Found a discrepancy between Pandoc and Zettlr:

This is some test $x=1$3. And another test $y=2$. detects as math from the first to the last dollar sign, showing an error. Pandoc, on the other hand, fully disregards both the first dollar signs, and properly renders the second pair.

@benniekiss

Copy link
Copy Markdown
Collaborator Author

Found a discrepancy between Pandoc and Zettlr:

This is some test $x=1$3. And another test $y=2$. detects as math from the first to the last dollar sign, showing an error. Pandoc, on the other hand, fully disregards both the first dollar signs, and properly renders the second pair.

The same happens with spaces around the delimiters. I think handling this case will actually make the parsing simpler, but it's going to be a significant refactor. I have an idea though and should have something up in a bit

@benniekiss

Copy link
Copy Markdown
Collaborator Author

Ok, I think the solution was actually much simpler, no refactoring needed. This handles the edge case you presented. I think this is good to merge on my end,

@nathanlesage nathanlesage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is good -- merging!

@nathanlesage nathanlesage merged commit 1419a91 into Zettlr:develop Dec 27, 2025
1 check passed
@benniekiss benniekiss deleted the math-parser branch December 27, 2025 17:26
benniekiss added a commit to benniekiss/Zettlr that referenced this pull request Dec 28, 2025
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.

3 participants