🪛 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)
|
||||
|
||||
33
lib/seo.ts
Normal file
33
lib/seo.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
export interface SEOData {
|
||||
title: string
|
||||
description: string
|
||||
url: string
|
||||
image?: string
|
||||
type?: 'website' | 'article'
|
||||
author?: string
|
||||
publishedTime?: string
|
||||
}
|
||||
|
||||
export function generateStructuredData(data: SEOData) {
|
||||
const structuredData: any = {
|
||||
'@context': 'https://schema.org',
|
||||
'@type': data.type === 'article' ? 'BlogPosting' : 'WebSite',
|
||||
headline: data.title,
|
||||
description: data.description,
|
||||
url: data.url,
|
||||
}
|
||||
|
||||
if (data.image) {
|
||||
structuredData.image = data.image
|
||||
}
|
||||
|
||||
if (data.type === 'article') {
|
||||
structuredData.author = {
|
||||
'@type': 'Person',
|
||||
name: data.author || 'Unknown',
|
||||
}
|
||||
structuredData.datePublished = data.publishedTime
|
||||
}
|
||||
|
||||
return JSON.stringify(structuredData)
|
||||
}
|
||||
Reference in New Issue
Block a user