Fix: escaped links/images/autolinks lose backslash in Markdoc.format (fixes #590)#609
Open
shivamtiwari3 wants to merge 3 commits into
Open
Conversation
…arkdoc#590) Root cause: the `text` case in `formatNode` wrote `content` verbatim, so a text node whose string happens to be `[a](url)` (produced by parsing `\[a](url)`) was emitted as `[a](url)` — which the parser re-reads as a link, breaking the round-trip. Fix: before the existing per-character escaping, apply three targeted guards in the `text` case: 1. `startsWithLink(content, offset)` — a short scanner that follows CommonMark's balanced-parenthesis rule — escapes `[` only when it genuinely opens an inline link `[text](url)`. Patterns with unbalanced parens (already treated as plain text by the parser) are left untouched. 2. `/<scheme:\/\/...>/` regex escapes `<` that would be re-parsed as an autolink. 3. `nextSibling` tracking in `formatChildren` lets the formatter escape a trailing `!` when it is immediately followed by a link node, preventing the sequence from being re-read as an image.
Author
|
Hi — happy to make any further changes if helpful. Just checking if this is still under consideration. |
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.
Summary
Fixes #590.
Markdoc.formatwas emitting text nodes verbatim, so a text node whose content happened to be[a](url)(produced by parsing\[a](url)) was written back as[a](url)— which the parser re-reads as a link. The original escaping was silently dropped on every format round-trip.Root Cause
In
formatter.ts, the'text'case offormatNode(previously line 214) passedcontentdirectly toescapeMarkdownCharacters. That helper only re-escaped characters that could form headings, blockquotes, or emphasis — it had no knowledge of inline-link or autolink syntax.Three patterns broke:
\[a](url)[a](url)[a](url)\!\[a](url)\<https://example.com><https://example.com><https://example.com>Solution
Three targeted guards are applied to
contentbefore the existing per-character escaping:startsWithLink(content, offset)— a small scanner that follows CommonMark's balanced-parenthesis rule for link destinations. It escapes[only when it genuinely opens an inline link[text](url). Patterns such as— which the parser already treats as plain text — are left untouched./<scheme:\/\/...>/regex — escapes<that would be re-parsed as a scheme-based autolink (e.g.<https://example.com>).nextSiblingtracking —formatChildrennow forwards the next sibling into each child's options. When atextnode ends with!and its next sibling is alinknode (the AST shape produced by\), the trailing!is escaped to\!, preventing the sequence from being re-read as an image.Testing
'preserves escaping on links, images, and autolinks (issue #590)'insrc/formatter.test.ts:\[a](url)round-trips stably (formatter re-emits\[)\<https://example.com>round-trips stably (formatter re-emits\<)[brackets](no URL) gains no spurious backslash<not-an-autolink>gains no spurious backslash\!\[a](url)re-parses to the same text-node content ()npm testChecklist
npm run lint— no errors)