From 3bbcd6f345e422a85d037c19c76c5e081203f7ef Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Tue, 31 Mar 2026 02:22:08 +0200 Subject: [PATCH] feat: add heading extraction and wip flag to posts --- bun.lock | 7 +++++++ lib/posts.ts | 29 ++++++++++++++++++++++++++++- package.json | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/bun.lock b/bun.lock index 34f5e1a..21156fc 100644 --- a/bun.lock +++ b/bun.lock @@ -14,6 +14,7 @@ "react-dom": "19.1.0", "reading-time": "^1.5.0", "rehype-pretty-code": "^0.14.3", + "rehype-slug": "^6.0.0", "remark": "^15.0.1", "remark-html": "^16.0.1", "rss": "^1.2.2", @@ -274,6 +275,8 @@ "extend-shallow": ["extend-shallow@2.0.1", "", { "dependencies": { "is-extendable": "^0.1.0" } }, "sha512-zCnTtlxNoAiDc3gqY2aYAWFx7XWWiasuF2K8Me5WbN8otHKTUKBwjPtNpRs/rbUZm7KxWAaNj7P1a/p52GbVug=="], + "github-slugger": ["github-slugger@2.0.0", "", {}, "sha512-IaOQ9puYtjrkq7Y0Ygl9KDZnrf/aiUJYUpVf89y8kyaxbRG7Y1SrX/jaumrv81vc61+kiMempujsM3Yw7w5qcw=="], + "graceful-fs": ["graceful-fs@4.2.11", "", {}, "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ=="], "gray-matter": ["gray-matter@4.0.3", "", { "dependencies": { "js-yaml": "^3.13.1", "kind-of": "^6.0.2", "section-matter": "^1.0.0", "strip-bom-string": "^1.0.0" } }, "sha512-5v6yZd4JK3eMI3FqqCouswVqwugaA9r4dNZB1wwcmrD02QkV5H0y7XBQW8QwQqEaZY1pM9aqORSORhJRdNK44Q=="], @@ -282,6 +285,8 @@ "hast-util-from-parse5": ["hast-util-from-parse5@8.0.3", "", { "dependencies": { "@types/hast": "^3.0.0", "@types/unist": "^3.0.0", "devlop": "^1.0.0", "hastscript": "^9.0.0", "property-information": "^7.0.0", "vfile": "^6.0.0", "vfile-location": "^5.0.0", "web-namespaces": "^2.0.0" } }, "sha512-3kxEVkEKt0zvcZ3hCRYI8rqrgwtlIOFMWkbclACvjlDw8Li9S2hk/d51OI0nr/gIpdMHNepwgOKqZ/sy0Clpyg=="], + "hast-util-heading-rank": ["hast-util-heading-rank@3.0.0", "", { "dependencies": { "@types/hast": "^3.0.0" } }, "sha512-EJKb8oMUXVHcWZTDepnr+WNbfnXKFNf9duMesmr4S8SXTJBJ9M4Yok08pu9vxdJwdlGRhVumk9mEhkEvKGifwA=="], + "hast-util-parse-selector": ["hast-util-parse-selector@4.0.0", "", { "dependencies": { "@types/hast": "^3.0.0" } }, "sha512-wkQCkSYoOGCRKERFWcxMVMOcYE2K1AaNLU8DXS9arxnLOUEWbOXKXiJUNzEpqZ3JOKpnha3jkFrumEjVliDe7A=="], "hast-util-sanitize": ["hast-util-sanitize@5.0.2", "", { "dependencies": { "@types/hast": "^3.0.0", "@ungap/structured-clone": "^1.0.0", "unist-util-position": "^5.0.0" } }, "sha512-3yTWghByc50aGS7JlGhk61SPenfE/p1oaFeNwkOOyrscaOkMGrcW9+Cy/QAIOBpZxP1yqDIzFMR0+Np0i0+usg=="], @@ -494,6 +499,8 @@ "rehype-recma": ["rehype-recma@1.0.0", "", { "dependencies": { "@types/estree": "^1.0.0", "@types/hast": "^3.0.0", "hast-util-to-estree": "^3.0.0" } }, "sha512-lqA4rGUf1JmacCNWWZx0Wv1dHqMwxzsDWYMTowuplHF3xH0N/MmrZ/G3BDZnzAkRmxDadujCjaKM2hqYdCBOGw=="], + "rehype-slug": ["rehype-slug@6.0.0", "", { "dependencies": { "@types/hast": "^3.0.0", "github-slugger": "^2.0.0", "hast-util-heading-rank": "^3.0.0", "hast-util-to-string": "^3.0.0", "unist-util-visit": "^5.0.0" } }, "sha512-lWyvf/jwu+oS5+hL5eClVd3hNdmwM1kAC0BUvEGD19pajQMIzcNUd/k9GsfQ+FfECvX+JE+e9/btsKH0EjJT6A=="], + "remark": ["remark@15.0.1", "", { "dependencies": { "@types/mdast": "^4.0.0", "remark-parse": "^11.0.0", "remark-stringify": "^11.0.0", "unified": "^11.0.0" } }, "sha512-Eht5w30ruCXgFmxVUSlNWQ9iiimq07URKeFS3hNc8cUWy1llX4KDWfyEDZRycMc+znsN9Ux5/tJ/BFdgdOwA3A=="], "remark-html": ["remark-html@16.0.1", "", { "dependencies": { "@types/mdast": "^4.0.0", "hast-util-sanitize": "^5.0.0", "hast-util-to-html": "^9.0.0", "mdast-util-to-hast": "^13.0.0", "unified": "^11.0.0" } }, "sha512-B9JqA5i0qZe0Nsf49q3OXyGvyXuZFDzAP2iOFLEumymuYJITVpiH1IgsTEwTpdptDmZlMDMWeDmSawdaJIGCXQ=="], diff --git a/lib/posts.ts b/lib/posts.ts index 02b44de..fa621ed 100644 --- a/lib/posts.ts +++ b/lib/posts.ts @@ -5,6 +5,12 @@ import readingTime from 'reading-time'; const postsDirectory = path.join(process.cwd(), 'posts'); +export interface Heading { + level: number; + text: string; + slug: string; +} + export interface PostData { id: string; date: string; @@ -12,6 +18,8 @@ export interface PostData { description: string; content: string; readingTime: string; + headings: Heading[]; + wip?: boolean; } export interface PostMeta { @@ -20,6 +28,22 @@ export interface PostMeta { title: string; description: string; readingTime: string; + wip?: boolean; +} + +function extractHeadings(content: string): Heading[] { + const regex = /^(#{2,3})\s+(.+)$/gm; + const headings: Heading[] = []; + let match; + while ((match = regex.exec(content)) !== null) { + const text = match[2].trim(); + const slug = text + .toLowerCase() + .replace(/[^a-z0-9]+/g, '-') + .replace(/(^-|-$)/g, ''); + headings.push({ level: match[1].length, text, slug }); + } + return headings; } export function getSortedPostsData(): PostMeta[] { @@ -40,6 +64,7 @@ export function getSortedPostsData(): PostMeta[] { title: matterResult.data.title as string, description: matterResult.data.description as string, readingTime: stats.text, + wip: matterResult.data.wip ?? false, }; }); @@ -76,5 +101,7 @@ export async function getPostData(id: string): Promise { title: matterResult.data.title, description: matterResult.data.description, readingTime: stats.text, + headings: extractHeadings(matterResult.content), + wip: matterResult.data.wip ?? false, }; -} \ No newline at end of file +} diff --git a/package.json b/package.json index b6e1c08..72bdc28 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "react-dom": "19.1.0", "reading-time": "^1.5.0", "rehype-pretty-code": "^0.14.3", + "rehype-slug": "^6.0.0", "remark": "^15.0.1", "remark-html": "^16.0.1", "rss": "^1.2.2",