Skip to content

fix: stop autolink literals at CJK punctuation - #972

Merged
agegr merged 1 commit into
agegr:mainfrom
tonyleungchiuwai:fix/cjk-autolink-literal
Sep 30, 2026
Merged

agegr merged 1 commit into
agegr:mainfrom
tonyleungchiuwai:fix/cjk-autolink-literal

Conversation

@tonyleungchiuwai

Copy link
Copy Markdown

Fixes #971.

Problem

A GFM autolink literal (https://…, www.…) that is immediately followed by CJK prose swallowed that prose into its href, so the link was broken. Chinese/Japanese text does not use spaces, so this is the normal case rather than an edge case:

开 http://localhost:4321,或直接开 library/x.png。

before:

开 <a href="http://localhost:4321%EF%BC%8C%E6%88%96%E7%9B%B4%E6%8E%A5%E5%BC%80">http://localhost:4321,或直接开</a> library/x.png。

after:

开 <a href="http://localhost:4321">http://localhost:4321</a>,或直接开 library/x.png。

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-gfm and splits each autolink literal at its first CJK punctuation mark, turning the trailing prose back into a text node.

Two deliberate choices:

  • Ideographs are not boundaries. Only punctuation splits. A genuine CJK path such as https://zh.wikipedia.org/wiki/中文条目 still works, and so do query strings (?a=1&b=2) and ~/%20 paths.
  • Explicit links are untouched. An autolink literal's raw source is its text, while [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 its http:// and [email protected]:说明 keeps its mailto:.

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 real MarkdownBody render, asserting the emitted href (this is the regression that would have caught the reported bug).

Checks

npm run lint        ✅
npx tsc --noEmit    ✅
npm test            ✅ 1318 passing

@types/mdast is added to devDependencies because lib/markdown.ts now imports mdast types; it was already present transitively, so the lockfile diff is a single line.

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).
@tonyleungchiuwai

Copy link
Copy Markdown
Author

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 singleTilde: false note (#385), so the next person can tell why the workaround is there and when it can be deleted.

CI. GitHub has not run the workflows on this branch yet (first-time contributor, so they need approval). I ran both jobs locally instead:

checks:  npm ci ✅  npm run lint ✅  npx tsc --noEmit ✅  npm test ✅ 1318 passing
e2e:     npm ci ✅  npm run build ✅ (exit 0)  E2E_SERVER_MODE=start npm run test:e2e ✅ 10 PASS

The e2e suite includes a markdown-rendering pass (1280px browser pagination, branch, markdown, code, tool call, and compaction navigation), and it still passes.

Happy to move this upstream into micromark-extension-gfm-autolink-literal instead if you would rather not carry a workaround here — I can follow cmark-gfm#377 and send it there.

@agegr

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Reviewed. #971 reproduces on main: with the current lib/markdown.ts, the three new MarkdownBody tests fail, and http://localhost:4321,或直接开 renders as one link.

What I checked:

  • Only autolink literals are split. Comparing the node's source slice with its text leaves [text](url) alone, including [https://a.com](https://a.com). Links with a title or more than one child are skipped too.
  • Only punctuation is a boundary. CJK paths like wiki/中文条目, query strings, and %20 still work, and the http:// / mailto: prefix on www. and email literals is kept when the url is rewritten.
  • Placement. The transformer runs right after remark-gfm in both the chat and preview plugin lists, which are the only remark-gfm users outside the demo.
  • @types/mdast. It was already installed transitively at 4.0.4, so the lockfile only gains the devDependency line.

Merged locally with current main (clean): tsc --noEmit, eslint, and MarkdownBody.test.mjs + markdown.test.mjs (46/46) pass. LGTM 👍


Generated by Claude Code

@agegr
agegr merged commit 4a5081a into agegr:main Sep 30, 2026
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
…gr#976 agegr#974 agegr#989 agegr#990 agegr#991)

这 8 个提交与我们的分叉没有文本冲突,直接并入:
- agegr#1011 回合答案出现后折叠过程详情
- agegr#1012 移动端打开模型选择器不弹键盘
- agegr#972/agegr#976 行内链接在 CJK 标点处停止、行内公式与货币格式
- agegr#974 首条消息前即报告 system prompt
- agegr#989/agegr#990/agegr#991 子代理:收集标记按 run 计、重启后的中断报告、续跑背景报告标注
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
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).
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autolink literals swallow trailing CJK text and produce broken hrefs

2 participants