From 74c2e8fa47e363775646d103933b99c9cdf84822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20P=C4=99dzich?= Date: Wed, 26 Apr 2023 10:23:10 +0200 Subject: [PATCH] Create a custom Table Of Contents component --- src/components/TableOfContents.astro | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/components/TableOfContents.astro diff --git a/src/components/TableOfContents.astro b/src/components/TableOfContents.astro new file mode 100644 index 0000000..f24d5a9 --- /dev/null +++ b/src/components/TableOfContents.astro @@ -0,0 +1,59 @@ +--- +import type { MarkdownHeading } from 'astro'; + +type Props = { + headings: MarkdownHeading[]; +}; + +type HeadingWithSubheadings = MarkdownHeading & { + subheadings: MarkdownHeading[]; +}; + +const { headings } = Astro.props; + +const grouppedHeadings = headings.reduce((array, heading) => { + if (heading.depth === 2) { + array.push({ ...heading, subheadings: [] }); + } else if (heading.depth === 3) { + array.at(-1)?.subheadings.push(heading); + } + + return array; +}, [] as HeadingWithSubheadings[]); +--- + + + +