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 — IDN sentence tail and mobile brackets

Chriseb94fee13670
diff --git a/apps/mobile/src/__tests__/messageMarkdown.test.ts b/apps/mobile/src/__tests__/messageMarkdown.test.ts
index 09ca1995a4..a8fa289b38 100644
--- a/apps/mobile/src/__tests__/messageMarkdown.test.ts
+++ b/apps/mobile/src/__tests__/messageMarkdown.test.ts
@@ -295,6 +295,20 @@ describe('messageMarkdown', () => {
url: 'https://example.com/~alice',
},
]);
+ expect(parseMobileMarkdownInlines('打开 https://example.com/api/[id] 看')).toEqual([
+ { type: 'text', text: '打开 ' },
+ {
+ type: 'link',
+ text: 'https://example.com/api/[id]',
+ url: 'https://example.com/api/[id]',
+ },
+ { type: 'text', text: ' 看' },
+ ]);
+ expect(parseMobileMarkdownInlines('看 https://例子。测试。这是说明')).toEqual([
+ { type: 'text', text: '看 ' },
+ { type: 'link', text: 'https://例子。测试', url: 'https://例子。测试' },
+ { type: 'text', text: '。这是说明' },
+ ]);
expect(parseMobileMarkdownInlines('看 https://例子。测试。')).toEqual([
{ type: 'text', text: '看 ' },
{ type: 'link', text: 'https://例子。测试', url: 'https://例子。测试' },
diff --git a/apps/mobile/src/session/messageMarkdown.ts b/apps/mobile/src/session/messageMarkdown.ts
index d9b20e0239..120dbe9ae2 100644
--- a/apps/mobile/src/session/messageMarkdown.ts
+++ b/apps/mobile/src/session/messageMarkdown.ts
@@ -902,6 +902,7 @@ function findNextInlineToken(
markdownWrapMarker: mobileMarkdownWrapMarker(
input.slice(0, match.index),
),
+ cutPathBrackets: false,
})
: trimUrlPunctuation(rawHref);
return { type: 'link' as const, text: href, url: href };
diff --git a/packages/maker-shared/src/__tests__/urlTextBoundary.test.ts b/packages/maker-shared/src/__tests__/urlTextBoundary.test.ts
index 4d894bf92e..d9a96b4434 100644
--- a/packages/maker-shared/src/__tests__/urlTextBoundary.test.ts
+++ b/packages/maker-shared/src/__tests__/urlTextBoundary.test.ts
@@ -97,12 +97,21 @@ describe('clipBareHttpAutolinkText', () => {
expect(clipBareHttpAutolinkText('https://例子。测试。')).toBe(
'https://例子。测试',
);
+ expect(clipBareHttpAutolinkText('https://例子。测试。这是说明')).toBe(
+ 'https://例子。测试',
+ );
expect(clipBareHttpAutolinkText('https://example.com/path・说明')).toBe(
'https://example.com/path',
);
expect(clipBareHttpAutolinkText('https://example.com/path—说明')).toBe(
'https://example.com/path',
);
+ expect(
+ clipBareHttpAutolinkText('https://example.com/api/[id]', { cutPathBrackets: false }),
+ ).toBe('https://example.com/api/[id]');
+ expect(clipBareHttpAutolinkText('https://example.com/api/[id]')).toBe(
+ 'https://example.com/api/',
+ );
});
});
diff --git a/packages/maker-shared/src/urlTextBoundary.ts b/packages/maker-shared/src/urlTextBoundary.ts
index b4d184b621..eca3341738 100644
--- a/packages/maker-shared/src/urlTextBoundary.ts
+++ b/packages/maker-shared/src/urlTextBoundary.ts
@@ -375,7 +375,7 @@ function firstAuthDelimiterIndex(text: string, from = 0): number {
}
/** IDN 点号只在 hostname 内保留;端口和 IPv6 `]` 之后仍是正文边界。 */
-function hostnameEndIndex(raw: string): number {
+function hostnameRange(raw: string): { start: number; end: number } {
const scheme = raw.match(/^https?:\/\//i);
let start = scheme ? scheme[0].length : 0;
const authEnd = firstAuthDelimiterIndex(raw, start);
@@ -383,12 +383,12 @@ function hostnameEndIndex(raw: string): number {
if (at >= start) start = at + 1;
if (raw[start] === '[') {
const close = raw.indexOf(']', start + 1);
- return close >= 0 && close < authEnd ? close + 1 : authEnd;
+ return { start, end: close >= 0 && close < authEnd ? close + 1 : authEnd };
}
for (let index = start; index < authEnd; index += 1) {
- if (raw[index] === ':') return index;
+ if (raw[index] === ':') return { start, end: index };
}
- return authEnd;
+ return { start, end: authEnd };
}
function isHostLabelChar(ch: string | undefined): boolean {
@@ -397,27 +397,39 @@ function isHostLabelChar(ch: string | undefined): boolean {
return !isAutolinkPunctuationBoundary(ch);
}
-function isIdnDotInHostname(raw: string, index: number, hostnameEnd: number): boolean {
- return (
- index < hostnameEnd &&
- isIdnDomainDot(raw[index] ?? '') &&
- isHostLabelChar(raw[index + 1])
- );
+function hasEarlierIdnDot(raw: string, hostStart: number, index: number): boolean {
+ for (let cursor = hostStart; cursor < index; cursor += 1) {
+ if (isIdnDomainDot(raw[cursor] ?? '')) return true;
+ }
+ return false;
+}
+
+function isIdnDotInHostname(
+ raw: string,
+ index: number,
+ hostStart: number,
+ hostEnd: number,
+): boolean {
+ if (index < hostStart || index >= hostEnd) return false;
+ if (!isIdnDomainDot(raw[index] ?? '') || !isHostLabelChar(raw[index + 1])) return false;
+ if (!hasEarlierIdnDot(raw, hostStart, index)) return true;
+ // 已经有过 IDN 点号后,再出现的 `。这是说明` 当句号;后面还有 path/port 才继续当域名。
+ return hostEnd < raw.length;
}
/**
- * 标点/全角符号是正文边界;hostname 里的 IDN 点号(`例子。测试`)留下。
- * 端口、IPv6 `]`、路径之后的 `。` 仍当句号切掉。
+ * 标点/全角符号是正文边界;hostname 里第一处 IDN 点号(`例子。测试`)留下。
+ * 端口、IPv6 `]`、路径、以及第二个句末 `。` 之后仍当句号切掉。
*/
function findAutolinkProseBoundary(raw: string): number {
- const hostnameEnd = hostnameEndIndex(raw);
+ const { start: hostStart, end: hostEnd } = hostnameRange(raw);
for (let index = 0; index < raw.length; ) {
const code = raw.codePointAt(index);
if (code == null) break;
const char = String.fromCodePoint(code);
if (
isAutolinkPunctuationBoundary(char) &&
- !isIdnDotInHostname(raw, index, hostnameEnd)
+ !isIdnDotInHostname(raw, index, hostStart, hostEnd)
) {
return index;
}
@@ -441,6 +453,8 @@ export type ClipBareHttpAutolinkOptions = {
prefix?: string | null;
markdownWrapMarker?: string | null;
stripMarkdownFormattingPunct?: boolean | 'auto';
+ /** Desktop GFM 会把 path 里的 `[]`/`{}` 当说明;Mobile 原匹配器会保留。 */
+ cutPathBrackets?: boolean;
};
/**
@@ -464,7 +478,7 @@ export function clipBareHttpAutolink(
const proseCut = Math.min(
limited,
cutBeforeUnbalancedParenProse(raw, limited, { wrappingParenCount }),
- cutBeforePathBracketProse(raw),
+ options.cutPathBrackets === false ? raw.length : cutBeforePathBracketProse(raw),
);
const stripMarkdown =
options.stripMarkdownFormattingPunct === true ||