fix(math): reject currency patterns and cross-line matches in inline math - #447
Merged
lepture merged 1 commit intoJun 16, 2026
Merged
Conversation
…math Fix three bugs in INLINE_MATH_PATTERN that cause false-positive inline math detection on currency values like $51,300: 1. Add (?!\$) after opening $ to reject $$ block delimiter starts 2. Change [^$\\] to [^$\\\n] to disallow newlines (single-line only) 3. Add (?!\d) after closing $ to reject currency (GFM-compatible) 4. Remove broken (?!\s) lookahead before closing $ (was always a no-op since it checked the $ delimiter itself, not preceding content) The (?!\s) at the opening is preserved — inline math still requires non-whitespace after the opening $. Reported-by: geopanther (geopanther/mdfluence#70)
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #447 +/- ##
=======================================
Coverage 91.75% 91.75%
=======================================
Files 34 34
Lines 2644 2644
Branches 432 432
=======================================
Hits 2426 2426
Misses 146 146
Partials 72 72
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Mistune's
INLINE_MATH_PATTERNhas bugs that cause false-positive inline math detection on currency values like$51,300. This PR fixes them directly in the upstream regex.Root Cause
Three issues in the current regex
\$(?!\s)(?P<math_text>(?:[^$\\]|\\.)+?)(?!\s)\$:(?!\s)checks the closing$delimiter (which is never whitespace), not the preceding content character. It's a no-op.[^$\\]allows newlines, so$signs on different lines can pair as math delimiters.$followed by a digit (e.g.$51,300) is accepted, matching currency as math.Additionally, no
(?!\$)guard prevents partial matching of$$block delimiters as inline math.Fix
New pattern:
\$(?!\$)(?!\s)(?P<math_text>(?:[^$\\\n]|\\.)+?)\$(?!\d)Changes:
(?!\$): reject$$block delimiter starts[^$\\\n]: disallow newlines in inline math (single-line only)(?!\d): closing$cannot precede a digit (GFM-compatible, rejects currency)(?!\s)before closing$(was always a no-op)Tests
New fixture test cases added covering:
$x^2$,$n$th, trailing punctuation$51,300pairs, single$1.6TAll 978 existing tests pass with no regressions.
Credit
Bug identified via geopanther/mdfluence#70.