Skip to content

fix: track code fences line-by-line in MDX escaper - #46

Merged
zzstoatzz merged 1 commit into
mainfrom
fix/mdx-escape-linebased
Jun 22, 2026
Merged

zzstoatzz merged 1 commit into
mainfrom
fix/mdx-escape-linebased

Conversation

@zzstoatzz

Copy link
Copy Markdown
Owner

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-links clean, and code examples keep their literal braces. Regression test added for the unterminated-fence case.

🤖 Generated with Claude Code

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
zzstoatzz merged commit c95664f into main Jun 22, 2026
@zzstoatzz
zzstoatzz deleted the fix/mdx-escape-linebased branch June 22, 2026 18:44
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>
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.

1 participant