Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(markdown): preserve wrapped HTTP link destinations

vastsa76d2b8b948df
diff --git a/apps/desktop/src/components/Markdown.tsx b/apps/desktop/src/components/Markdown.tsx
index 4a163c811..d21389b67 100644
--- a/apps/desktop/src/components/Markdown.tsx
+++ b/apps/desktop/src/components/Markdown.tsx
@@ -56,6 +56,7 @@ import {
import { useAppStore } from "../stores/app-store";
import { useReferencedImageDataUrl } from "../lib/use-referenced-image-data-url";
import { absoluteImagePath, remarkLocalImagePaths } from "../lib/markdown-image-paths";
+import { remarkNormalizeWrappedMarkdownLinkDestinations } from "../lib/markdown-link-destinations";
import { useOpenChatFileRef } from "../hooks/use-preview-target";
import {
useChatFileMenuItems,
@@ -786,7 +787,11 @@ const markdownComponents: Components = {
// The grammar the block splitter parses with, plus the renderer-only rewrite
// of local image paths. `remarkLocalImagePaths` transforms URLs and moves no
// block boundary, so the splitter has no reason to run it.
-const staticRemarkPlugins = [...markdownRemarkPlugins, remarkLocalImagePaths];
+const staticRemarkPlugins = [
+ ...markdownRemarkPlugins,
+ remarkNormalizeWrappedMarkdownLinkDestinations,
+ remarkLocalImagePaths,
+];
// Extend the default schema only for the media elements rendered above, plus
// `remark-math`'s math classes on `<code>`: the default `language-*` allow list
diff --git a/apps/desktop/src/lib/markdown-link-destinations.ts b/apps/desktop/src/lib/markdown-link-destinations.ts
new file mode 100644
index 000000000..602cb144c
--- /dev/null
+++ b/apps/desktop/src/lib/markdown-link-destinations.ts
@@ -0,0 +1,35 @@
+type MarkdownNode = {
+ type?: unknown;
+ url?: unknown;
+ children?: unknown;
+};
+
+const WRAPPED_HTTP_DESTINATION = /^\(\[[^\]\r\n]+\]\((https?:\/\/\S+)\)\)$/i;
+
+function normalizeWrappedHttpDestination(url: string): string | null {
+ return WRAPPED_HTTP_DESTINATION.exec(url)?.[1] ?? null;
+}
+
+function normalizeLinks(node: unknown): void {
+ if (Array.isArray(node)) {
+ for (const child of node) normalizeLinks(child);
+ return;
+ }
+ if (node === null || typeof node !== "object") return;
+
+ const current = node as MarkdownNode;
+ if (current.type === "link" && typeof current.url === "string") {
+ const normalized = normalizeWrappedHttpDestination(current.url);
+ if (normalized) current.url = normalized;
+ }
+ normalizeLinks(current.children);
+}
+
+/**
+ * Repair a nested linked-host wrapper in an HTTP(S) Markdown destination
+ * before rehype-sanitize validates the URL. Other link destinations are kept
+ * unchanged, so this cannot enable additional protocols.
+ */
+export function remarkNormalizeWrappedMarkdownLinkDestinations() {
+ return (tree: unknown) => normalizeLinks(tree);
+}
diff --git a/apps/desktop/test/markdown-link-destinations.test.mjs b/apps/desktop/test/markdown-link-destinations.test.mjs
new file mode 100644
index 000000000..aa18d12b7
--- /dev/null
+++ b/apps/desktop/test/markdown-link-destinations.test.mjs
@@ -0,0 +1,65 @@
+import assert from "node:assert/strict";
+import { readFile } from "node:fs/promises";
+import remarkGfm from "remark-gfm";
+import remarkParse from "remark-parse";
+import test from "node:test";
+import { unified } from "unified";
+import { remarkNormalizeWrappedMarkdownLinkDestinations } from "../src/lib/markdown-link-destinations.ts";
+
+const markdownSource = await readFile(
+ new URL("../src/components/Markdown.tsx", import.meta.url),
+ "utf8",
+);
+
+function collectLinks(node, links = []) {
+ if (Array.isArray(node)) {
+ for (const child of node) collectLinks(child, links);
+ } else if (node && typeof node === "object") {
+ if (node.type === "link") links.push(node.url);
+ collectLinks(node.children, links);
+ }
+ return links;
+}
+
+const processor = unified()
+ .use(remarkParse)
+ .use(remarkGfm)
+ .use(remarkNormalizeWrappedMarkdownLinkDestinations);
+
+test("wrapped linked-host HTTP destinations become clickable HTTP links", () => {
+ const source = [
+ "[#1106](([github.com](https://github.com/vastsa/PI-Desktop/issues/1106)))",
+ "[#1107](([github.com](https://github.com/vastsa/PI-Desktop/issues/1107)))",
+ ].join("\n");
+ const tree = processor.runSync(processor.parse(source));
+
+ assert.deepEqual(collectLinks(tree), [
+ "https://github.com/vastsa/PI-Desktop/issues/1106",
+ "https://github.com/vastsa/PI-Desktop/issues/1107",
+ ]);
+});
+
+test("ordinary links and non-HTTP destinations are not rewritten", () => {
+ const tree = {
+ type: "root",
+ children: [
+ { type: "link", url: "https://example.com/issue", children: [] },
+ { type: "link", url: "([site](javascript:alert(1)))", children: [] },
+ { type: "link", url: "([site](file:///tmp/issue))", children: [] },
+ ],
+ };
+ processor.runSync(tree);
+
+ assert.deepEqual(collectLinks(tree), [
+ "https://example.com/issue",
+ "([site](javascript:alert(1)))",
+ "([site](file:///tmp/issue))",
+ ]);
+});
+
+test("Markdown installs the normalizer before the rehype sanitizer", () => {
+ assert.match(
+ markdownSource,
+ /staticRemarkPlugins\s*=\s*\[[\s\S]*?remarkNormalizeWrappedMarkdownLinkDestinations/,
+ );
+});
diff --git a/apps/desktop/test/markdown-math-rendering.test.mjs b/apps/desktop/test/markdown-math-rendering.test.mjs
index 1183b264b..f102c0d4b 100644
--- a/apps/desktop/test/markdown-math-rendering.test.mjs
+++ b/apps/desktop/test/markdown-math-rendering.test.mjs
@@ -15,7 +15,7 @@ import { fileURLToPath } from "node:url";
* block splitting, `remark-math`, the TeX bracket normalizer, `rehype-raw`,
* `rehype-sanitize` and `rehype-katex`.
*/
-test("TeX bracket math renders through the production Markdown pipeline", async () => {
+test("TeX math and wrapped HTTP links render through the production Markdown pipeline", async () => {
const server = await createServer({
root: fileURLToPath(new URL("..", import.meta.url)),
configFile: false,
@@ -48,6 +48,17 @@ test("TeX bracket math renders through the production Markdown pipeline", async
const inline = (html) =>
html.includes('class="katex"') && !html.includes("katex-display");
+ // A nested linked-host destination must be normalized before the
+ // production rehype sanitizer removes its invalid wrapper href.
+ const wrappedLink = render(
+ "[#1106](([github.com](https://github.com/vastsa/PI-Desktop/issues/1106)))",
+ );
+ assert.match(
+ wrappedLink,
+ /<a[^>]*href="https:\/\/github\.com\/vastsa\/PI-Desktop\/issues\/1106"/,
+ );
+ assert.match(wrappedLink, /target="_blank"/);
+
// The issue's exact replies: a display formula on its own lines.
const issueMultiline = render("\\[\nD \\leftrightarrow H\n\\]");
assert.match(issueMultiline, /katex-display/);
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 43c006aa4..6640f2d08 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -12043,7 +12043,13 @@ are withdrawn with ADR 0165.
Open in default browser, Open in work panel, and Copy link address. Repeat
the menu actions with keyboard focus and Arrow/Home/End navigation.
4. Repeat a link click with Ctrl/Cmd, Shift, and Alt held.
+ 5. Render and use links whose HTTP(S) destination is wrapped as a nested
+ linked-host Markdown destination, such as
+ `[#1106](([github.com](https://github.com/vastsa/PI-Desktop/issues/1106)))`.
- **Expected**:
+ - Wrapped linked-host destinations resolve to their inner HTTP(S) URL before
+ sanitization, so the link is clickable and its right-click menu offers both
+ browser destinations and Copy link address.
- The Work panel browser is the default plain-click destination.
- The Default OS browser setting routes chat, transcript, and plugin HTTP(S)
clicks through the main-owned external opener, including markdown links,
@@ -12065,7 +12071,9 @@ are withdrawn with ADR 0165.
- **Acceptance**: B (settings), C (conversation & stream), Security, Quality
- **Milestone**: M5
- **Status**: Unit-covered (`apps/desktop/test/markdown-link-menu.test.mjs`,
- locale catalog tests, `apps/desktop/test/open-http-url.test.mjs`); full UI
+ `apps/desktop/test/markdown-link-destinations.test.mjs`,
+ `apps/desktop/test/markdown-math-rendering.test.mjs`, locale catalog tests,
+ `apps/desktop/test/open-http-url.test.mjs`); full UI
journey Draft (run only in a capable environment when this surface changes)
#### E2E-201: Alias a configured model and copy a model id