Files
blog/lib/posts.ts
Gabriel Kaszewski 8a921b0423 Add new blog posts and update existing content
- Created "My 2023 Coding Edition" post detailing projects and experiences in Rust and game development.
- Added "My 2024 and 2025 roadmap" outlining goals and projects for the upcoming years.
- Introduced "Python Tutorial - Introduction" and "Python - Variables" posts to teach Python programming basics.
- Published "ROADMAP for 2023" to outline initial goals for the year.
- Added "My Rust little adventure" post summarizing various Rust projects undertaken.
- Released "Spanish Inquisition - 3.0.1 UPDATE" detailing the latest game update and features.
- Added multiple background images in AVIF format for website use.
- Removed unused SVG files to clean up the public directory.
2025-09-03 23:27:41 +02:00

80 lines
2.1 KiB
TypeScript

import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import readingTime from 'reading-time';
const postsDirectory = path.join(process.cwd(), 'posts');
export interface PostData {
id: string;
date: string;
title: string;
description: string;
content: string;
readingTime: string;
}
export interface PostMeta {
id: string;
date: string;
title: string;
description: string;
readingTime: string;
}
export function getSortedPostsData(): PostMeta[] {
try {
const fileNames = fs.readdirSync(postsDirectory);
const allPostsData = fileNames
.filter(fileName => fileName.endsWith('.mdx'))
.map(fileName => {
const id = fileName.replace(/\.mdx$/, '');
const fullPath = path.join(postsDirectory, fileName);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const matterResult = matter(fileContents);
const stats = readingTime(matterResult.content);
return {
id,
date: matterResult.data.date as string,
title: matterResult.data.title as string,
description: matterResult.data.description as string,
readingTime: stats.text,
};
});
return allPostsData.sort((a, b) => (a.date < b.date ? 1 : -1));
} catch (error) {
console.log("No 'posts' directory found, returning empty array.");
return [];
}
}
export function getAllPostIds() {
try {
const fileNames = fs.readdirSync(postsDirectory);
return fileNames.map(fileName => ({
params: {
slug: fileName.replace(/\.mdx$/, ''),
},
}));
} catch (error) {
return [];
}
}
export async function getPostData(id: string): Promise<PostData> {
const fullPath = path.join(postsDirectory, `${id}.mdx`);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const matterResult = matter(fileContents);
const stats = readingTime(matterResult.content);
return {
id,
content: matterResult.content,
date: matterResult.data.date,
title: matterResult.data.title,
description: matterResult.data.description,
readingTime: stats.text,
};
}