Skip to content

Commit 0dafc8b

Browse files
fix: ensure unique TOC slugs for repeated headings(Examples) (#4593)
Co-authored-by: Sourya07 <singhsourya137@gmail.com> Co-authored-by: Sambhav Gupta <81870866+sambhavgupta0705@users.noreply.github.com>
1 parent a056c20 commit 0dafc8b

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

components/TOC.tsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface ITOCProps {
1111
lvl: number;
1212
content: string;
1313
slug: string;
14+
seen?: number;
1415
}[];
1516
contentSelector?: string;
1617
depth?: number;
@@ -29,6 +30,7 @@ export default function TOC({ className, cssBreakingPoint = 'xl', toc, contentSe
2930

3031
if (!toc || !toc.length) return null;
3132
const minLevel = toc.reduce((mLevel, item) => (!mLevel || item.lvl < mLevel ? item.lvl : mLevel), 0);
33+
3234
const tocItems = toc
3335
.filter((item) => item.lvl <= minLevel + depth)
3436
.map((item) => ({
@@ -38,30 +40,40 @@ export default function TOC({ className, cssBreakingPoint = 'xl', toc, contentSe
3840
// markdown document MDX takes these "a" tags and uses them to render the "id" for headers like
3941
// a-namedefinitionsapplicationaapplication slugWithATag contains transformed heading name that is later used
4042
// for scroll spy identification
41-
slugWithATag: item.content
42-
.replace(/[<>?!:`'."\\/=@#$%^&*()[\]{}+,;]/gi, '')
43-
.replace(/\s/gi, '-')
44-
.toLowerCase()
43+
slugWithATag: (() => {
44+
const base = item.content
45+
.replace(/[<>?!:`'."\\/=@#$%^&*()[\]{}+,;]/gi, '')
46+
.replace(/\s/gi, '-')
47+
.toLowerCase();
48+
49+
if (typeof item.seen === 'number' && item.seen > 0) {
50+
return `${base}-${item.seen}`;
51+
}
52+
53+
return base;
54+
})()
4555
}));
4656

4757
return (
4858
<div
49-
className={twMerge(`${className} ${tocItems.length ? '' : 'hidden'}
50-
${cssBreakingPoint === 'xl' ? 'xl:block' : 'lg:block'} md:top-24 md:max-h-(screen-14) mb-4 z-20`)}
59+
className={twMerge(
60+
`${className} ${tocItems.length ? '' : 'hidden'}
61+
${cssBreakingPoint === 'xl' ? 'xl:block' : 'lg:block'} md:top-24 md:max-h-(screen-14) mb-4 z-20`
62+
)}
5163
onClick={() => setOpen(!open)}
5264
>
5365
<div
5466
className={`flex cursor-pointer ${tocItems.length ? '' : 'hidden'}
5567
${cssBreakingPoint === 'xl' ? 'xl:cursor-auto' : 'lg:cursor-auto'} xl:mt-2`}
5668
>
5769
<h5
58-
className={twMerge(`${open && 'mb-4'} flex-1 text-primary-500 font-medium uppercase tracking-wide
59-
text-sm font-sans antialiased ${
60-
cssBreakingPoint === 'xl'
61-
? `xl:mb-4 xl:text-xs xl:text-gray-900
62-
xl:font-bold`
63-
: 'lg:mb-4 lg:text-xs lg:text-gray-900 lg:font-bold'
64-
}`)}
70+
className={twMerge(
71+
`${open && 'mb-4'} flex-1 text-primary-500 font-medium uppercase tracking-wide text-sm font-sans antialiased ${
72+
cssBreakingPoint === 'xl'
73+
? 'xl:mb-4 xl:text-xs xl:text-gray-900 xl:font-bold'
74+
: 'lg:mb-4 lg:text-xs lg:text-gray-900 lg:font-bold'
75+
}`
76+
)}
6577
data-testid='TOC-Heading'
6678
>
6779
On this page

0 commit comments

Comments
 (0)