-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.ts
83 lines (72 loc) · 2.46 KB
/
loader.ts
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
import matter from 'gray-matter';
import glob from 'glob';
const { globals } = require('globals.ts');
export type PostData = {
path: string;
title: string;
subtitle?: string;
content: string;
description?: string;
canonicalUrl?: string;
published: boolean;
datePublished: number;
tags?: string[];
bannerImage?: string;
bannerImageAlt?: string;
thumbnailImage?: string;
thumbnailImageAlt?: string;
location?: string;
year?: number;
};
type RawFile = { path: string; contents: string };
export const loadMarkdownFile = async (path: string): Promise<RawFile> => {
const mdFile = await import(`./md/${path}`);
return { path, contents: mdFile.default };
};
export const mdToPost = (file: RawFile): PostData => {
const metadata = matter(file.contents);
const path = file.path.replace('.md', '');
const post = {
path,
title: metadata.data.title,
subtitle: metadata.data.subtitle || null,
published: metadata.data.published || false,
datePublished: metadata.data.datePublished || null,
description: metadata.data.description || null,
canonicalUrl: metadata.data.canonicalUrl || `${globals.url}/${path}`,
thumbnailImage: metadata.data.thumbnailImage || null,
thumbnailImageAlt: metadata.data.thumbnailImageAlt || null,
bannerImage: metadata.data.bannerImage || null,
bannerImageAlt: metadata.data.bannerImageAlt || null,
location: metadata.data.location || null,
year: metadata.data.year || null,
content: metadata.content,
};
if (!post.title)
throw new Error(`Missing required field: title.`);
if (!post.content)
throw new Error(`Missing required field: content.`);
if (!post.datePublished)
throw new Error(`Missing required field: datePublished.`);
return post as PostData;
};
export const loadMarkdownFiles = async (path: string) => {
const blogPaths = glob.sync(`./md/${path}`);
const postDataList = await Promise.all(
blogPaths.map((blogPath) => {
const modPath = blogPath.slice(blogPath.indexOf(`md/`) + 3);
return loadMarkdownFile(`${modPath}`);
})
);
return postDataList;
};
export const loadPost = async (path: string): Promise<PostData> => {
const file = await loadMarkdownFile(path);
return mdToPost(file);
};
export const loadObrasPosts = async (): Promise<PostData[]> => {
return await (await loadMarkdownFiles(`obras/*.md`))
.map(mdToPost)
.filter((p) => p.published)
.sort((a, b) => (b.datePublished || 0) - (a.datePublished || 0));
};