Skip to content

Commit ba5a8a5

Browse files
fix(web): markdown headings never share an id
`Setup`, `Setup`, `Setup-1` gave the second and third headings the same `setup-1` id, so a link to the third scrolled to the second. Ids now skip any id already in the document, authored or assigned. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent b96304e commit ba5a8a5

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,30 @@ describe("ChatMarkdown Windows file links", () => {
10351035
});
10361036
});
10371037

1038+
describe("ChatMarkdown heading ids", () => {
1039+
it("never gives two headings the same id, even when a suffix matches another heading", () => {
1040+
const html = renderToStaticMarkup(
1041+
<ChatMarkdown
1042+
cwd="/tmp/project"
1043+
parseRawHtml
1044+
text={
1045+
'## Setup\n\n## Setup\n\n## Setup-1\n\n<h2 id="install-1">Pinned</h2>\n\n## Install\n\n## Install'
1046+
}
1047+
/>,
1048+
);
1049+
const ids = [...html.matchAll(/<h2 id="([^"]+)"/g)].map((match) => match[1]);
1050+
expect(ids).toEqual([
1051+
"user-content-setup",
1052+
"user-content-setup-1",
1053+
"user-content-setup-1-1",
1054+
"user-content-install-1",
1055+
"user-content-install",
1056+
"user-content-install-2",
1057+
]);
1058+
expect(new Set(ids).size).toBe(ids.length);
1059+
});
1060+
});
1061+
10381062
describe("ChatMarkdown in-page links", () => {
10391063
it.each([true, false])(
10401064
"scrolls a table-of-contents link to its heading without touching the URL (parseRawHtml=%s)",

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,14 +588,28 @@ function githubHeadingSlug(text: string): string {
588588
*/
589589
function rehypeHeadingIds() {
590590
return (tree: MarkdownImageHastNode) => {
591-
const seen = new Map<string, number>();
591+
// Every id already in the document, authored or assigned, so a suffix never
592+
// lands on one that exists: `Setup`, `Setup`, `Setup-1` get three distinct ids.
593+
const taken = new Set<string>();
594+
const collect = (node: MarkdownImageHastNode) => {
595+
const id = node.properties?.id;
596+
if (typeof id === "string") taken.add(id);
597+
node.children?.forEach(collect);
598+
};
599+
collect(tree);
600+
const nextSuffix = new Map<string, number>();
592601
const visit = (node: MarkdownImageHastNode) => {
593602
if (node.type === "element" && node.tagName && /^h[1-6]$/.test(node.tagName)) {
594603
const slug = githubHeadingSlug(hastPlainTextDeep(node));
595604
if (node.properties?.id === undefined && slug) {
596-
const count = seen.get(slug) ?? 0;
597-
seen.set(slug, count + 1);
598-
const id = `${SANITIZED_FRAGMENT_PREFIX}${count === 0 ? slug : `${slug}-${count}`}`;
605+
let count = nextSuffix.get(slug) ?? 0;
606+
let id = `${SANITIZED_FRAGMENT_PREFIX}${slug}`;
607+
while (taken.has(id)) {
608+
count += 1;
609+
id = `${SANITIZED_FRAGMENT_PREFIX}${slug}-${count}`;
610+
}
611+
nextSuffix.set(slug, count);
612+
taken.add(id);
599613
node.properties = { ...node.properties, id };
600614
}
601615
return;

0 commit comments

Comments
 (0)