Skip to content

fix(#72): differentiate math and escaped brackets#73

Merged
KyleKing merged 2 commits intomainfrom
fix-72
Jan 6, 2026
Merged

fix(#72): differentiate math and escaped brackets#73
KyleKing merged 2 commits intomainfrom
fix-72

Conversation

@KyleKing
Copy link
Owner

@KyleKing KyleKing commented Jan 6, 2026

No description provided.

@KyleKing KyleKing requested a review from Copilot January 6, 2026 01:50
@KyleKing KyleKing self-assigned this Jan 6, 2026
@coderabbitai
Copy link

coderabbitai bot commented Jan 6, 2026

Walkthrough

The pull request updates the Arithmatex plugin to detect and ignore escaped bracket sequences (\[...\]) so they are not parsed as math. It adds a regex and helper to identify escaped brackets, wraps two texmath block rules with a conditional wrapper to bypass math parsing when escaped brackets are present, includes imports and documentation updates, and expands tests covering escaped-bracket edge cases.


📜 Recent review details

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between befce35 and a37c660.

📒 Files selected for processing (2)
  • mdformat_mkdocs/mdit_plugins/_pymd_arithmatex.py
  • tests/format/fixtures/pymd_arithmatex_edge_cases.md
🔇 Additional comments (6)
tests/format/fixtures/pymd_arithmatex_edge_cases.md (1)

12-66: LGTM - Comprehensive test coverage for escaped bracket scenarios.

The new test cases properly cover the key edge cases: escaped brackets in lists, multi-line math blocks vs single-line escaped brackets, and escaped brackets with trailing content.

mdformat_mkdocs/mdit_plugins/_pymd_arithmatex.py (5)

20-32: LGTM - Imports are appropriate.


44-47: Regex pattern correctly identifies escaped brackets.

The pattern properly detects single-line \[...\] with non-whitespace immediately after, distinguishing escaped brackets from valid math blocks.


52-64: Logic is sound with proper bounds checking.

The function correctly:

  1. Guards against out-of-bounds access
  2. Only processes lines starting with \[
  3. Uses regex to distinguish escaped brackets from math blocks

87-95: Wrapper pattern is clean and correct.

The closure captures the original function and conditionally bypasses math parsing for escaped brackets.


97-118: Runtime validation properly guards against configuration changes.

The wrapped_count check with a clear error message addresses the fragility of relying on plugin registration order. This ensures the fix fails fast if the underlying mdit-py-plugins API changes.

Note: The private __rules__ access is a known limitation documented in past reviews - the public Ruler API cannot distinguish between rules with the same name.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Fix all issues with AI Agents 🤖
In @mdformat_mkdocs/mdit_plugins/_pymd_arithmatex.py:
- Around line 75-97: The rule-wrapping loop that modifies
md.block.ruler.__rules__ is fragile; add runtime validation to ensure the
intended texmath rules were actually wrapped. In the loop that iterates over
md.block.ruler.__rules__ (the block that references found_first_math_block,
rule.name == "math_block", rule.name == "math_block_eqno", and uses
make_wrapper(rule.fn)), increment a wrapped_count each time you replace a
rule.fn with make_wrapper and after the loop assert wrapped_count == 2 (or raise
a RuntimeError with a clear message if less), so that
make_wrapper/_is_escaped_bracket logic only proceeds when both the second
"math_block" and "math_block_eqno" have been found and wrapped.
📜 Review details

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5aa0b3c and befce35.

📒 Files selected for processing (1)
  • mdformat_mkdocs/mdit_plugins/_pymd_arithmatex.py
🔇 Additional comments (4)
mdformat_mkdocs/mdit_plugins/_pymd_arithmatex.py (4)

20-20: LGTM: Imports are necessary and correctly placed.

The re module and StateBlock type are both used appropriately in the new code.

Also applies to: 29-29


47-57: LGTM: Function logic is sound.

The early return for non-\[ lines is an efficient optimization. The function inherits any limitations from _ESCAPED_BRACKET_RE (see previous comment).


65-65: LGTM: Documentation update is clear and appropriate.


41-44: Pattern is intentionally restrictive and working as designed.

The regex correctly identifies escaped bracket notation (like \[label\]: value) that should NOT be processed as math blocks. Cases like \[content\] at line end or followed by whitespace properly fall through to the texmath parser, which is the intended behavior. The (\S) requirement after \] specifically catches the escaped bracket pattern to fix issue #72.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #72 by adding logic to differentiate between LaTeX math expressions using \[...\] syntax and escaped bracket patterns like \[test\]: value that should not be treated as math.

Key changes:

  • Adds a regex pattern and helper function to detect escaped bracket syntax on a single line with non-whitespace content immediately following the closing bracket
  • Wraps the texmath plugin's block rules to filter out escaped bracket patterns before processing math blocks

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@KyleKing KyleKing merged commit 403deec into main Jan 6, 2026
8 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