Skip to content

fix(math): reject currency patterns and cross-line matches in inline math - #447

Merged
lepture merged 1 commit into
lepture:mainfrom
geopanther:fix/inline-math-currency-pattern
Jun 16, 2026
Merged

fix(math): reject currency patterns and cross-line matches in inline math#447
lepture merged 1 commit into
lepture:mainfrom
geopanther:fix/inline-math-currency-pattern

Conversation

@geopanther

Copy link
Copy Markdown
Contributor

Summary

Mistune's INLINE_MATH_PATTERN has 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)\$:

  1. Broken lookahead: The second (?!\s) checks the closing $ delimiter (which is never whitespace), not the preceding content character. It's a no-op.
  2. Cross-line matching: [^$\\] allows newlines, so $ signs on different lines can pair as math delimiters.
  3. No digit boundary: Closing $ 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)
  • Remove broken (?!\s) before closing $ (was always a no-op)

Tests

New fixture test cases added covering:

  • Valid math: $x^2$, $n$th, trailing punctuation
  • Currency rejection: $51,300 pairs, single $1.6T
  • Cross-line rejection

All 978 existing tests pass with no regressions.

Credit

Bug identified via geopanther/mdfluence#70.

…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)
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Jun 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.75%. Comparing base (f21a29b) to head (566e173).

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           
Flag Coverage Δ
unittests 91.71% <100.00%> (ø)

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

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lepture
lepture merged commit 74608f5 into lepture:main Jun 16, 2026
24 checks passed
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.

2 participants