Skip to content

fix(markdown): end a bare URL where the sentence ends, not at the next space - #815

Merged
huichen merged 1 commit into
mainfrom
fix/autolink-boundary
Sep 24, 2026
Merged

huichen merged 1 commit into
mainfrom
fix/autolink-boundary

Conversation

@huichen

@huichen huichen commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes the mobile/desktop rendering of a reply like:

**https://github.com/huichen/futureos-wechat-articles**(PRIVATE,`huichen` 账号下)

which rendered as one link-coloured blob whose target was
https://…articles**(PRIVATE,huichen`` (tapping it 404s), with the ** and the
backticks printed literally.

Root cause

GFM links a bare URL up to the next whitespace and only trims trailing ASCII
punctuation — a rule for Latin prose. In Chinese/Japanese the punctuation comes
straight after the word, so:

  • 见 https://x.com/a。下一句 put 。下一句 inside the target (18 of the 187
    recorded messages with URLs change);
  • **https://x.com/a**(PRIVATE) also ate the closing **. The emphasis could
    then never close, so the asterisks showed up as literal text and everything up
    to the next space was painted as a link.

The swallowing happens in GFM's autolink tokenizer, which runs before inline
syntax is resolved — emphasis markers, code spans and punctuation that follow the
URL become part of the link and never come back.

Change (packages/markdown, shared by desktop, mobile and previews)

  1. The tokenizer (protocolAutolink/wwwAutolink/emailAutolink) is turned off.
    remark-gfm's node-scoped pass stays: it runs after emphasis and link
    resolution, links the same URLs inside one text node, and therefore cannot
    reach outside it. **url**(…) now parses as bold + link + text + code.
  2. That pass only sees text nodes, so the new plugin cuts them at both ends of
    the URL: after it at whitespace or full-width punctuation, and before it where
    GFM is stricter than the tokenizer it replaces (网页https://… has no space
    in Chinese). Registration order matters and is documented — it has to precede
    remark-gfm.

Verification

  • Recorded corpus (187 real messages containing a URL, from agent.db),
    old vs new parse: 18 link targets cleaned, no URL stops being linked, and
    the visible text changes only where literal ** became real emphasis
    (text.replace(/\*\*/g,"") equality).
  • Negative control: with .use(remarkAutolinkBoundary) removed, 6 of the new
    tests fail.
  • mobile: 1906 tests / 125 suites, tsc --noEmit, eslint — green.
  • desktop: 275 markdown tests (incl. a new streaming fixture that pins the
    second processor chain), tsc --noEmit, eslint, stylelint — green.
  • packages/markdown: tsc --noEmit; streaming prefix-equality fixtures extended
    with the reported line, so the streaming projector and the canonical parser
    stay identical at every prefix.

Known limits (no occurrence in ~3000 recorded messages)

  • A bare URL containing inline syntax (a literal backtick, __, <) is no
    longer linked: the old tokenizer shielded those characters, the node-scoped
    pass cannot see them. Markdown links ([x](url)) are unaffected.
  • A URL glued to Chinese without any punctuation (…/herita-project在做的…)
    keeps its glued tail — Han characters are legal URL characters
    (zh.wikipedia.org/wiki/中文), so the tail is not cut at a character boundary.
    This is unchanged from before.

…t space

GFM links a bare URL up to the next whitespace. Chinese prose has no space
before its punctuation, so `见 https://x.com/a。下一句` swallowed the rest of
the sentence into the link target; worse, `**https://x.com/a**(PRIVATE)` also
swallowed the closing `**`, which broke the target and left the emphasis
unclosed — the asterisks rendered literally and the whole sentence was painted
as a link.

Two changes in the shared parser (desktop, mobile and every preview):

- GFM's autolink *tokenizer* is turned off. It runs before inline syntax is
  resolved, so whatever followed the URL — emphasis markers, code spans,
  punctuation — became part of the link and never came back. The node-scoped
  pass remark-gfm keeps runs after emphasis and link resolution, links the same
  URLs inside one text node, and cannot reach outside it.
- That pass sees text nodes, so the plugin cuts them at both ends of the URL:
  after it at whitespace or full-width punctuation, and before it where GFM is
  stricter than the tokenizer it replaces (`网页https://…` is how Chinese is
  written, no space).

Known limit, no occurrence in ~3000 recorded messages: a bare URL containing
inline syntax (a literal backtick, `__`) is no longer linked — the tokenizer
used to shield those characters and the pass cannot see them.

Verified against the recorded corpus: every changed parse cleans a target, no
URL stops being a link, and the visible text changes only where literal `**`
became real emphasis.
@huichen
huichen enabled auto-merge (squash) September 24, 2026 04:04
@huichen
huichen merged commit 80ab76f into main Sep 24, 2026
12 checks passed
@huichen
huichen deleted the fix/autolink-boundary branch September 24, 2026 04:06
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.

1 participant