From 3bf0200e7efb79bd47c3d5c3a174a5b6154316f6 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Fri, 1 Sep 2023 12:38:59 -0500 Subject: [PATCH] wip toc --- src/components/TocWrapper.astro | 41 +++++++++++ src/content/config.ts | 1 + .../index.md | 1 + src/pages/posts/[...slug].astro | 73 ++++++++++--------- src/styles/global.scss | 6 +- src/styles/post.scss | 11 +++ src/styles/toc.scss | 44 +++++++++++ 7 files changed, 138 insertions(+), 39 deletions(-) create mode 100644 src/components/TocWrapper.astro create mode 100644 src/styles/toc.scss diff --git a/src/components/TocWrapper.astro b/src/components/TocWrapper.astro new file mode 100644 index 0000000..fa292c0 --- /dev/null +++ b/src/components/TocWrapper.astro @@ -0,0 +1,41 @@ +--- +import type { MarkdownHeading } from "astro"; +import "../styles/toc.scss"; + +interface Props { + toc: boolean; + headings: MarkdownHeading[]; +} + +const { toc, headings } = Astro.props; +--- + +{ + toc ? ( + <> +
+ +
+ Table of contents +
    + {headings.map((heading) => { + return ( +
  • + {heading.text} +
  • + ); + })} +
+
+
+ + ) : ( + + ) +} + + diff --git a/src/content/config.ts b/src/content/config.ts index 9563b52..3afe8c9 100644 --- a/src/content/config.ts +++ b/src/content/config.ts @@ -27,6 +27,7 @@ const posts = defineCollection({ tags: z.array(z.string()), draft: z.boolean().default(false), math: z.boolean().default(false), + toc: z.boolean().default(false), }), }); diff --git a/src/content/posts/2023-09-01-formal-cek-machine-in-agda/index.md b/src/content/posts/2023-09-01-formal-cek-machine-in-agda/index.md index 14ff529..e33076d 100644 --- a/src/content/posts/2023-09-01-formal-cek-machine-in-agda/index.md +++ b/src/content/posts/2023-09-01-formal-cek-machine-in-agda/index.md @@ -11,6 +11,7 @@ tags: heroImage: ./header.jpg heroAlt: gears spinning wallpaper math: true +toc: true --- Back in 2022, I took a special topics course, CSCI 8980, on [reasoning about diff --git a/src/pages/posts/[...slug].astro b/src/pages/posts/[...slug].astro index fd936ff..3a5ac30 100644 --- a/src/pages/posts/[...slug].astro +++ b/src/pages/posts/[...slug].astro @@ -4,6 +4,7 @@ import BaseLayout from "../../layouts/BaseLayout.astro"; import { type CollectionEntry, getCollection } from "astro:content"; import Timestamp from "../../components/Timestamp.astro"; import { getImage } from "astro:assets"; +import TocWrapper from "../../components/TocWrapper.astro"; export async function getStaticPaths() { const posts = await getCollection("posts"); @@ -16,8 +17,8 @@ export async function getStaticPaths() { type Props = CollectionEntry<"posts">; const post = Astro.props; -const { Content, remarkPluginFrontmatter } = await post.render(); -const { heroImage: heroImagePath, heroAlt } = post.data; +const { Content, remarkPluginFrontmatter, headings } = await post.render(); +const { toc, heroImage: heroImagePath, heroAlt } = post.data; let heroImage; if (heroImagePath) { @@ -44,44 +45,44 @@ const datestamp = post.data.date.toLocaleDateString(undefined, { -
-

{post.data.title}

+ +
+

{post.data.title}

+ + + { + post.data.draft && ( + +