diff --git a/src/components/blog/BlogContent.astro b/src/components/blog/BlogContent.astro index 832e314..ef85956 100644 --- a/src/components/blog/BlogContent.astro +++ b/src/components/blog/BlogContent.astro @@ -9,6 +9,8 @@ import TagSmallList from './tag/TagSmallList.astro' import AuthorIcon from './icon/AuthorIcon.astro' import { formatDate } from '@/utils/format-date' import '@/styles/remark-link-card.css' +import TableOfContents from './TableOfContents.astro' + interface Props { blog: CollectionEntry<'blogs'> } @@ -20,7 +22,15 @@ const { data: blogMeta } = (await getEntry({ })) ?? { data: { postDate: new Date(), updateDate: undefined } } const tags = await getEntries(blog.data.tags) const author = await getEntry(blog.data.author) -const { Content } = await render(blog) +const { Content, headings } = await render(blog) + +const hasH1 = headings.some((heading) => heading.depth === 1) +const tocHeadings = hasH1 + ? headings.filter((heading) => heading.depth <= 2) + : headings.filter((heading) => heading.depth <= 3).map((heading) => ({ + ...heading, + depth: heading.depth - 1, + })) ---
@@ -62,6 +72,7 @@ const { Content } = await render(blog)
+
diff --git a/src/components/blog/TableOfContents.astro b/src/components/blog/TableOfContents.astro new file mode 100644 index 0000000..6410274 --- /dev/null +++ b/src/components/blog/TableOfContents.astro @@ -0,0 +1,23 @@ +--- +interface Heading { + depth: number + slug: string + text: string +} + +interface Props { + headings: Heading[] +} + +const { headings } = Astro.props +--- + +