diff options
| author | Dawid Rycerz <dawid@rycerz.xyz> | 2025-07-03 19:23:17 +0300 |
|---|---|---|
| committer | Dawid Rycerz <dawid@rycerz.xyz> | 2025-07-03 19:23:17 +0300 |
| commit | 32f719a3de8969d674f609869d406a0a91f6d1f0 (patch) | |
| tree | 501e4ec08e1a2e595ade652e6be48245da294669 /src/utils/generateToc.ts | |
| parent | d015147a6c25e2be49f21d6e73a9b309f15daf9d (diff) | |
Remove TOC
Diffstat (limited to 'src/utils/generateToc.ts')
| -rw-r--r-- | src/utils/generateToc.ts | 37 |
1 files changed, 0 insertions, 37 deletions
diff --git a/src/utils/generateToc.ts b/src/utils/generateToc.ts deleted file mode 100644 index f63f0bf..0000000 --- a/src/utils/generateToc.ts +++ /dev/null @@ -1,37 +0,0 @@ -// Heavy inspiration from starlight: https://github.com/withastro/starlight/blob/main/packages/starlight/utils/generateToC.ts -import type { MarkdownHeading } from "astro"; - -export interface TocItem extends MarkdownHeading { - children: TocItem[]; -} - -interface TocOpts { - maxHeadingLevel?: number | undefined; - minHeadingLevel?: number | undefined; -} - -/** Inject a ToC entry as deep in the tree as its `depth` property requires. */ -function injectChild(items: TocItem[], item: TocItem): void { - const lastItem = items.at(-1); - if (!lastItem || lastItem.depth >= item.depth) { - items.push(item); - } else { - injectChild(lastItem.children, item); - return; - } -} - -export function generateToc( - headings: ReadonlyArray<MarkdownHeading>, - { maxHeadingLevel = 4, minHeadingLevel = 2 }: TocOpts = {}, -) { - // by default this ignores/filters out h1 and h5 heading(s) - const bodyHeadings = headings.filter( - ({ depth }) => depth >= minHeadingLevel && depth <= maxHeadingLevel, - ); - const toc: Array<TocItem> = []; - - for (const heading of bodyHeadings) injectChild(toc, { ...heading, children: [] }); - - return toc; -} |
