fix: track code fences line-by-line in MDX escaper - #46
Merged
Merged
Conversation
The regex-based escaper could not handle unbalanced fences: Griffe's Examples
sections sometimes emit an unterminated ```python block, and MDX auto-closes it
at end of input. The regex required a closing fence, so it treated the trailing
code as prose and escaped braces inside it (e.g. `print(f"hello {name}")` became
`print(f"hello \{name\}")`), corrupting code examples.
Replace the regex with a line-based scanner that toggles fence state on ``` lines
(mirroring CommonMark/MDX, including auto-close) and only escapes non-fenced
lines. Inline code is still matched single-line so multi-line backtick spans —
which MDX does not treat as code — fall through to prose escaping.
Adds a regression test for the unterminated-fence case.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
zzstoatzz
added a commit
that referenced
this pull request
Jun 22, 2026
Reverts the brace-escaping added in 0.2.43/0.2.44. It degraded output (turning backtick-wrapped inline code into backslash-escaped braces) and was the wrong layer to address MDX parse issues. Restores formatter.py to its pre-#45 state; the empty-module and navigation fixes from 0.2.40-0.2.42 are unaffected. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Follow-up to #45. The regex-based escaper there fixed the hard MDX parse failures but introduced a cosmetic regression: docstrings with an unterminated ```python example fence (which Griffe's Examples sections sometimes emit) had braces inside the code escaped —
print(f"hello {name}")became `print(f"hello {name}")`.MDX auto-closes an unterminated fence at end of input; the regex required a closing fence, so it misclassified the trailing code as prose. Replaced with a line-based scanner that toggles fence state on ``` lines (mirroring CommonMark/MDX), escaping only non-fenced lines. Inline code is still matched single-line so multi-line backtick spans fall through to prose escaping.
Verified against Prefect's core + integration docs:
mint broken-linksclean, and code examples keep their literal braces. Regression test added for the unterminated-fence case.🤖 Generated with Claude Code