Conversation
image() in src/mdformat/renderer/_context.py rendered node.attrs['title']
directly into the parenthesized `(uri "title")` form without escaping
embedded double quotes, unlike link() a few lines below which already
does `title.replace('"', '\\\\"')`. An unescaped quote in an image title
terminates the title early, producing malformed Markdown, and made
mdformat.text() non-idempotent on such input (a second format pass
reparsed the corrupted output differently, even escaping the image
brackets themselves).
Mirror link()'s escaping in image() so titles round-trip correctly.
Adds a `link and image titles with embedded quotes` case to
tests/data/default_style.md covering both link and image titles.
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 #586.
Root cause
image()insrc/mdformat/renderer/_context.pywritesnode.attrs["title"]straight into the(uri "title")form without escaping embedded double quotes:link(), a few lines below, already handles this correctly:An unescaped
"in an image title closes the title early, producing malformed Markdown, and makesmdformat.text()non-idempotent on such input — reformatting the (already malformed) output a second time reparses it differently and even escapes the image's own brackets.Reproduced exactly as described in the issue:
Fix
Mirror
link()'s escaping inimage().Tests
Added a
link and image titles with embedded quotescase totests/data/default_style.mdcovering both link and image titles with an embedded, escaped quote. Confirmed this case fails against the unfixed code —mdformat._cli.run()actually raisesCould not format ...: Formatted Markdown renders to different HTML than input Markdown, mdformat's own consistency check catching the corruption — and passes after the fix. Full test suite: 4286 passed, 1 skipped, 0 failures.