Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix: address review — do not cut mixed-script IRIs

Chrisc6f5e49f149e
diff --git a/apps/desktop/src/renderer/__tests__/remarkTruncateCjkUrls.test.ts b/apps/desktop/src/renderer/__tests__/remarkTruncateCjkUrls.test.ts
index da7005bc12..be69add5bd 100644
--- a/apps/desktop/src/renderer/__tests__/remarkTruncateCjkUrls.test.ts
+++ b/apps/desktop/src/renderer/__tests__/remarkTruncateCjkUrls.test.ts
@@ -169,24 +169,31 @@ describe('remarkTruncateCjkUrls', () => {
expect(para.children).toHaveLength(1);
});
- it('URL + 直接跟中文文字:https://example.com/path这是说明', () => {
+ it('混合 ASCII/CJK 路径保留:https://example.com/2024年报告', () => {
+ const tree = autolinkTree('https://example.com/2024年报告');
+ transform(tree);
+ const para = tree.children[0] as Paragraph;
+ const link = para.children[0] as Link;
+ expect(link.url).toBe('https://example.com/2024年报告');
+ expect(para.children).toHaveLength(1);
+ });
+
+ it('无标点的汉字跟在 ASCII 词后也保留(IRI,不按脚本猜测)', () => {
const tree = autolinkTree('https://example.com/path这是说明');
transform(tree);
const para = tree.children[0] as Paragraph;
const link = para.children[0] as Link;
- const tail = para.children[1] as Text;
- expect(link.url).toBe('https://example.com/path');
- expect(tail.value).toBe('这是说明');
+ expect(link.url).toBe('https://example.com/path这是说明');
+ expect(para.children).toHaveLength(1);
});
- it('URL + 日文假名:https://example.com/fooこんにちは', () => {
+ it('URL + 日文假名路径保留:https://example.com/fooこんにちは', () => {
const tree = autolinkTree('https://example.com/fooこんにちは');
transform(tree);
const para = tree.children[0] as Paragraph;
const link = para.children[0] as Link;
- const tail = para.children[1] as Text;
- expect(link.url).toBe('https://example.com/foo');
- expect(tail.value).toBe('こんにちは');
+ expect(link.url).toBe('https://example.com/fooこんにちは');
+ expect(para.children).toHaveLength(1);
});
it('URL + 全角感叹号:https://example.com/path!wow', () => {
@@ -215,15 +222,13 @@ describe('remarkTruncateCjkUrls', () => {
expect(tail.value).toBe('**(完整');
});
- it('Markdown 装饰残留仍剥掉 trailing _ 和 ~', () => {
+ it('无标点的 _~ 加汉字视为路径,不按脚本切断', () => {
const tree = autolinkTree('https://github.com/makecindy/cindy/pull/90_~说明');
transform(tree);
const para = tree.children[0] as Paragraph;
const link = para.children[0] as Link;
- const tail = para.children[1] as Text;
- expect(link.url).toBe('https://github.com/makecindy/cindy/pull/90');
- expect((link.children[0] as Text).value).toBe('https://github.com/makecindy/cindy/pull/90');
- expect(tail.value).toBe('_~说明');
+ expect(link.url).toBe('https://github.com/makecindy/cindy/pull/90_~说明');
+ expect(para.children).toHaveLength(1);
});
it('ASCII prose 截断时保留 URL 尾部合法 markdown 字符', () => {
@@ -237,15 +242,13 @@ describe('remarkTruncateCjkUrls', () => {
expect(tail.value).toBe('(draft');
});
- it('ASCII prose 截断先于 CJK boundary 时保留 URL 尾部合法 markdown 字符', () => {
+ it('配对括号内的汉字保留:https://example.com/Foo_(draft说明)', () => {
const tree = autolinkTree('https://example.com/Foo_(draft说明)');
transform(tree);
const para = tree.children[0] as Paragraph;
const link = para.children[0] as Link;
- const tail = para.children[1] as Text;
- expect(link.url).toBe('https://example.com/Foo_');
- expect((link.children[0] as Text).value).toBe('https://example.com/Foo_');
- expect(tail.value).toBe('(draft说明)');
+ expect(link.url).toBe('https://example.com/Foo_(draft说明)');
+ expect(para.children).toHaveLength(1);
});
it('ASCII prose 截断时保留 URL 尾部合法多字符 markdown 字符', () => {
@@ -259,24 +262,22 @@ describe('remarkTruncateCjkUrls', () => {
expect(tail.value).toBe('(draft');
});
- it('URL 尾部半角句点 + 中文:https://x.com/foo.说明 → 剥掉 .', () => {
+ it('半角句点后的汉字保留在路径里:https://x.com/foo.说明', () => {
const tree = autolinkTree('https://x.com/foo.说明');
transform(tree);
const para = tree.children[0] as Paragraph;
const link = para.children[0] as Link;
- const tail = para.children[1] as Text;
- expect(link.url).toBe('https://x.com/foo');
- expect(tail.value).toBe('.说明');
+ expect(link.url).toBe('https://x.com/foo.说明');
+ expect(para.children).toHaveLength(1);
});
- it('URL 尾部半角分号 + 中文:https://x.com/foo;说明 → 剥掉 ;', () => {
+ it('半角分号后的汉字保留在路径里:https://x.com/foo;说明', () => {
const tree = autolinkTree('https://x.com/foo;说明');
transform(tree);
const para = tree.children[0] as Paragraph;
const link = para.children[0] as Link;
- const tail = para.children[1] as Text;
- expect(link.url).toBe('https://x.com/foo');
- expect(tail.value).toBe(';说明');
+ expect(link.url).toBe('https://x.com/foo;说明');
+ expect(para.children).toHaveLength(1);
});
it('未配对的 ) 被剥掉:https://x.com/foo)(说明)', () => {
@@ -660,15 +661,12 @@ describe('remarkTruncateCjkUrls', () => {
expect(tail.value).toBe("'");
});
- it("有 opening apostrophe 且 URL 后接 CJK 时也剥掉包裹 apostrophe", () => {
+ it("opening apostrophe 后的 path'汉字 视为 IRI,不按脚本切断", () => {
const tree = apostropheWrappedAutolinkTree("https://example.com/path'说明");
transform(tree);
const para = tree.children[0] as Paragraph;
const link = para.children[1] as Link;
- const tail = para.children[2] as Text;
- expect(link.url).toBe('https://example.com/path');
- expect((link.children[0] as Text).value).toBe('https://example.com/path');
- expect(tail.value).toBe("'说明");
+ expect(link.url).toBe("https://example.com/path'说明");
});
it('纯 ASCII autolink 也剥掉尾部分号:https://x.com/foo; next', () => {
diff --git a/apps/desktop/src/renderer/__tests__/userMessageLinkify.test.ts b/apps/desktop/src/renderer/__tests__/userMessageLinkify.test.ts
index 77b59fe747..f384656ea8 100644
--- a/apps/desktop/src/renderer/__tests__/userMessageLinkify.test.ts
+++ b/apps/desktop/src/renderer/__tests__/userMessageLinkify.test.ts
@@ -83,9 +83,9 @@ describe('userMessageLinkify', () => {
]);
});
- it('stops URL matches before ASCII quotes and CJK text', () => {
+ it('stops URL matches before ASCII quotes and CJK punctuation', () => {
expect(urls('看 "https://example.com/path"然后')).toEqual(['https://example.com/path']);
- expect(urls('看 https://example.com/path这是说明')).toEqual(['https://example.com/path']);
+ expect(urls('看 https://example.com/path。然后')).toEqual(['https://example.com/path']);
});
it('keeps Unicode domain and path segments', () => {
@@ -93,6 +93,9 @@ describe('userMessageLinkify', () => {
'https://example.com/路径',
'https://例子.测试/path',
]);
+ expect(urls('打开 https://www.例子.com/2024年报告')).toEqual([
+ 'https://www.例子.com/2024年报告',
+ ]);
});
it('keeps apostrophes inside URL paths and at URL endings', () => {
diff --git a/packages/maker-shared/src/__tests__/urlTextBoundary.test.ts b/packages/maker-shared/src/__tests__/urlTextBoundary.test.ts
index e41f8bc427..769ff2a529 100644
--- a/packages/maker-shared/src/__tests__/urlTextBoundary.test.ts
+++ b/packages/maker-shared/src/__tests__/urlTextBoundary.test.ts
@@ -45,27 +45,25 @@ describe('clipBareHttpAutolinkText', () => {
);
});
- it('keeps Unicode domain and path, but still cuts glued CJK prose', () => {
+ it('keeps mixed ASCII/CJK domains and paths that URL can parse', () => {
expect(clipBareHttpAutolinkText('https://example.com/路径')).toBe(
'https://example.com/路径',
);
expect(clipBareHttpAutolinkText('https://例子.测试/path')).toBe(
'https://例子.测试/path',
);
- expect(clipBareHttpAutolinkText('https://example.com/中文')).toBe(
- 'https://example.com/中文',
+ expect(clipBareHttpAutolinkText('https://www.例子.com/path')).toBe(
+ 'https://www.例子.com/path',
);
- expect(clipBareHttpAutolinkText('https://example.com/path这是说明')).toBe(
- 'https://example.com/path',
+ expect(clipBareHttpAutolinkText('https://example.com/2024年报告')).toBe(
+ 'https://example.com/2024年报告',
);
- expect(clipBareHttpAutolinkText('https://x.com/foo.说明')).toBe(
- 'https://x.com/foo',
+ expect(clipBareHttpAutolinkText('https://example.com/abc中文')).toBe(
+ 'https://example.com/abc中文',
+ );
+ expect(clipBareHttpAutolinkText('https://例子123.测试/path')).toBe(
+ 'https://例子123.测试/path',
);
- expect(
- clipBareHttpAutolinkText('https://github.com/makecindy/cindy/pull/90_~说明', {
- stripMarkdownFormattingPunct: 'auto',
- }),
- ).toBe('https://github.com/makecindy/cindy/pull/90');
});
});
diff --git a/packages/maker-shared/src/urlTextBoundary.ts b/packages/maker-shared/src/urlTextBoundary.ts
index dad67b5225..3631167bb3 100644
--- a/packages/maker-shared/src/urlTextBoundary.ts
+++ b/packages/maker-shared/src/urlTextBoundary.ts
@@ -362,38 +362,17 @@ function isAutolinkPunctuationBoundary(ch: string): boolean {
return false;
}
-function isUrlLetterScript(ch: string): boolean {
- const code = ch.codePointAt(0);
- if (code == null) return false;
- if (code >= 0x3040 && code <= 0x30ff) return true;
- if (code >= 0x3400 && code <= 0x4dbf) return true;
- if (code >= 0x4e00 && code <= 0x9fff) return true;
- if (code >= 0xac00 && code <= 0xd7af) return true;
- return false;
-}
-
/**
- * 标点/全角符号永远是正文边界。
- * 汉字假名谚文:跟在 URL 结构符后面算地址(`/路径`、`例子.测试`),
- * 紧贴 ASCII 字母数字则是粘上去的说明(`path这是说明`)。
+ * 只有标点/全角符号是正文边界。汉字假名谚文一律算地址,
+ * 不按「前一个字符是不是 ASCII」猜测(`www.例子.com`、`/2024年报告`
+ * 都是浏览器能打开的 IRI)。无标点的 `path这是说明` 无法和真路径区分,不猜。
*/
-function isAsciiTrailPunct(ch: string | undefined): boolean {
- return ch != null && ".,;:?!_~'*" .includes(ch);
-}
-
-function isGluedCjkProse(raw: string, index: number): boolean {
- let cursor = index - 1;
- while (cursor >= 0 && isAsciiTrailPunct(raw[cursor])) cursor -= 1;
- return cursor >= 0 && isAsciiAlnum(raw[cursor]);
-}
-
function findAutolinkProseBoundary(raw: string): number {
for (let index = 0; index < raw.length; ) {
const code = raw.codePointAt(index);
if (code == null) break;
const char = String.fromCodePoint(code);
if (isAutolinkPunctuationBoundary(char)) return index;
- if (isUrlLetterScript(char) && isGluedCjkProse(raw, index)) return index;
index += char.length;
}
return raw.length;