🪛 03 routing
This commit is contained in:
@@ -52,12 +52,13 @@ export function validateFrontmatter(data: any): FrontMatter {
|
||||
};
|
||||
}
|
||||
|
||||
export function getPostBySlug(slug: string[]): Post {
|
||||
const sanitized = slug.map(s => sanitizePath(s));
|
||||
export function getPostBySlug(slug: string | string[]): Post | null {
|
||||
const slugArray = Array.isArray(slug) ? slug : [slug];
|
||||
const sanitized = slugArray.map(s => sanitizePath(s));
|
||||
const fullPath = path.join(POSTS_PATH, ...sanitized) + '.md';
|
||||
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
throw new Error('Post not found');
|
||||
return null;
|
||||
}
|
||||
|
||||
const fileContents = fs.readFileSync(fullPath, 'utf8');
|
||||
@@ -89,7 +90,7 @@ export function getAllPosts(includeContent = false): Post[] {
|
||||
const slug = prefix ? `${prefix}/${file.replace(/\.md$/, '')}` : file.replace(/\.md$/, '');
|
||||
try {
|
||||
const post = getPostBySlug(slug.split('/'));
|
||||
if (!post.frontmatter.draft) {
|
||||
if (post && !post.frontmatter.draft) {
|
||||
posts.push(includeContent ? post : { ...post, content: '' });
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -106,8 +107,12 @@ export function getAllPosts(includeContent = false): Post[] {
|
||||
return posts.sort((a, b) => new Date(b.frontmatter.date).getTime() - new Date(a.frontmatter.date).getTime());
|
||||
}
|
||||
|
||||
export function getRelatedPosts(currentSlug: string, category: string, tags: string[], limit = 3): Post[] {
|
||||
export async function getRelatedPosts(currentSlug: string, limit = 3): Promise<Post[]> {
|
||||
const currentPost = getPostBySlug(currentSlug);
|
||||
if (!currentPost) return [];
|
||||
|
||||
const allPosts = getAllPosts(false);
|
||||
const { category, tags } = currentPost.frontmatter;
|
||||
|
||||
const scored = allPosts
|
||||
.filter(post => post.slug !== currentSlug)
|
||||
|
||||
Reference in New Issue
Block a user