-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add heading level hierarchy to table of contents (#2101)
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> Closes #2099
- Loading branch information
Showing
10 changed files
with
139 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { MarkdownHeading } from 'astro'; | ||
export interface TocItem extends MarkdownHeading { | ||
children: TocItem[]; | ||
} | ||
|
||
function diveChildren(item: TocItem, depth: number): TocItem[] { | ||
if (depth === 1) { | ||
return item.children; | ||
} else { | ||
// e.g., 2 | ||
return diveChildren(item.children[item.children.length - 1], depth - 1); | ||
} | ||
} | ||
|
||
export default function generateToc(headings: MarkdownHeading[], title = 'Overview') { | ||
const overview = { depth: 2, slug: 'overview', text: title }; | ||
headings = [overview, ...headings.filter(({ depth }) => depth > 1 && depth < 4)]; | ||
const toc: Array<TocItem> = []; | ||
|
||
for (const heading of headings) { | ||
if (toc.length === 0) { | ||
toc.push({ | ||
...heading, | ||
children: [], | ||
}); | ||
} else { | ||
const lastItemInToc = toc[toc.length - 1]; | ||
if (heading.depth < lastItemInToc.depth) { | ||
console.log(headings); | ||
throw new Error(`Orphan heading found: ${heading.text}.`); | ||
} | ||
if (heading.depth === lastItemInToc.depth) { | ||
// same depth | ||
toc.push({ | ||
...heading, | ||
children: [], | ||
}); | ||
} else { | ||
// higher depth | ||
// push into children, or children' children alike | ||
const gap = heading.depth - lastItemInToc.depth; | ||
const target = diveChildren(lastItemInToc, gap); | ||
target.push({ | ||
...heading, | ||
children: [], | ||
}); | ||
} | ||
} | ||
} | ||
return toc | ||
} |