fun improvements (#1)
Some checks failed
Build and Deploy Blog / build-and-deploy-local (push) Failing after 11s

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-03-31 00:50:16 +00:00
parent 902521e1f3
commit 1ba5ee1b41
13 changed files with 424 additions and 66 deletions

View File

@@ -2,9 +2,16 @@ import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import readingTime from 'reading-time';
import { notFound } from 'next/navigation';
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 +19,8 @@ export interface PostData {
description: string;
content: string;
readingTime: string;
headings: Heading[];
wip: boolean;
}
export interface PostMeta {
@@ -20,6 +29,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 +65,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,
};
});
@@ -65,6 +91,7 @@ export function getAllPostIds() {
export async function getPostData(id: string): Promise<PostData> {
const fullPath = path.join(postsDirectory, `${id}.mdx`);
if (!fs.existsSync(fullPath)) notFound();
const fileContents = fs.readFileSync(fullPath, 'utf8');
const matterResult = matter(fileContents);
const stats = readingTime(matterResult.content);
@@ -76,5 +103,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,
};
}
}