Skip to content

fix: re-fence code spans with inner backticks in MarkdownRenderer#457

Closed
sarathfrancis90 wants to merge 1 commit into
lepture:mainfrom
sarathfrancis90:fix-markdown-codespan-backticks
Closed

fix: re-fence code spans with inner backticks in MarkdownRenderer#457
sarathfrancis90 wants to merge 1 commit into
lepture:mainfrom
sarathfrancis90:fix-markdown-codespan-backticks

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

While reformatting Markdown with MarkdownRenderer I noticed inline code spans get mangled when their content contains a backtick. codespan always wraps the content in a single backtick, so ab`` `` comes back as `` ab and re-parses to `<code>a</code>b instead of <code>ab. The same thing happens to content padded with spaces (`` x `` drops a space on each side) and to a lone backtick (`` `` turns into a code fence).

I changed codespan to fence with one more backtick than the longest run inside the content, and to pad with a space when the content starts or ends with a backtick so it survives the code-span space-stripping rule on re-parse.

Added round-trip cases to TestMarkdownRendererRoundTrip. Verified with pytest, mypy --strict src and mypy src tests; I also fuzzed random code spans through reformat then HTML against the originals and they match.

MarkdownRenderer.codespan always wrapped the content in a single backtick,
so reformatting any code span that contained a backtick (or was padded with
spaces) changed its meaning on re-parse, e.g. ``a`b`` became `a`b`.

Fence with one more backtick than the longest inner run, and pad with a
space when the content starts or ends with a backtick so it survives the
code-span space-stripping rule.
@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.03%. Comparing base (90b1a73) to head (8b3e38b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #457      +/-   ##
==========================================
+ Coverage   91.01%   91.03%   +0.01%     
==========================================
  Files          34       34              
  Lines        3395     3401       +6     
  Branches      670      672       +2     
==========================================
+ Hits         3090     3096       +6     
  Misses        184      184              
  Partials      121      121              
Flag Coverage Δ
unittests 91.00% <100.00%> (+0.01%) ⬆️

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 commented Jun 26, 2026

Copy link
Copy Markdown
Owner

Is it the same as #450 ?

@sarathfrancis90

Copy link
Copy Markdown
Contributor Author

Yes — same fix as #450, and that one came first. I missed it before opening this; apologies for the duplicate. #450 covers the same cases (delimiter sized past the longest inner backtick run, plus space-padding for a leading/trailing backtick), so I'll close this in its favor.

@sonarqubecloud

Copy link
Copy Markdown

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