feat: add heading extraction and wip flag to posts

This commit is contained in:
2026-03-31 02:22:08 +02:00
parent 902521e1f3
commit 3bbcd6f345
3 changed files with 36 additions and 1 deletions

View File

@@ -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<PostData> {
title: matterResult.data.title,
description: matterResult.data.description,
readingTime: stats.text,
headings: extractHeadings(matterResult.content),
wip: matterResult.data.wip ?? false,
};
}
}