-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathposts.ts
More file actions
138 lines (117 loc) · 3.88 KB
/
posts.ts
File metadata and controls
138 lines (117 loc) · 3.88 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { generateReadingTime } from '@/lib/utils';
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import { toc } from 'mdast-util-toc';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
const processor = unified().use(remarkParse);
export interface PostData {
slug: string;
pinned: number;
title: string;
description: string;
author: string;
author_title: string;
author_url: string;
author_image_url: string;
categories: string[];
tags: string[];
date: string;
content: string;
video_url?: string;
og_image?: string;
thumb_image?: string;
reading_time?: number;
last_modified: string;
featured?: boolean;
toc?: string;
comments?: boolean;
canonical_url?: string;
series?: string;
cover_image?: string;
related_posts?: string[];
word_count?: number;
unpublished?: boolean;
}
const postsDirectory = path.join(process.cwd(), '_blog');
export function getAllPosts(): PostData[] {
const fileNames = fs.readdirSync(postsDirectory);
return fileNames
.map(getPostByFilename)
.sort((a, b) => (new Date(a.date) > new Date(b.date) ? -1 : 1))
.filter((item) => !item.unpublished);
}
export async function getPostData(slug: string): Promise<PostData> {
const fileNames = fs.readdirSync(postsDirectory);
const fileName = fileNames.find((name) => name.includes(slug))!;
if(!fileName) {
console.error(`[getPostData] Post not found for slug: "${slug}"`);
console.error(`[getPostData] Posts directory: ${postsDirectory}`);
console.error(`[getPostData] Available files (${fileNames.length} total):`, fileNames.slice(0, 10));
console.error(`[getPostData] Searched with: name.includes("${slug}")`);
throw new Error('Post not found');
}
return getPostByFilename(fileName);
}
export async function getRelatedPosts(post: PostData): Promise<PostData[]> {
const relatedPosts = post.related_posts || [];
const posts = await Promise.all(
relatedPosts.map(async (slug) => {
try {
return await getPostData(slug);
} catch (error) {
console.error(`[getRelatedPosts] Failed to load related post with slug: "${slug}" for post: "${post.slug}"`);
console.error(`[getRelatedPosts] Error:`, error);
return null;
}
})
);
return posts.filter((item) => item !== null && !item.unpublished) as PostData[];
}
export function getPostByFilename(fileName: string): PostData {
const fullPath = path.join(postsDirectory, fileName);
const [, , , ...rest] = fileName.replace(/\.mdx$/, '').split('-');
const slug = rest.join('-');
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { data, content } = matter(fileContents);
const tree = processor.parse(content);
const tocResult = toc(tree, {
maxDepth: data.toc_depth ? data.toc_depth : 2,
}).map;
return {
slug,
unpublished: data.unpublished || false,
pinned: data.pinned || 0,
title: data.title,
description: data.description,
author: data.author,
author_title: data.author_title,
author_url: data.author_url,
author_image_url: data.author_image_url,
categories: data.categories || [],
tags: data.tags || [],
date: data.date,
content,
word_count: content.split(/\s+/gu).length,
// New fields
video_url: data.video_url,
og_image: data.image,
thumb_image: data.thumb,
reading_time: generateReadingTime(content),
last_modified: data.last_modified || data.date,
featured: data.featured || false,
toc: tocResult
? unified()
.use(remarkStringify)
// eslint-disable-next-line
.stringify(tocResult as any).replaceAll('**', '')
: '',
comments: data.comments !== undefined ? data.comments : true,
canonical_url: data.canonical_url,
series: data.series,
cover_image: data.image,
related_posts: data.related,
};
}