Compare commits

...
1 Commits
Author SHA1 Message Date
Mohamed Boudra 24f71ac10c perf(app): skip the Markdown parse for replies that only link outside the workspace
Opening a chat parsed every assistant reply that contained any Markdown link into a full
mdast tree, because a link into the workspace gets a hover title. A reply whose links all
point elsewhere produces no edit, so the parse and its garbage were wasted. Parse only when
the source contains the workspace root after a link, or a backslash escape that could spell
it. Character references already take the parse path through the `&` check.
2026-11-29 17:33:35 +01:00
2 changed files with 34 additions and 1 deletions
@@ -688,6 +688,32 @@ it("keeps workspace-relative hover titles on explicit absolute file links", () =
);
});
it.each([
[
"an angle-bracket destination",
"[Open](</repo/src/file.ts>)",
'[Open](</repo/src/file.ts> "src/file.ts")',
],
["an escaped slash", "[Open](\\/repo/src/file.ts)", '[Open](\\/repo/src/file.ts "src/file.ts")'],
[
"an escaped separator",
"[Open](/repo\\/src/file.ts)",
'[Open](/repo\\/src/file.ts "src/file.ts")',
],
[
"a character reference",
"[Open](&#x2F;repo/src/file.ts)",
'[Open](&#x2F;repo/src/file.ts "src/file.ts")',
],
])("keeps the workspace hover title on a link written with %s", (_form, source, rendered) => {
expect(prepareMarkdownSource(source, "/repo").render()).toBe(rendered);
});
it("leaves links outside the workspace as written", () => {
const source = "[Docs](https://example.com/repo/x) and [Other](/repository/x) near /repo/x";
expect(prepareMarkdownSource(source, "/repo").render()).toBe(source);
});
it.each(['""', "''", "()"])("preserves an authored empty link title: %s", (title) => {
const source = `[Open](/repo/file.ts ${title})`;
expect(prepareMarkdownSource(source, "/repo").render()).toBe(source);
@@ -150,6 +150,13 @@ function workspaceHoverTitle(link: Link, markup: string, workspaceRoot?: string)
return link.url.slice(workspaceRoot.length + 1);
}
// Only a link into the workspace gets an edit, its hover title. A link's URL differs from its
// source text only by backslash escapes and character references, and `&` already takes the parse.
function mayLinkIntoWorkspace(source: string, workspaceRoot?: string): boolean {
if (!workspaceRoot || !source.includes("](")) return false;
return source.includes(`${workspaceRoot}/`) || source.includes("\\");
}
/** Preserve source spelling; adapt workspace images, spoiler delimiters, and entity text. */
export function prepareMarkdownSource(
source: string,
@@ -191,7 +198,7 @@ export function prepareMarkdownSource(
!/^[\t >]*[^\t >\r\n][^\r\n]*(?:\r\n|\r|\n)[^\r\n]+(?:\r\n|\r|\n)(?=[\t >:|-]*\|)[\t >|]*:?-[-\t :|]*$/m.test(
source,
) &&
!(workspaceRoot && source.includes("](")) &&
!mayLinkIntoWorkspace(source, workspaceRoot) &&
!(external && /\]|<[a-z]+:/i.test(source))
) {
return { images, ...metadata, render: () => source };