-
Notifications
You must be signed in to change notification settings - Fork 417
/
Copy pathchangelog.ts
56 lines (47 loc) · 1.49 KB
/
changelog.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
import fs from "fs";
import path from "path";
export type ChangelogMetadata = {
title: string;
app: string;
publishedAt: string;
version: string;
image?: string;
};
function parseFrontmatter(fileContent: string) {
let frontmatterRegex = /---\s*([\s\S]*?)\s*---/;
let match = frontmatterRegex.exec(fileContent);
let frontMatterBlock = match![1];
let content = fileContent.replace(frontmatterRegex, "").trim();
let frontMatterLines = frontMatterBlock.trim().split("\n");
let metadata: Partial<ChangelogMetadata> = {};
frontMatterLines.forEach((line) => {
let [key, ...valueArr] = line.split(": ");
let value = valueArr.join(": ").trim();
value = value.replace(/^['"](.*)['"]$/, "$1"); // Remove quotes
metadata[key.trim() as keyof ChangelogMetadata] = value;
});
return { metadata: metadata as ChangelogMetadata, content };
}
const dir = path.join(process.cwd(), "content/changelog");
function getMDXFiles() {
return fs.readdirSync(dir).filter((file) => path.extname(file) === ".mdx");
}
function readMDXFile(filePath: string) {
let rawContent = fs.readFileSync(filePath, "utf-8");
return parseFrontmatter(rawContent);
}
function getMDXData() {
let mdxFiles = getMDXFiles();
return mdxFiles.map((file) => {
let { metadata, content } = readMDXFile(path.join(dir, file));
let slug = path.basename(file, path.extname(file));
return {
metadata,
slug,
content,
};
});
}
export function getChangelogPosts() {
return getMDXData();
}