Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,15 @@ export const Layout = ({

pageHeadings.forEach((node) => {
const { innerText, id, localName } = node as HTMLElement;
if (innerText && id && (localName == 'h2' || localName == 'h3')) {
if (innerText && id && localName == 'h2') {
headings.push({
linkText: innerText,
hash: id,
level: localName,
subheadings: []
});
} else if (innerText && id && localName == 'h3') {
headings[headings.length - 1].subheadings.push({
linkText: innerText,
hash: id,
level: localName
Expand Down
64 changes: 51 additions & 13 deletions src/components/TableOfContents/TableOfContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface HeadingInterface {
linkText: string;
hash: string;
level: string;
subheadings: Array<object>;
}
interface TableOfContents {
headers?: HeadingInterface[];
Expand All @@ -21,20 +22,57 @@ export const TableOfContents = ({ headers }) => {
)}
<View as="ul" className="toc-list">
{headers.map(({ linkText, hash, level }, index) => {
return (
<View
as="li"
className={`toc-item toc-item--${level}`}
key={`toc-${index}`}
>
<Link
href={`#${hash}`}
className={`toc-item__link toc-item__link--${level}`}
if (headers[index].subheadings?.length === 0) {
return (
<View
as="li"
className={`toc-item toc-item--${level}`}
key={`toc-${index}`}
>
{linkText}
</Link>
</View>
);
<Link
href={`#${hash}`}
className={`toc-item__link toc-item__link--${level}`}
>
{linkText}
</Link>
</View>
);
} else {
return (
<View
as="li"
className={`toc-item toc-item--${level}`}
key={`toc-${index}`}
>
<Link
href={`#${hash}`}
className={`toc-item__link toc-item__link--${level}`}
>
{linkText}
</Link>
<View as="ul" className="toc-list">
{headers[index].subheadings?.map(
({ linkText, hash, level }, index) => {
return (
<View
as="li"
className={`toc-item toc-item--${level}`}
key={`toc-${index}`}
>
<Link
href={`#${hash}`}
className={`toc-item__link toc-item__link--${level}`}
>
{linkText}
</Link>
</View>
);
}
)}
</View>
</View>
);
}
})}
</View>
</Flex>
Expand Down