-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathposts.ts
More file actions
50 lines (40 loc) · 1.32 KB
/
posts.ts
File metadata and controls
50 lines (40 loc) · 1.32 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
import { readOne, readAll } from "./markdoc/read";
import { blog } from "./markdoc/frontmatter.schema";
import {SITE_BANNER_URL, SITE_URL} from "../config";
export async function getPostSlugs() {
const posts = await readAll({
directory: "blog",
frontmatterSchema: blog,
});
// we don't want to generate pages for posts that link to external websites
const filteredPosts = posts
.filter((p) => p.frontmatter.draft !== true)
.filter(({ frontmatter }) => !frontmatter.external);
//console.log(filteredPosts.map(post => post.slug));
return filteredPosts.map((post) => {
return { params: { slug: post.slug } };
})
}
export async function getPost(slug: string) {
if (!slug) {
throw Error(`slug should be string. Received: ${slug}`);
}
const posts = await readAll({
directory: "blog",
frontmatterSchema: blog,
});
const post = posts.find(post => post.slug === slug);
if (!post) {
throw Error(`Could not find post: ${slug}`);
}
const { content, frontmatter } = post;
const ogImageAbsoluteUrl =
frontmatter.ogImagePath
? new URL(frontmatter.ogImagePath, SITE_URL).toString()
: SITE_BANNER_URL;
return {
content,
ogImageAbsoluteUrl,
frontmatter
}
}