Fix MarkdownRenderer re-rendering of code spans containing backticks#450
Merged
Merged
Conversation
MarkdownRenderer.codespan always wrapped the content in a single backtick pair, so a code span whose content contains backticks re-parsed wrong: the inner backtick closed the span early, and a lone backtick rendered as an empty fenced code block. Size the delimiter to one more than the longest backtick run in the content, and pad with a space on each side when the content begins or ends with a backtick (the parser strips that padding back off), matching parse_codespan and CommonMark.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #450 +/- ##
==========================================
+ Coverage 91.78% 91.80% +0.01%
==========================================
Files 34 34
Lines 2653 2659 +6
Branches 433 434 +1
==========================================
+ Hits 2435 2441 +6
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.



Problem
MarkdownRenderer.codespanalways wraps the content in a single backtick pair:When the code-span content itself contains backticks, re-rendering the AST produces Markdown that no longer round-trips:
`renders as```, which re-parses as an empty fenced code block.This shows up through mistune's own round-trip oracle (
to_html(text)vsto_html(reformat(text))):Fix
Size the delimiter to one more than the longest backtick run inside the content, and add one space of padding on each side when the content begins or ends with a backtick. That matches both CommonMark and mistune's own
parse_codespan, which strips a single surrounding space back off (inline_parser.py:if code.startswith(" ") and code.endswith(" "): code = code[1:-1]).For content with no backticks the delimiter stays a single backtick, so output is byte-for-byte identical to before; only backtick-containing spans change.
Tests
Added
test_codespan_containing_backtickstoTestMarkdownRendererRoundTrip, exercising single/double inner backticks plus leading, trailing, and surrounding backticks through the existingassert_round_triphelper. It fails before the change and passes after. Full suite: 985 passed;ruff check,ruff format, andmypyall clean.I used an AI assistant to help investigate this under my direction, and I have reviewed and verified the change myself.