Repository navigation
Resolve backslash escapes in image alt text - #95
Open
MattFisher wants to merge 1 commit into
Open
MattFisher wants to merge 1 commit into
MattFisher wants to merge 1 commit into
Conversation
markdown-it keeps an image's alt text as raw Markdown source, so an escaped character such as "\*" reached Substack with its backslash. That also broke the export round trip: the exporter escapes "\\" and "]" in alt text, and reading the Markdown back kept those escapes. Resolve backslash escapes when building the image node, and have the exporter escape "[" as well so an unbalanced bracket in alt text no longer stops the line being read back as an image. Co-Authored-By: Claude <noreply@anthropic.com>
This was referenced Nov 16, 2026
This branch has not been deployed
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.
Backslash escapes in image alt text reach Substack with the backslash still in place, and alt text containing a backslash or square bracket does not survive export and re-import. This PR fixes both.
The problem
markdown-it stores an image's alt text as raw Markdown source, and
_captioned_image()uses it as-is. So:is stored with the alt text
find . -iname "\*taxes\*"instead offind . -iname "*taxes*". The escapes are needed in the source, because other Markdown tools read unescaped*…*in alt text as emphasis and drop the asterisks.The same thing breaks the export round trip.
mdexportalready escapes\and]in alt text, but reading that Markdown back keeps the escapes:back\slash and ] bracketback\\slash and \] bracketback\\slash and \] bracketan [unbalanced bracketan [unbalanced bracketThe change
mdrender: resolve backslash escapes (a backslash before ASCII punctuation) when building the image node.mdexport: escape[as well as\and], so an unbalanced bracket can no longer stop the line being read as an image.Alt text without backslashes is unaffected. Unescaped markup such as
*word*is still kept literally, as it is today.Testing
test_from_markdown_features.pyfor escapes on import, including that\q(not an escape) is left alone.test_markdown_export.pyfor alt text containing backslashes,], an unbalanced[, and literal\*.pytest -q -m "not live" --strict-markers: 296 passed.\*in Markdown was stored without backslashes.Note
#94 touches the lines next to this one in
mdrender.py, so whichever of the two is merged second will need a small rebase. I'm happy to do that.🤖 Generated with Claude Code