Repository navigation
fix: stop autolink literals at CJK punctuation - #972
Conversation
A GFM autolink literal that is immediately followed by CJK prose swallowed that prose into its href, because the scanner only stops at whitespace and its trailing-punctuation trim only knows ASCII punctuation. Chinese and Japanese text does not use spaces, so `开 http://localhost:4321,或直接开 library/x.png。` produced a link whose href was `http://localhost:4321,或直接开` and clicking it went nowhere. Split the literal at its first CJK punctuation mark instead, turning the trailing prose back into a text node. Ideographs are deliberately not boundaries, so a genuine CJK path such as /wiki/中文条目 keeps working, and an explicit [text](url) link is left alone (its raw source is not its text).
cc721f9 to
d8bc047
Compare
|
Two notes for review. Upstream context (I looked before assuming this was a remark-gfm bug). remark-gfm only terminates autolinks on ASCII punctuation because that is what GitHub does, and that was already raised and closed as not planned for this reason:
So this PR deliberately does not claim remark-gfm is wrong. It is an application-layer workaround until cmark-gfm#377 lands: a remark transformer that splits the literal at the first CJK punctuation. I left that reasoning in the code comment next to the existing CI. GitHub has not run the workflows on this branch yet (first-time contributor, so they need approval). I ran both jobs locally instead: The e2e suite includes a markdown-rendering pass ( Happy to move this upstream into |
|
Reviewed. #971 reproduces on What I checked:
Merged locally with current Generated by Claude Code |
A GFM autolink literal that is immediately followed by CJK prose swallowed that prose into its href, because the scanner only stops at whitespace and its trailing-punctuation trim only knows ASCII punctuation. Chinese and Japanese text does not use spaces, so `开 http://localhost:4321,或直接开 library/x.png。` produced a link whose href was `http://localhost:4321,或直接开` and clicking it went nowhere. Split the literal at its first CJK punctuation mark instead, turning the trailing prose back into a text node. Ideographs are deliberately not boundaries, so a genuine CJK path such as /wiki/中文条目 keeps working, and an explicit [text](url) link is left alone (its raw source is not its text).
A GFM autolink literal that is immediately followed by CJK prose swallowed that prose into its href, because the scanner only stops at whitespace and its trailing-punctuation trim only knows ASCII punctuation. Chinese and Japanese text does not use spaces, so `开 http://localhost:4321,或直接开 library/x.png。` produced a link whose href was `http://localhost:4321,或直接开` and clicking it went nowhere. Split the literal at its first CJK punctuation mark instead, turning the trailing prose back into a text node. Ideographs are deliberately not boundaries, so a genuine CJK path such as /wiki/中文条目 keeps working, and an explicit [text](url) link is left alone (its raw source is not its text).
Fixes #971.
Problem
A GFM autolink literal (
https://…,www.…) that is immediately followed by CJK prose swallowed that prose into itshref, so the link was broken. Chinese/Japanese text does not use spaces, so this is the normal case rather than an edge case:before:
after:
The autolink scanner only stops at whitespace, and its trailing-punctuation trim only knows ASCII punctuation, so CJK punctuation stayed inside the URL — and everything after it did too.
Fix
A small remark transformer that runs right after
remark-gfmand splits each autolink literal at its first CJK punctuation mark, turning the trailing prose back into a text node.Two deliberate choices:
https://zh.wikipedia.org/wiki/中文条目still works, and so do query strings (?a=1&b=2) and~/%20paths.[text](url)'s raw source is[text](url); comparing the two against the file source is what keeps hand-written links safe even when their text happens to equal their url (which[https://a.com](https://a.com)does).The transform also rewrites the URL prefix correctly, so
www.example.com,后面keeps itshttp://and[email protected]:说明keeps itsmailto:.Tests
lib/markdown.test.mjs— the transform against hand-built trees: split + url rewrite,www./mailto:prefixes, trailing。, CJK paths left alone, query strings, explicit links, punctuation at index 0, missing position, and links nested in other blocks.components/MarkdownBody.test.mjs— end-to-end through the realMarkdownBodyrender, asserting the emittedhref(this is the regression that would have caught the reported bug).Checks
@types/mdastis added todevDependenciesbecauselib/markdown.tsnow imports mdast types; it was already present transitively, so the lockfile diff is a single line.