Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(markdown): treat CJK full-width punctuation as URL boundary (#1630)

Naiyuan Qingc3ae212b4054
diff --git a/packages/ui/markdown/linkify.ts b/packages/ui/markdown/linkify.ts
index 1dd5037840..4db9749e99 100644
--- a/packages/ui/markdown/linkify.ts
+++ b/packages/ui/markdown/linkify.ts
@@ -15,6 +15,15 @@ const linkify = new LinkifyIt()
const FILE_PATH_REGEX =
/(?:^|[\s([{<])((\/|~\/|\.\/)[\w\-./@]+\.(?:ts|tsx|js|jsx|mjs|cjs|md|json|yaml|yml|py|go|rs|css|scss|less|html|htm|txt|log|sh|bash|zsh|swift|kt|java|c|cpp|h|hpp|rb|php|xml|toml|ini|cfg|conf|env|sql|graphql|vue|svelte|astro|prisma|dockerfile|makefile|gitignore))(?=[\s)\]}.,;:!?>]|$)/gi
+// CJK full-width punctuation that should terminate a URL.
+// linkify-it only treats ASCII punctuation as URL boundaries, so in Chinese /
+// Japanese text a URL followed by e.g. "。" gets the punctuation and every
+// character up to the next whitespace swallowed into the href. We truncate the
+// detected URL at the first occurrence of any of these characters. Character
+// set mirrors the fix applied in mattermost/marked#22.
+const CJK_URL_TERMINATOR_REGEX =
+ /[!-/:-@[-`{-~、。「-】]/
+
interface DetectedLink {
type: 'url' | 'email' | 'file'
text: string
@@ -116,23 +125,54 @@ function rangesOverlap(
return a.start < b.end && b.start < a.end
}
+/**
+ * Run linkify-it on `text` and push normalized link records into `out`,
+ * shifted by `offset`. When linkify-it merges multiple URLs into one match
+ * because they are separated only by CJK punctuation (which it doesn't treat
+ * as a URL boundary), we truncate at that punctuation and re-scan the tail.
+ */
+function collectLinkifyMatches(text: string, offset: number, out: DetectedLink[]): void {
+ const matches = linkify.match(text)
+ if (!matches) return
+
+ for (const match of matches) {
+ const cjkIdx = match.text.search(CJK_URL_TERMINATOR_REGEX)
+ if (cjkIdx === 0) continue // match starts with CJK punct — skip
+
+ const truncate = cjkIdx > 0
+ const matchText = truncate ? match.text.slice(0, cjkIdx) : match.text
+ // linkify-it may prepend a scheme (e.g. "http://" or "mailto:") to url
+ // while leaving text as the raw substring. Preserve that prefix.
+ const schemePrefix = match.url.slice(0, match.url.length - match.text.length)
+ const matchUrl = truncate ? schemePrefix + matchText : match.url
+ const matchEnd = truncate ? match.index + cjkIdx : match.lastIndex
+
+ out.push({
+ type: match.schema === 'mailto:' ? 'email' : 'url',
+ text: matchText,
+ url: matchUrl,
+ start: match.index + offset,
+ end: matchEnd + offset
+ })
+
+ if (truncate) {
+ // Rescan the tail after the CJK punct — linkify-it had greedily swallowed
+ // it, so any additional URLs after the punct were never emitted.
+ const tailStart = matchEnd + 1
+ collectLinkifyMatches(text.slice(tailStart), offset + tailStart, out)
+ return
+ }
+ }
+}
+
/**
* Detect all links (URLs, emails, file paths) in text
*/
export function detectLinks(text: string): DetectedLink[] {
const links: DetectedLink[] = []
- // 1. Detect URLs and emails with linkify-it
- const urlMatches = linkify.match(text) || []
- for (const match of urlMatches) {
- links.push({
- type: match.schema === 'mailto:' ? 'email' : 'url',
- text: match.text,
- url: match.url,
- start: match.index,
- end: match.lastIndex
- })
- }
+ // 1. Detect URLs and emails with linkify-it, applying CJK boundary handling.
+ collectLinkifyMatches(text, 0, links)
// 2. Detect file paths with custom regex
// Reset regex state
diff --git a/packages/views/editor/utils/preprocess-links.test.ts b/packages/views/editor/utils/preprocess-links.test.ts
new file mode 100644
index 0000000000..7310d1b209
--- /dev/null
+++ b/packages/views/editor/utils/preprocess-links.test.ts
@@ -0,0 +1,80 @@
+import { describe, expect, it } from "vitest";
+import { preprocessLinks } from "@multica/ui/markdown/linkify";
+
+// The bug: linkify-it does not treat CJK full-width punctuation as a URL
+// boundary, so the href can swallow trailing punctuation and the Chinese
+// characters that follow it (up to the next space). The fix truncates the
+// detected URL at the first CJK full-width punctuation character.
+
+describe("preprocessLinks — CJK punctuation boundary", () => {
+ it("stops URL at ideographic full stop 。", () => {
+ const out = preprocessLinks("见 https://example.com/path。然后继续");
+ expect(out).toBe("见 [https://example.com/path](https://example.com/path)。然后继续");
+ });
+
+ it("stops URL at fullwidth comma ,", () => {
+ const out = preprocessLinks("打开 https://example.com/a,以及其他");
+ expect(out).toBe("打开 [https://example.com/a](https://example.com/a),以及其他");
+ });
+
+ it("stops URL at ideographic comma 、", () => {
+ const out = preprocessLinks("两个地址 https://a.com/x、https://b.com/y");
+ expect(out).toBe(
+ "两个地址 [https://a.com/x](https://a.com/x)、[https://b.com/y](https://b.com/y)",
+ );
+ });
+
+ it("stops URL at fullwidth right paren )", () => {
+ const out = preprocessLinks("(见 https://example.com/x)后文");
+ expect(out).toBe("(见 [https://example.com/x](https://example.com/x))后文");
+ });
+
+ it("stops URL at corner bracket 」", () => {
+ const out = preprocessLinks("「https://example.com/a」后文");
+ expect(out).toBe("「[https://example.com/a](https://example.com/a)」后文");
+ });
+
+ it("stops URL at fullwidth exclamation !", () => {
+ const out = preprocessLinks("太好了 https://example.com/x!继续");
+ expect(out).toBe("太好了 [https://example.com/x](https://example.com/x)!继续");
+ });
+
+ it("handles the original bug report (PR link then 。 then more text)", () => {
+ const out = preprocessLinks(
+ "已合并 PR #1623:https://github.com/multica-ai/multica/pull/1623。merge commit",
+ );
+ expect(out).toBe(
+ "已合并 PR #1623:[https://github.com/multica-ai/multica/pull/1623](https://github.com/multica-ai/multica/pull/1623)。merge commit",
+ );
+ });
+
+ it("does not swallow the entire remainder when there is no trailing space", () => {
+ const out = preprocessLinks("https://github.com/x/y/issues/1619。我接下来把这个");
+ expect(out).toBe(
+ "[https://github.com/x/y/issues/1619](https://github.com/x/y/issues/1619)。我接下来把这个",
+ );
+ });
+
+ it("preserves ASCII trailing period handling (no regression)", () => {
+ const out = preprocessLinks("visit https://example.com/path. next.");
+ expect(out).toBe("visit [https://example.com/path](https://example.com/path). next.");
+ });
+
+ it("preserves plain URL with no trailing punctuation (no regression)", () => {
+ const out = preprocessLinks("go https://example.com/path");
+ expect(out).toBe("go [https://example.com/path](https://example.com/path)");
+ });
+
+ it("preserves CJK letters inside URL path (only trims on punctuation)", () => {
+ const out = preprocessLinks("https://zh.wikipedia.org/wiki/中国 参考");
+ expect(out).toBe(
+ "[https://zh.wikipedia.org/wiki/中国](https://zh.wikipedia.org/wiki/中国) 参考",
+ );
+ });
+
+ it("does not re-link an already-linked URL that contains 。", () => {
+ // If a user or upstream already wrote [text](url。), we leave it alone.
+ const input = "见 [link](https://example.com/x。)后文";
+ expect(preprocessLinks(input)).toBe(input);
+ });
+});