Commit b042996
committed
fix: escape literal emphasis markers in MarkdownRenderer
MarkdownRenderer.text() only escaped backticks, so a text token that is a
literal "*"/"_" emphasis delimiter was emitted unescaped and re-parsed as
emphasis on the round-trip. This silently changed the document's meaning,
violating the invariant asserted by TestMarkdownRendererRoundTrip
("reformatting valid Markdown must not change its meaning").
An escaped marker in the source (``\*not emphasis\*``) is stored by the
inline parser as bare "*" text tokens (the backslash is consumed and the
parser's internal no-emphasis flag is dropped before the AST). Re-emitting
those tokens without a backslash let them pair up again:
reformat(r"\*not emphasis\*") -> "*not emphasis*"
html(reformat(...)) -> "<p><em>not emphasis</em></p>" # was <p>*not emphasis*</p>
The same happened for "_", "**"/"__", and unmatched leftover markers.
Fix: in text(), if a text token consists entirely of "*"/"_" it is a
literal delimiter (an escaped marker or an unmatched leftover), so
backslash-escape each character. Prose punctuation such as "2 * 3" or
"snake_case" always arrives mixed with other characters, so it is left
untouched and is not over-escaped (guarded by the existing
test_prose_not_over_escaped and a new test_real_emphasis_not_over_escaped).
Added test_escaped_emphasis_markers and test_real_emphasis_not_over_escaped
to TestMarkdownRendererRoundTrip. The regression test fails without the
source change (asserts <em>...</em> reappears) and passes with it; full
suite 1122 passed, ruff check/format and mypy clean.1 parent fe02f40 commit b042996
3 files changed
Lines changed: 45 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
6 | 12 | | |
7 | 13 | | |
8 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
43 | 52 | | |
44 | 53 | | |
45 | | - | |
| 54 | + | |
46 | 55 | | |
47 | 56 | | |
48 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
49 | 78 | | |
50 | 79 | | |
51 | 80 | | |
| |||
0 commit comments