Fix error on emphasis around whitespace character reference (#492)#583
Open
apoorvdarshan wants to merge 1 commit into
Open
Fix error on emphasis around whitespace character reference (#492)#583apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
Emphasis whose content is a whitespace character reference such as `*&hukkin#32;*` triggered the AST safety check: markdown-it decodes the reference to literal whitespace in the text token, and mdformat emitted that literal whitespace between the emphasis markers. On re-parse, a Unicode-whitespace flanking character can no longer open/close emphasis, so the formatted output rendered to different HTML than the input. Re-decimalify leading/trailing whitespace of em/strong content, mirroring the existing handling in `paragraph()`, so boundary whitespace stays in character reference form and the emphasis markers remain functional.
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.
Fixes #492
Root cause
For emphasis whose content is a whitespace character reference, e.g.
markdown-it decodes the reference to literal whitespace in the text token's
content(" "), keeping the original source only in the token'smarkupfield. The
em/strongrenderers emitted that literal whitespace directlybetween the emphasis markers, producing
* *. On re-parse, aUnicode-whitespace flanking character can no longer open or close emphasis
(CommonMark emphasis rules), so the formatted Markdown renders to different
HTML than the input and the AST safety check aborts with an error.
Reproduction
On the pristine
masterclone (mdformat 1.0.0):Internally (validation disabled) mdformat produced
\* *, whereas:* *renders to<p><em> </em></p>\* */* *renders to<p>* *</p>(no emphasis) — hence the mismatch.With the fix:
Both round-trip to the same HTML as the input.
Fix
In
em()andstrong()(src/mdformat/renderer/_context.py), re-decimalifyleading/trailing Unicode whitespace of the rendered inner text using the
existing
decimalify_leading/decimalify_trailinghelpers. This mirrorsexactly what
paragraph()already does for whitespace at paragraphboundaries, so boundary whitespace is kept in character-reference form and the
emphasis markers stay functional. The change is localized to the two emphasis
renderers; non-whitespace content is untouched.
Tests
Added two round-trip cases to
tests/data/default_style.md(driven bytests/test_style.py, which runs the real CLI including the AST check):* *→* *** **→** **Confirmed these fail on unmodified code (the CLI exits non-zero via the AST
check) and pass with the fix. The full test suite passes locally
(
4286 passed, 1 skipped), including all CommonMark spec fixtures, andblack/isort/flake8report no issues on the changed file.Disclosure: prepared with AI assistance; reviewed and verified locally.