-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathblog-server.ts
More file actions
43 lines (37 loc) · 1.41 KB
/
blog-server.ts
File metadata and controls
43 lines (37 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { readFileSync, existsSync } from 'fs'
import { join } from 'path'
import matter from 'gray-matter'
import articlesIndex from '../public/articles/articles-index.json'
import type { BlogPost } from '@/types/blog'
// Server-side function to get post by slug with actual markdown content
export function getPostBySlugServer(slug: string): BlogPost | null {
try {
const markdownPath = join(process.cwd(), 'public/articles', `${slug}.md`)
if (existsSync(markdownPath)) {
const fileContents = readFileSync(markdownPath, 'utf8')
const { content } = matter(fileContents)
// Find the article metadata from the index
const articleMetadata = articlesIndex.articles.find(article => article.slug === slug)
if (!articleMetadata) {
return null
}
return {
...articleMetadata,
content
}
} else {
// Fallback: return post with placeholder content
const articleMetadata = articlesIndex.articles.find(article => article.slug === slug)
if (!articleMetadata) {
return null
}
return {
...articleMetadata,
content: `# ${articleMetadata.title}\n\nThis is a placeholder content for ${articleMetadata.title}. The full content would be loaded from the markdown file.`
}
}
} catch (error) {
console.error(`Error reading markdown file for ${slug}:`, error)
return null
}
}