forked from dfinity/portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog-posts.js
137 lines (115 loc) · 3.51 KB
/
blog-posts.js
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
const fs = require("fs");
const path = require("path");
const logger = require("@docusaurus/logger");
const {
parseBlogFileName,
} = require("@docusaurus/plugin-content-blog/lib/blogUtils");
const {
getFileCommitDate,
normalizeUrl,
parseMarkdownString,
} = require("@docusaurus/utils");
const glob = require("glob");
function postDate(frontMatter, pathName, parsedPathNameDate) {
if (frontMatter.date) {
if (frontMatter.date instanceof Date) {
return frontMatter.date;
}
if (typeof frontMatter.date === "string") {
return new Date(frontMatter.date);
}
logger.error(
`Blog post ${pathName} date is invalid. Please use "YYYY-MM-DD" format.`
);
}
if (parsedPathNameDate) {
return parsedPathNameDate;
}
// fallback to git commit date
try {
return getFileCommitDate(pathName, { age: "oldest" }).date;
} catch {
return fs.statSync(pathName).birthtime;
}
}
function parseBlogPostMarkdownFile(blogSourceAbsolute) {
const markdownString = fs.readFileSync(blogSourceAbsolute, "utf-8");
try {
return parseMarkdownString(markdownString, {
removeContentTitle: true,
});
} catch (err) {
logger.error`Error while parsing blog post file path=${blogSourceAbsolute}.`;
throw err;
}
}
function formatBlogPostDate(locale, date, calendar) {
try {
return new Intl.DateTimeFormat(locale, {
day: "numeric",
month: "long",
year: "numeric",
timeZone: "UTC",
calendar,
}).format(date);
} catch (err) {
logger.error`Can't format blog post date "${String(date)}"`;
throw err;
}
}
/** @type {import('@docusaurus/types').PluginModule} */
const BlogPostsPlugin = async function (context) {
const blogConfig = context.siteConfig.presets?.find(
([name, _]) => name == "classic"
)?.[1].blog;
const { i18n } = context;
const { baseUrl } = context.siteConfig;
const { path: blogPath, routeBasePath = "blog" } = blogConfig;
const blogDir = path.join(__dirname, "..", blogPath);
return {
name: "blog-posts",
getPathsToWatch() {
return [path.join(blogDir, "**/*.{md,mdx}")];
},
async loadContent() {
const posts = [];
for (const postAbsolute of glob.globSync(
path.join(blogDir, "**/*.{md,mdx}"),
{
cwd: __dirname,
}
)) {
const relativePath = path.relative(blogDir, postAbsolute);
const { frontMatter, contentTitle } =
parseBlogPostMarkdownFile(postAbsolute);
let parsedFileName = parseBlogFileName(relativePath);
const date = postDate(frontMatter, postAbsolute, parsedFileName.date);
const slug = frontMatter.slug ?? parsedFileName.slug;
const permalink = normalizeUrl([baseUrl, routeBasePath, slug]);
const title =
frontMatter.title ?? contentTitle ?? parsedBlogFileName.text;
const formattedDate = formatBlogPostDate(
i18n.currentLocale,
date,
i18n.localeConfigs[i18n.currentLocale].calendar
);
posts.push({
date,
formattedDate,
tags: frontMatter.tags ?? [],
permalink,
title,
description: frontMatter.description,
image: frontMatter.image,
});
}
posts.sort((a, b) => b.date.getTime() - a.date.getTime());
return posts;
},
async contentLoaded({ content, actions }) {
const { createData } = actions;
createData("blog-posts.json", JSON.stringify(content, null, 2));
},
};
};
module.exports = BlogPostsPlugin;