fix: keep typed link source literal when a space follows it - #242
Merged
Conversation
Milkdown's text handler returns any value ending in whitespace without escaping it, so typed link and autolink source reached the file as live syntax. Holding the trailing whitespace out of `safe` keeps it from being encoded as a character reference at a line ending.
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
Typed link source was written to the file as live Markdown whenever a space followed it, so text the editor deliberately kept literal became a link on the next open. The same source ending the paragraph was escaped. Which of the two states won depended on a trailing space.
The cause is Milkdown's own
textstringify handler, installed throughremarkStringifyOptionsCtx:Any value ending in whitespace and free of
*,_, and\is returned without escaping. This is not specific to links — it disables escaping for the whole value, so#,>,`, and|at a position that needs escaping were affected the same way.mdast-util-to-markdownescapes both forms correctly on its own; serializing the same mdast directly confirms it.Leafdown now installs its own handler. It escapes the value up to its trailing whitespace through
state.safeand appends that whitespace raw, which keeps Milkdown's reason for the bypass —safeencodes a space before a line ending as , and an ordinary typed trailing space should not produce that.Typed source now serializes identically in both positions:
[test link](./test.html)\[test link]\(./test.html)[test link](./test.html)\[test link]\(./test.html)https://example.comhttps\://example.comhttps://example.comhttps\://example.com<https://example.com>\<https\://example.com><https://example.com>\<https\://example.com>Related Issue
Closes #234
Verification
Focused tests in
markdownCompatibility.test.tsxassert the serialized Markdown for the inline link, autolink literal, and URI autolink, typed at the end of a paragraph and typed with a space after it. A reload test asserts that the escaped inline-link output parses back to literal text with no anchor, in both positions. A further test asserts an ordinary trailing space is still written as itself rather than , which is the behavior Milkdown's bypass existed to protect.pnpm check:frontendpasses. The backend is untouched.Not verified: manual Tauri verification was not performed. The change is serialization, exercised through the real editor mount in the automated tests.
Notes
#234's second
Done whenbullet is met in full. The first is met except for one clause, and that clause cannot be met inside #234's own scope.The issue's
On reloadcolumn is wrong for the two autolink rows.https\://example.com— the escaped output the editor already produced at a61106a — reloads as a link, with or without a trailing space, and\<https\://example.com>reloads as a literal<, a link, and a literal>.mdast-util-gfm-autolink-literalrunstransformGfmAutolinkLiterals, afindAndReplaceover mdast text values after parsing, once escapes and character references are already decoded;https\://example.comrelinks too. No source escaping survives that pass.So "the reloaded document matches what the editor presented before the save" was written believing autolinks already round-tripped as literal text. Reaching it needs either recognition while typing or a parser change, and #234's
Out of scopesection rules out both. The clause is unsatisfiable within the issue that states it, so it is tracked under #238 rather than held against this one.What #234 names as its subject, "the two typed paths disagreeing with each other", is delivered for all three forms.
Two consequences worth review:
" leading and trailing "serializes as leading and trailing. The bypass previously left it raw only when the value also ended in whitespace, which was already inconsistent with the same value without a trailing space. A raw leading space is dropped on reload, so this is a correction, but it changes bytes written for a paragraph that starts with a space.<https://example.com>now writes\<https\://example.com>rather than the valid URI autolink it happened to produce before. That is the point of the fix, since the editor showed literal text, but it changes saved output for that sequence.The handler's type is derived through
remarkStringifyOptionsCtx._typeInfobecauseremark-stringifyis not resolvable fromsrc/under pnpm's strict layout. A change to that slice's shape surfaces as a type error rather than silent breakage.