-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdx.ts
More file actions
102 lines (91 loc) · 2.9 KB
/
mdx.ts
File metadata and controls
102 lines (91 loc) · 2.9 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
import fs from "fs";
import path from "path";
import matter from "gray-matter";
import {
getDatabasePosts,
getDatabasePostBySlug,
type BlogPost,
} from "./posts";
import { BlogConfig } from "./config";
export async function getAllPosts(config: BlogConfig): Promise<BlogPost[]> {
// Get file-based posts
const fileNames = fs.readdirSync(config.contentPath);
const filePosts = fileNames
.filter(
(fileName) =>
!fs.lstatSync(path.join(config.contentPath, fileName)).isSymbolicLink()
)
.map((fileName) => {
const slug = fileName.replace(/\.mdx$/, "");
const fullPath = path.join(config.contentPath, fileName);
const fileContents = fs.readFileSync(fullPath, "utf8");
const { data: frontmatter } = matter(fileContents);
return {
slug,
frontmatter: {
title: frontmatter.title || "",
date: frontmatter.date || "",
description: frontmatter.description || "",
author: frontmatter.author || "",
draft: frontmatter.draft,
},
source: "file" as const,
};
});
// Get database posts
const databasePosts = await getDatabasePosts(config.supabase);
// Merge posts, prioritizing database posts over file posts with same slug
const postMap = new Map<string, BlogPost>();
// Add file posts first
filePosts.forEach((post: any) => {
postMap.set(post.slug, post);
});
// Add database posts (will overwrite file posts with same slug)
databasePosts.forEach((post: any) => {
postMap.set(post.slug, post);
});
// Convert back to array and sort by date
const allPosts = Array.from(postMap.values());
return allPosts.sort((a, b) => {
return (
new Date(b.frontmatter.date).getTime() -
new Date(a.frontmatter.date).getTime()
);
});
}
export async function getPostBySlug(slug: string, config: BlogConfig) {
// First check database for the post
const databasePost = await getDatabasePostBySlug(config.supabase, slug);
if (databasePost) {
return {
frontmatter: {
title: databasePost.title,
date: databasePost.date,
description: databasePost.description || "",
author: databasePost.author,
draft: databasePost.draft,
},
content: databasePost.content,
source: "database" as const,
};
}
// Fall back to file-based post
try {
const fullPath = path.join(config.contentPath, `${slug}.mdx`);
const fileContents = fs.readFileSync(fullPath, "utf8");
const { data: frontmatter, content } = matter(fileContents);
return {
frontmatter: {
title: frontmatter.title || "",
date: frontmatter.date || "",
description: frontmatter.description || "",
author: frontmatter.author || "",
draft: frontmatter.draft,
},
content,
source: "file" as const,
};
} catch (error) {
throw new Error(`Post not found: ${slug}`);
}
}