Skip to content

Commit d70d933

Browse files
committed
fix(web): preserve bare anchor placeholders in markdown
1 parent d8d037e commit d70d933

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎apps/web/src/components/ChatMarkdown.test.tsx‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @vitest-environment jsdom
2+
13
import { EnvironmentId } from "@t3tools/contracts";
24
import { act, type ComponentProps, type ReactNode } from "react";
35
import { renderToStaticMarkup } from "react-dom/server";
@@ -73,6 +75,45 @@ function codeButton(renderer: ReactTestRenderer, label: string) {
7375
return button.props as ComponentProps<typeof Button>;
7476
}
7577

78+
describe("ChatMarkdown bare anchor placeholders", () => {
79+
it.each(["<A>", "<a>", "<a >"])(
80+
"preserves unmatched %s without linking later blocks",
81+
(token) => {
82+
const text = `- **"From ${token}"** appears in the header.\n\n- **Tests:** cover inheritance.\n\nThe deferred move continues on B.\n\nSee <a href="https://example.com">the link</a>.`;
83+
const document = new DOMParser().parseFromString(
84+
renderToStaticMarkup(<ChatMarkdown cwd="/tmp/project" text={text} />),
85+
"text/html",
86+
);
87+
88+
expect(document.querySelector("strong")?.textContent).toBe(`"From ${token}"`);
89+
expect([...document.querySelectorAll("a")].map((link) => link.textContent)).toEqual([
90+
"the link",
91+
]);
92+
expect(document.querySelectorAll("li")).toHaveLength(2);
93+
expect(
94+
[...document.querySelectorAll("p")].map((paragraph) => paragraph.textContent),
95+
).toContain("The deferred move continues on B.");
96+
},
97+
);
98+
99+
it("preserves paired HTML anchors, details, markdown links, and inline code", () => {
100+
const text =
101+
'Bare <a>label</a>, <a id="section"></a>, `<A>`, and [docs](https://example.com).\n\n<details><summary>More</summary>Details</details>';
102+
const document = new DOMParser().parseFromString(
103+
renderToStaticMarkup(<ChatMarkdown cwd="/tmp/project" text={text} />),
104+
"text/html",
105+
);
106+
107+
expect([...document.querySelectorAll("a")].map((link) => link.textContent)).toEqual([
108+
"label",
109+
"",
110+
"docs",
111+
]);
112+
expect(document.querySelector("code")?.textContent).toBe("<A>");
113+
expect(document.querySelector("[data-markdown-details]")?.textContent).toContain("More");
114+
});
115+
});
116+
76117
describe("ChatMarkdown context references", () => {
77118
it("renders text and image references through the chip renderer, with readable fallback", async () => {
78119
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ function orderedListGutterStyle(
394394

395395
type MarkdownImageHastNode = {
396396
type?: string;
397+
value?: string;
397398
tagName?: string;
398399
properties?: Record<string, unknown>;
399400
children?: MarkdownImageHastNode[];
@@ -448,6 +449,28 @@ function markStandaloneImages(node: MarkdownImageHastNode) {
448449
});
449450
}
450451

452+
/** Keep unmatched inline `<A>` placeholders from opening an HTML link over later blocks. */
453+
function rehypePreserveBareAnchorPlaceholders() {
454+
return (tree: MarkdownImageHastNode) => {
455+
const anchors: Array<MarkdownImageHastNode | null> = [];
456+
const visit = (node: MarkdownImageHastNode) => {
457+
if (node.type === "raw" && typeof node.value === "string") {
458+
if (/^<a(?:\s|>)/i.test(node.value)) {
459+
anchors.push(/^<a\s*>$/i.test(node.value) ? node : null);
460+
} else if (/^<\/a\s*>$/i.test(node.value)) {
461+
anchors.pop();
462+
}
463+
}
464+
node.children?.forEach(visit);
465+
};
466+
467+
visit(tree);
468+
for (const anchor of anchors) {
469+
if (anchor) anchor.type = "text";
470+
}
471+
};
472+
}
473+
451474
/** Carries authored image source metadata through the sanitizer to the image renderer. */
452475
function rehypePreserveImageSourceMeta() {
453476
return (tree: MarkdownImageHastNode) => {
@@ -514,6 +537,7 @@ const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
514537
] satisfies NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
515538

516539
const CHAT_MARKDOWN_REHYPE_PLUGINS = [
540+
rehypePreserveBareAnchorPlaceholders,
517541
rehypeRaw,
518542
rehypePreserveImageSourceMeta,
519543
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],

0 commit comments

Comments
 (0)