-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-markdown.ts
168 lines (159 loc) · 4.33 KB
/
parse-markdown.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { readFileSync, readdirSync, writeFileSync } from "fs";
import splitMetadataFromMDContent from "parse-md";
import { Marked } from "marked";
import { v4 as uuidv4 } from "uuid";
import { Plugin } from "vite";
type Metadata = {
published: boolean;
title: string;
description: string;
date: Date;
slug: string;
author: string;
authorImage: string;
tags?: string[];
imageUrl?: string;
};
type Post = {
id: string;
metadata: Metadata;
html: string;
};
export function buildPostsFromMarkdown(): Plugin {
return {
name: "posts_builder",
buildStart() {
process.stdout.write("Building posts.json file...\n");
createPostsJson();
},
handleHotUpdate(ctx) {
if (ctx.file.includes("content")) {
createPostsJson();
}
},
};
}
function createPostsJson() {
try {
const files = readdirSync("./content").filter((file) =>
file.endsWith(".md"),
);
const output: Post[] = [];
files.forEach((file) => {
const post = parseMarkdownFile(`./content/${file}`);
if (!post) {
console.log("\x1b[34mError in " + file + ", skipping \x1b[0m");
return null;
}
output.push(post);
});
writeFileSync("./src/posts/posts.json", JSON.stringify(output, null, 2));
} catch (error) {
console.error(error);
}
}
function parseMarkdownFile(filePath: string) {
const myFile = readFileSync(filePath, "utf-8");
const { metadata, content } = splitMetadataFromMDContent(myFile) as {
metadata: Metadata;
content: string;
};
const fileName = filePath.split("/").pop();
if (!fileName) {
console.error(" \x1b[33mBad path: ", filePath, " \x1b[0m");
return null;
}
if (!validateMetadata(metadata, fileName)) {
console.error(" \x1b[33minvalid metadata in", fileName, " \x1b[0m");
return null;
}
const marked = new Marked();
const html = marked.parse(content);
const output = {
id: uuidv4(),
metadata,
html,
};
return output as Post;
}
function validateMetadata(metadata: unknown, fileName: string) {
if (typeof metadata != "object" || metadata === null) {
return false;
}
const {
published,
title,
description,
date,
author,
authorImage,
slug,
imageUrl,
} = metadata as Metadata;
try {
if (typeof published !== "boolean") {
throw new Error(
"\x1b[31mThe markdown must contain a published boolean. \x1b[0m",
);
}
if (typeof title !== "string") {
throw new Error(
"\x1b[31mThe markdown must contain a valid title string. \x1b[0m",
);
}
if (typeof description !== "string") {
throw new Error(
"\x1b[31mThe markdown must contain a valid description string. \x1b[0m",
);
}
if (!(date instanceof Date)) {
throw new Error(
"\x1b[31mThe markdown must contain a valid date object. \x1b[0m",
);
}
if (typeof slug !== "string") {
throw new Error(
"\x1b[31mThe markdown must contain a valid slug string. \x1b[0m",
);
}
if (
imageUrl &&
!imageUrl.startsWith("http://") &&
!imageUrl.startsWith("https://") &&
!imageUrl.startsWith("/")
) {
throw new Error(
"\x1b[31mimageUrl must be an absolute URL or a path starting with /\nExample: /images/my-image.jpg for image in public folder\nOr: https://example.com/image.jpg for an external image\x1b[0m",
);
}
if (imageUrl === null) {
throw new Error("\x1b[31mimageUrl must be a string\x1b[0m");
}
if (typeof author !== "string") {
throw new Error(
"\x1b[31mThe markdown must contain a valid author.\x1b[0m",
);
}
if (typeof authorImage !== "string") {
throw new Error("\x1b[31mauthorImage must be a string\x1b[0m");
}
if (
!authorImage.startsWith("http://") &&
!authorImage.startsWith("https://") &&
!authorImage.startsWith("/")
) {
throw new Error(
"\x1b[31mimageUrl must be an absolute URL or a path starting with /\nExample: /images/my-image.jpg for image in public folder\nOr: https://example.com/image.jpg for an external image\x1b[0m",
);
}
} catch (error) {
if (error instanceof Error) {
console.error(
"\x1b[34mThere was an error inside of " + fileName + ":\n\x1b[0m",
error.message,
);
}
return false;
}
return true;
}