Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(ui): end literal autolinks at CJK text and punctuation (#2233)
Call White49bc9b7f052d
diff --git a/ui/src/components/ui/markdown.test.tsx b/ui/src/components/ui/markdown.test.tsxindex 6700dac4d..1ccaa4085 100644--- a/ui/src/components/ui/markdown.test.tsx+++ b/ui/src/components/ui/markdown.test.tsx@@ -15,6 +15,51 @@ import { Markdown } from './markdown';afterEach(cleanup);+// GFM ends a bare URL only at whitespace, and CJK text puts none after it.+// A literal autolink must stop where CJK text or non-ASCII punctuation begins,+// with every character still rendered exactly once.+describe('Markdown literal autolinks next to CJK text', () => {+ it.each([+ [+ 'PR #2232(https://github.com/avibe-bot/avibe/pull/2232),在等 Codex',+ ['https://github.com/avibe-bot/avibe/pull/2232'],+ ],+ ['打开 https://example.com/a这个页面', ['https://example.com/a']],+ ['(https://example.com/a),然后', ['https://example.com/a']],+ ['见 https://example.com/a.。', ['https://example.com/a']],+ ['“https://example.com/x”', ['https://example.com/x']],+ ['看 https://example.com/x✅', ['https://example.com/x']],+ ['见 www.example.com。', ['http://www.example.com']],+ [+ 'https://a.example.com/1,https://b.example.com/2。',+ ['https://a.example.com/1', 'https://b.example.com/2'],+ ],+ ['https://a.example.com/1,看https://b.example.com/2', ['https://a.example.com/1', 'https://b.example.com/2']],+ // An ASCII comma stays inside the URL, as GFM parses it before whitespace.+ ['https://a.example/1,https://b.example/2。', ['https://a.example/1,https://b.example/2']],+ ['http://localhost:3000/setup中文', ['http://localhost:3000/setup']],+ ])('links %j as %j', (content, hrefs) => {+ const { container } = render(<Markdown content={content} />);+ const anchors = [...container.querySelectorAll('a')];+ expect(anchors.map((a) => a.getAttribute('href'))).toEqual(hrefs);+ expect(anchors.map((a) => a.textContent)).toEqual(+ hrefs.map((href) => href.replace(/^http:\/\/(?=www\.)/, '')),+ );+ expect(container.textContent).toBe(content);+ });++ it.each([+ ['[文档](https://example.com/中文)', 'https://example.com/%E4%B8%AD%E6%96%87'],+ ['<https://example.com/中文>', 'https://example.com/%E4%B8%AD%E6%96%87'],+ ['https://example.com/a. 然后', 'https://example.com/a'],+ // A CJK host leaves no valid literal before the boundary, so GFM's link stays.+ ['https://中文.example.com', 'https://%E4%B8%AD%E6%96%87.example.com'],+ ])('leaves %j as GFM parses it', (content, href) => {+ const { container } = render(<Markdown content={content} />);+ expect([...container.querySelectorAll('a')].map((a) => a.getAttribute('href'))).toEqual([href]);+ });+});+// A character reference is the other way Markdown writes a character the// reader must see, and every surface has to agree on which spellings are// references and what each one stands for. This renderer is the referencediff --git a/ui/src/components/ui/markdown.tsx b/ui/src/components/ui/markdown.tsxindex 83c09ef6e..fcfe9fe0b 100644--- a/ui/src/components/ui/markdown.tsx+++ b/ui/src/components/ui/markdown.tsx@@ -218,6 +218,120 @@ function remarkLiteralAuthority() {};}+// GFM ends a literal autolink (a bare `https://…` / `www.…`) only at whitespace+// and trims only ASCII trailing punctuation, matching GitHub (remarkjs/remark-gfm#83,+// github/cmark-gfm#377). CJK text puts no space after a URL, so+// `(https://x/pull/1),在等` linked everything up to the next space. CJK script+// and any non-ASCII punctuation, symbol, or space end the URL here, and GFM's+// trailing-punctuation trim runs again on what is left. Only where a literal+// ends changes: explicit `[text](url)` and `<url>` links keep their destination,+// so a URL that really contains CJK can still be written either way.+const AUTOLINK_BOUNDARY =+ /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]|(?!\p{ASCII})[\p{P}\p{S}\p{Z}]/u;+// Where another literal may start inside a swallowed run, by GFM's rule on the+// character before it: `http(s)://` after anything but an ASCII letter, `www.`+// only after punctuation or a symbol (a run holds no whitespace).+const AUTOLINK_START = /(?<![a-z])https?:\/\/|(?<=[\p{P}\p{S}])www\./giu;++type AutolinkNode = {+ type: string;+ value?: string;+ url?: string;+ title?: null;+ children?: AutolinkNode[];+ position?: { start?: { offset?: number } };+};++// GFM's trailing-punctuation rule (mdast-util-gfm-autolink-literal `splitUrl`):+// drop trailing ASCII punctuation, keeping a `)` that closes an open `(`.+function trimAutolinkTrail(url: string): string {+ const trail = /[!"&'),.:;<>?\]}]+$/.exec(url);+ if (!trail) return url;+ let kept = url.slice(0, trail.index);+ let rest = trail[0];+ const opening = kept.split('(').length - 1;+ let closing = kept.split(')').length - 1;+ let close = rest.indexOf(')');+ while (close !== -1 && opening > closing) {+ kept += rest.slice(0, close + 1);+ rest = rest.slice(close + 1);+ close = rest.indexOf(')');+ closing++;+ }+ return kept;+}++// GFM links a literal whose domain's last two labels each hold a letter or digit+// and no underscore, so `http://localhost:3000` links and a bare `https://` does not.+function isAutolinkLiteral(literal: string): boolean {+ const domain = /^[-.\w]*/.exec(literal.replace(/^https?:\/\//i, ''))?.[0] ?? '';+ return domain+ .split('.')+ .slice(-2)+ .every((label) => /[a-z\d]/i.test(label) && !label.includes('_'));+}++// Re-split the run GFM linked as one literal into the links and text it spells+// once a boundary ends each URL, or null to keep GFM's link as it is.+function splitAutolinkRun(run: string): AutolinkNode[] | null {+ if (!AUTOLINK_BOUNDARY.test(run)) return null;+ const nodes: AutolinkNode[] = [];+ let textStart = 0;+ let linkStart = 0;+ for (;;) {+ const tail = run.slice(linkStart);+ const boundary = tail.search(AUTOLINK_BOUNDARY);+ const literal = trimAutolinkTrail(boundary < 0 ? tail : tail.slice(0, boundary));+ let resume = linkStart + 1;+ if (isAutolinkLiteral(literal)) {+ if (linkStart > textStart) nodes.push({ type: 'text', value: run.slice(textStart, linkStart) });+ const url = (/^www\./i.test(literal) ? 'http://' : '') + literal;+ nodes.push({ type: 'link', url, title: null, children: [{ type: 'text', value: literal }] });+ textStart = resume = linkStart + literal.length;+ } else if (linkStart === 0) {+ // The cut left GFM's own literal invalid (a CJK host): keep GFM's link, as+ // this pass only moves where a literal ends and never removes one.+ return null;+ }+ AUTOLINK_START.lastIndex = resume;+ const next = AUTOLINK_START.exec(run);+ if (!next) break;+ linkStart = next.index;+ }+ if (textStart < run.length) nodes.push({ type: 'text', value: run.slice(textStart) });+ return nodes;+}++function remarkCjkAutolinkBoundary() {+ return (tree: unknown, file: { value?: unknown }) => {+ const source = typeof file.value === 'string' ? file.value : '';+ const visit = (parent: AutolinkNode) => {+ const children = parent.children;+ if (!children) return;+ for (let index = 0; index < children.length; index++) {+ const node = children[index];+ const label = node.children?.length === 1 ? node.children[0] : undefined;+ const run = label?.type === 'text' ? label.value : undefined;+ const offset = node.position?.start?.offset;+ // A literal autolink: its label is its url (GFM prepends `http://` to+ // `www.`), and it was not written as `[text](url)` or `<url>`.+ const isLiteral = node.type === 'link'+ && run !== undefined+ && (node.url === run || node.url === `http://${run}`)+ && (offset === undefined || (source[offset] !== '[' && source[offset] !== '<'));+ const pieces = isLiteral ? splitAutolinkRun(run) : null;+ if (pieces) {+ children.splice(index, 1, ...pieces);+ index += pieces.length - 1;+ } else {+ visit(node);+ }+ }+ };+ visit(tree as AutolinkNode);+ };+}+// Keep react-markdown's URL sanitizer from stripping our custom schemes (it allows// only http/https/mailto/tel/relative by default).function mentionUrlTransform(@@ -410,6 +524,7 @@ export const Markdown: React.FC<{() => [remarkGfm,remarkCjkFriendly,+ remarkCjkAutolinkBoundary,// Unconditional: which destinations were written with a bracketed host is// a fact about this text, not about whether it carries citations.remarkLiteralAuthority,