Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add table of contents to blog
Marc Seitz30c119ce2ed6
diff --git a/app/(static)/blog/[slug]/page.tsx b/app/(static)/blog/[slug]/page.tsxindex ed73a6d5..7a58e08c 100644--- a/app/(static)/blog/[slug]/page.tsx+++ b/app/(static)/blog/[slug]/page.tsx@@ -5,6 +5,7 @@ import Link from "next/link";import BlurImage from "@/components/blur-image";import { constructMetadata, formatDate } from "@/lib/utils";import { Metadata } from "next";+import TableOfContents from "@/components/mdx/table-of-contents";export async function generateStaticParams() {const posts = await getPosts();@@ -52,7 +53,7 @@ export default async function BlogPage({</h1><p className="text-xl text-gray-600">{post.data.summary}</p>- <div className="items-center space-x-4 flex flex-col self-start sm:hidden">+ <div className="items-center space-x-4 flex flex-col self-start"><Linkhref={`https://twitter.com/mfts0`}className="group flex items-center space-x-3"@@ -77,16 +78,24 @@ export default async function BlogPage({<div className="relative"><div className="grid grid-cols-4 gap-10 py-10 max-w-7xl w-full mx-auto px-4 md:px-8">- <div className="relative col-span-4 mb-10 flex flex-col space-y-8 bg-white md:col-span-3 sm:border-r sm:border-orange-500/50">- <div className="prose prose-h2:mb-2 first:prose-h2:mt-0 prose-h2:mt-10 prose-headings:font-medium sm:max-w-screen-md sm:pr-2 md:pr-0">+ <div className="relative col-span-4 mb-10 flex flex-col space-y-8 bg-white md:col-span-3 sm:border-r sm:border-orange-500">+ <div+ data-mdx-container+ className="prose prose-h2:mb-2 first:prose-h2:mt-0 prose-h2:mt-10 prose-headings:font-medium sm:max-w-screen-md sm:pr-2 md:pr-0"+ ><ContentBody>{post.body}</ContentBody></div></div><div className="sticky top-14 col-span-1 hidden flex-col divide-y divide-gray-200 self-start sm:flex">+ <div className="flex flex-col space-y-4">+ <TableOfContents items={post.toc} />+ </div>+ </div>++ {/* <div className="sticky top-14 col-span-1 hidden flex-col divide-y divide-gray-200 self-start sm:flex"><div className="flex flex-col space-y-4"><p className="text-sm text-gray-500">Written by</p>- {/* <Author username={data.author} /> */}<Linkhref={`https://twitter.com/mfts0`}className="group flex items-center space-x-3"@@ -106,7 +115,7 @@ export default async function BlogPage({</div></Link></div>- </div>+ </div> */}</div></div></>diff --git a/lib/content/blog.ts b/lib/content/blog.tsindex 21f66586..e71fde9c 100644--- a/lib/content/blog.ts+++ b/lib/content/blog.ts@@ -1,5 +1,6 @@import matter from "gray-matter";import { cache } from "react";+import { getHeadings } from "./utils";type Post = {data: {@@ -12,6 +13,7 @@ type Post = {published: boolean;};body: string;+ toc: { text: string; level: number }[];};const GITHUB_CONTENT_TOKEN = process.env.GITHUB_CONTENT_TOKEN;@@ -46,7 +48,9 @@ export const getPostsRemote = cache(async () => {return null;}- return { data, body: fileContent } as Post;+ const headingItems = await getHeadings(fileContent);++ return { data, body: fileContent, toc: headingItems } as Post;}),);diff --git a/lib/content/help.ts b/lib/content/help.tsindex 30500ac0..d6c6b9ca 100644--- a/lib/content/help.ts+++ b/lib/content/help.ts@@ -1,5 +1,6 @@import matter from "gray-matter";import { cache } from "react";+import { getHeadings } from "./utils";type Article = {data: {@@ -64,24 +65,3 @@ export async function getHelpArticle(slug: string) {const articles = await getHelpArticles();return articles.find((article) => article?.data.slug === slug);}--async function getHeadings(source: string) {- // Get each line individually, and filter out anything that- // isn't a heading.- const headingLines = source.split("\n").filter((line) => {- return line.match(/^###*\s/);- });-- // Transform the string '## Some text' into an object- // with the shape '{ text: 'Some text', level: 2 }'- return headingLines.map((raw) => {- const text = raw.replace(/^###*\s/, "");- // I only care about h2.- // If I wanted more levels, I'd need to count the- // number of #s.- // match only h2- const level = raw.slice(0, 3) === "###" ? 3 : 2;-- return { text, level };- });-}diff --git a/lib/content/utils.ts b/lib/content/utils.tsnew file mode 100644index 00000000..7a78c1d2--- /dev/null+++ b/lib/content/utils.ts@@ -0,0 +1,20 @@+export async function getHeadings(source: string) {+ // Get each line individually, and filter out anything that+ // isn't a heading.+ const headingLines = source.split("\n").filter((line) => {+ return line.match(/^##\s/);+ });++ // Transform the string '## Some text' into an object+ // with the shape '{ text: 'Some text', level: 2 }'+ return headingLines.map((raw) => {+ const text = raw.replace(/^##\s/, "");+ // I only care about h2.+ // If I wanted more levels, I'd need to count the+ // number of #s.+ // match only h2+ // const level = raw.slice(0, 2) === "##" ? 2 : 1;++ return { text, level: 2 };+ });+}