From d34b440664977a5af47da3118a3e9e18d7e72e62 Mon Sep 17 00:00:00 2001
From: ginger <26461046+gingerchew@users.noreply.github.com>
Date: Wed, 8 Jan 2025 12:20:13 -0500
Subject: [PATCH] feat: Add sticky table of contents (#178)
---
package.json | 2 +-
pnpm-lock.yaml | 10 +-
src/components/TableOfContents.astro | 158 +++++++++++++++++++++++++++
src/layouts/ProseLayout.astro | 76 ++++++++++++-
src/pages/[id].astro | 3 +-
src/pages/abuse.astro | 3 +-
src/pages/blog/[id].astro | 3 +-
src/pages/privacy.astro | 3 +-
src/pages/subprocessors.astro | 3 +-
src/pages/terms.astro | 3 +-
10 files changed, 249 insertions(+), 15 deletions(-)
create mode 100644 src/components/TableOfContents.astro
diff --git a/package.json b/package.json
index bdaa4d3..d6e38e9 100644
--- a/package.json
+++ b/package.json
@@ -32,7 +32,7 @@
"@types/react-dom": "^19.0.2",
"astro": "^5.1.2",
"astro-font": "^0.1.81",
- "astro-github-file-loader": "^1.0.2",
+ "astro-github-file-loader": "^1.1.0",
"dayjs": "^1.11.13",
"fathom-client": "^3.7.2",
"react": "^19.0.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index db675c2..3770e97 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -48,8 +48,8 @@ importers:
specifier: ^0.1.81
version: 0.1.81
astro-github-file-loader:
- specifier: ^1.0.2
- version: 1.0.2(@types/node@22.10.5)(idb-keyval@6.2.1)(jiti@2.4.2)(rollup@4.29.1)(sass@1.83.1)(typescript@5.7.2)(yaml@2.5.1)
+ specifier: ^1.1.0
+ version: 1.1.0(@types/node@22.10.5)(idb-keyval@6.2.1)(jiti@2.4.2)(rollup@4.29.1)(sass@1.83.1)(typescript@5.7.2)(yaml@2.5.1)
dayjs:
specifier: ^1.11.13
version: 1.11.13
@@ -2160,8 +2160,8 @@ packages:
astro-font@0.1.81:
resolution: {integrity: sha512-rbrv/+ZSYdgyjBlhkAqfCjMxqqo6iEwnSo/rAdDt/POILsF0ChxMFhefIIzKEukrRGtVjEj1lrLRYk+AdV9scQ==}
- astro-github-file-loader@1.0.2:
- resolution: {integrity: sha512-KL/ZYr6X5YlmISKLXxfLKCIqJZPe9Fm7TMNiz8VM+6YRF8bPbTQEox7FfWkyeaF+jPumpWGHIsMueA20DtEvhw==}
+ astro-github-file-loader@1.1.0:
+ resolution: {integrity: sha512-zRrmC+lEJPkqzbPQTS/g8l55TwXYDbKuJozKj4aAnCrF3KEP6VHkhVGfnzSABOX3XiGj/FpwBfIQdE//AgP9oQ==}
astro-integration-kit@0.17.0:
resolution: {integrity: sha512-fe31CCKmrGYn/kkBd1J4b7P02gEdMdEIFBz14zdAud+YAmJeLtZD6wmrCz3LDau+lE1oM1hQnvmZXVSM/YveKw==}
@@ -6974,7 +6974,7 @@ snapshots:
astro-font@0.1.81: {}
- astro-github-file-loader@1.0.2(@types/node@22.10.5)(idb-keyval@6.2.1)(jiti@2.4.2)(rollup@4.29.1)(sass@1.83.1)(typescript@5.7.2)(yaml@2.5.1):
+ astro-github-file-loader@1.1.0(@types/node@22.10.5)(idb-keyval@6.2.1)(jiti@2.4.2)(rollup@4.29.1)(sass@1.83.1)(typescript@5.7.2)(yaml@2.5.1):
dependencies:
astro: 5.1.2(@types/node@22.10.5)(idb-keyval@6.2.1)(jiti@2.4.2)(rollup@4.29.1)(sass@1.83.1)(typescript@5.7.2)(yaml@2.5.1)
transitivePeerDependencies:
diff --git a/src/components/TableOfContents.astro b/src/components/TableOfContents.astro
new file mode 100644
index 0000000..a9164a0
--- /dev/null
+++ b/src/components/TableOfContents.astro
@@ -0,0 +1,158 @@
+---
+import type { MarkdownHeading } from "astro";
+
+interface Props {
+ headings?: MarkdownHeading[];
+}
+
+const { headings } = Astro.props;
+
+const showTOC = headings && headings.length > 2;
+---
+
+{
+ showTOC && (
+
+ )
+}
+
+
+
+
diff --git a/src/layouts/ProseLayout.astro b/src/layouts/ProseLayout.astro
index 7421a1b..b95596a 100644
--- a/src/layouts/ProseLayout.astro
+++ b/src/layouts/ProseLayout.astro
@@ -4,6 +4,8 @@ import PageHeader from "~/components/PageHeader.astro";
import type { NamesakeColor } from "~/data/colors";
import { smartquotes } from "../helpers/helpers";
import BaseLayout from "./BaseLayout.astro";
+import type { MarkdownHeading } from "astro";
+import TableOfContents from "~/components/TableOfContents.astro";
interface Props {
title: string;
@@ -13,10 +15,19 @@ interface Props {
ogAlt?: string;
annotation?: RoughAnnotationType;
color?: NamesakeColor;
+ headings?: MarkdownHeading[];
}
-const { title, date, description, ogImage, ogAlt, annotation, color } =
- Astro.props;
+const {
+ title,
+ date,
+ description,
+ ogImage,
+ ogAlt,
+ annotation,
+ color,
+ headings,
+} = Astro.props;
---
-
+
+
@@ -42,9 +58,63 @@ const { title, date, description, ogImage, ogAlt, annotation, color } =