mirror of
https://github.com/tenrok/bootstrap.git
synced 2026-05-27 14:46:01 +03:00
a8ab19955b
Co-authored-by: HiDeoo <494699+HiDeoo@users.noreply.github.com> Co-authored-by: Mark Otto <markdotto@gmail.com>
43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import type { MarkdownHeading } from 'astro'
|
|
import { getConfig } from './config'
|
|
|
|
// Generate a tree like structure from a list of headings.
|
|
export function generateToc(allHeadings: MarkdownHeading[]) {
|
|
const headings = allHeadings.filter(
|
|
(heading) => heading.depth >= getConfig().toc.min && heading.depth <= getConfig().toc.max
|
|
)
|
|
|
|
const toc: TocEntry[] = []
|
|
|
|
for (const heading of headings) {
|
|
if (toc.length === 0) {
|
|
toc.push({ ...heading, children: [] })
|
|
continue
|
|
}
|
|
|
|
const previousEntry = toc[toc.length - 1]
|
|
|
|
if (heading.depth === previousEntry.depth) {
|
|
toc.push({ ...heading, children: [] })
|
|
continue
|
|
}
|
|
|
|
const children = getEntryChildrenAtDepth(previousEntry, heading.depth - previousEntry.depth)
|
|
children.push({ ...heading, children: [] })
|
|
}
|
|
|
|
return toc
|
|
}
|
|
|
|
function getEntryChildrenAtDepth(entry: TocEntry, depth: number): TocEntry['children'] {
|
|
if (!entry) {
|
|
return []
|
|
}
|
|
|
|
return depth === 1 ? entry.children : getEntryChildrenAtDepth(entry.children[entry.children.length - 1], depth - 1)
|
|
}
|
|
|
|
export interface TocEntry extends MarkdownHeading {
|
|
children: TocEntry[]
|
|
}
|