修复 Markdown 链接边界误截 Unicode 与显式目标

This commit is contained in:
pptg 2026-09-12 04:11:20 +08:00
parent cb01e5a39c
commit 2b1b3d5970
3 changed files with 52 additions and 12 deletions

View File

@ -358,3 +358,34 @@ test("R4:代码示例中的协议样例保留、空行不压缩、全角括号
'行内示例 `<!-- ppanel:{"protocol_version":1,"event_id":"example","kind":"decision","task_id":"demo"} -->` 必须保留。',
);
});
test("Unicode 和显式链接目标完整保留,真实 Issue 中文说明仅在裸链接处分开", async ({ page }) => {
const issue = 'https://gitea.d-robotics.cc/peng01.guo/ppanel/issues/34';
const cases = [
['https://example.com/中文路径', 'https://example.com/中文路径'],
['https://例子.测试/路径', 'https://例子.测试/路径'],
['[https://example.com/中文路径](https://example.com/中文路径)', 'https://example.com/中文路径'],
['https://example.com/%E4%B8%AD', 'https://example.com/%E4%B8%AD'],
[`[${issue}(根任务](${issue}(根任务)`, `${issue}(根任务`],
[`[${issue}(根任务](https://example.com/actual)`, 'https://example.com/actual'],
['https://example.com/合法(路径)', 'https://example.com/合法(路径)'],
[`${issue}(根任务 #29,PR #31)`, issue],
];
// 明确的 API 契约替身,加入正常 URL 边界输入;不访问这些外部链接。
const body = cases.map(([source]) => source).join('\n\n');
await page.route('**/api/tasks/taskA', async route => {
const response = await route.fetch();
const detail = await response.json();
detail.events = [{ id: 'unicode-links', taskId: 'taskA', kind: 'result', role: null,
body, createdAt: new Date().toISOString(), url: null }];
await route.fulfill({ response, json: detail });
});
await page.goto('/#/tasks/taskA');
const event = page.locator('.task-event').filter({ hasText: '例子.测试' });
const links = event.locator('.assistant-markdown a');
await expect(links).toHaveCount(cases.length);
const targets = await links.evaluateAll(nodes => nodes.map(node => (node as HTMLAnchorElement).href));
expect(targets).toEqual(cases.map(([, target]) => new URL(target!).href));
expect(await event.locator('.task-event-raw pre').textContent()).toBe(body);
await expect(event).toContainText('(根任务 #29,PR #31)');
});

View File

@ -52,3 +52,15 @@ it("renders GFM tables and code blocks as text", () => {
expect(html).toContain("<table>");
expect(html).toContain('<code class="language-bash">git status');
});
it.each([
['https://example.com/中文路径', 'https://example.com/%E4%B8%AD%E6%96%87%E8%B7%AF%E5%BE%84'],
['https://例子.测试/路径', 'https://%E4%BE%8B%E5%AD%90.%E6%B5%8B%E8%AF%95/%E8%B7%AF%E5%BE%84'],
['[https://example.com/中文路径](https://example.com/中文路径)', 'https://example.com/%E4%B8%AD%E6%96%87%E8%B7%AF%E5%BE%84'],
['https://example.com/%E4%B8%AD', 'https://example.com/%E4%B8%AD'],
['[https://example.com/issues/34(根任务](https://example.com/actual)', 'https://example.com/actual'],
['https://example.com/合法(路径)', 'https://example.com/%E5%90%88%E6%B3%95%EF%BC%88%E8%B7%AF%E5%BE%84%EF%BC%89'],
])('preserves the target of %s', (text, href) => {
const html = renderToStaticMarkup(<AssistantMarkdown text={text} />);
expect(html).toContain(`href="${href}"`);
});

View File

@ -3,18 +3,12 @@ import Markdown from "react-markdown";
import remarkGfm from "remark-gfm";
import "./markdown.css";
/**
* GFM 自动链接会把紧邻 URL 的全角标点(如中文括号“(”)吞进链接文本与目标:
* 真实事件 …/issues/34(根任务 曾被链接成 …/issues/34%EF%BC%88…(Issue #18 R4-F3,
* 证据 dfd4c0ab evidence/issue-18-r4/bad-links.json)。这里只对“文本本身是裸 URL”
* 的自动链接做边界修正:截到首个非 ASCII 可打印字符,其后文字回到正文;
* 手写 [文本](URL) 链接与纯 ASCII URL 不受影响。
/** 仅修正真实 Issue 记录中紧邻编号的“(根任务”说明(#18 R4-F3)。
* 是否为裸链接由 Markdown 源码位置确认,不从显示文本猜测显式链接目标。
*/
function fixAutolinkText(text: string): { url: string; rest: string } | null {
if (!/^https?:\/\//i.test(text)) return null;
const cut = text.search(/[^\x21-\x7E]/);
if (cut <= 0) return null;
return { url: text.slice(0, cut), rest: text.slice(cut) };
const match = /^(https?:\/\/[^/\s]+\/[^/\s]+\/[^/\s]+\/issues\/\d+)((根任务.*)$/i.exec(text);
return match ? { url: match[1]!, rest: match[2]! } : null;
}
/** 不启用 raw HTML;图片不自动加载,但保留 http(s) 地址供用户手动打开。 */
@ -25,9 +19,12 @@ export const AssistantMarkdown = memo(function AssistantMarkdown({ text }: { tex
remarkPlugins={[remarkGfm]}
urlTransform={(url) => (/^https?:\/\//i.test(url) ? url : "")}
components={{
a: ({ href, children }) => {
a: ({ href, children, node }) => {
if (href && typeof children === "string") {
const fixed = fixAutolinkText(children);
const start = node?.position?.start.offset;
const end = node?.position?.end.offset;
const isBare = start !== undefined && end !== undefined && text.slice(start, end) === children;
const fixed = isBare ? fixAutolinkText(children) : null;
if (fixed) {
return (
<>