forked from POWSummit/pow-summit-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatePostJson.js
157 lines (132 loc) · 3.73 KB
/
generatePostJson.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const fs = require("fs");
const glob = require("glob");
const yaml = require("yaml");
const markdownIt = require("markdown-it");
// helper functions
const clearHTMLTags = function (str) {
return str.replace(/(<([^>]+)>)/gi, "");
};
const clearCommonMDTags = function (str) {
return str
.replaceAll("-", "")
.replaceAll("\n", "")
.replaceAll("**", "")
.replaceAll("##", "")
.replaceAll("\n", "");
};
const getDescriptionFromBody = function (mdFile) {
const buffer = fs.readFileSync(mdFile);
let fileContent = buffer.toString();
// clear meta data
const metas = /---(.*?)---/s.exec(fileContent);
if (metas.length > 0) {
fileContent = fileContent.replaceAll(metas[0], "");
}
//try to get first n character as plain text
let body = "";
let fullBody = "";
try {
let md = new markdownIt({
html: true,
linkify: true,
typographer: true,
});
body = md.render(fileContent);
body = clearHTMLTags(body);
fullBody = body = clearCommonMDTags(body);
fullBody = fullBody.slice(0, 10000);
body = body.slice(0, 160) + "...";
} catch (e) {}
return [body, fullBody];
};
const getMetaData = function (mdFile) {
const buffer = fs.readFileSync(mdFile);
const fileContent = buffer.toString();
let metadata = {};
const metas = /---(.*?)---/s.exec(fileContent);
if (metas.length > 0) {
const clearMeta = metas[0].replaceAll("---", "");
try {
metadata = yaml.parse(clearMeta);
} catch ($e) {}
}
return metadata;
};
const parseDateFromFileName = function (file) {
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const splited = file.split("-");
let dateText = "";
if (splited.length > 3) {
const year = splited[0] || null;
const month = splited[1] || null;
const day = splited[2] || null;
const date = new Date(`${year}-${month}-${day}`);
if (Object.prototype.toString.call(date) === "[object Date]") {
// it is a date
if (isNaN(date)) {
// d.getTime() or d.valueOf() will also work
// date object is not valid
dateText = "";
} else {
dateText = `${monthNames[date.getMonth()]} ${String(
date.getDate()
).padStart(2, "0")}, ${date.getFullYear()}`;
}
} else {
dateText = "";
}
}
return dateText;
};
// main process
const folder = `./src/contents/posts`;
const postsJson = [];
fs.readdirSync(folder)
.reverse()
.forEach((file) => {
const mdFiles = glob.globSync(`${folder}/${file}/*.md`);
if (mdFiles.length > 0) {
const mdFile = mdFiles[0];
const metadata = getMetaData(mdFile);
const imgFiles = glob.globSync(`${folder}/${file}/*.{jpeg,jpg,png}`);
console.log(imgFiles.length);
if (imgFiles.length > 0) {
let featuredImgPath = imgFiles[0];
const folderRelative = `${folder}/${file}/`.replace("./", "");
const featuredImageName = featuredImgPath.replace(folderRelative, "");
fs.copyFile(
featuredImgPath,
`public/img/posts/featuredImg/${featuredImageName}`,
(err) => {
console.log(err)
}
);
}
postsJson.push({
title: metadata.title || "",
description: metadata.description || getDescriptionFromBody(mdFile)[0],
author: metadata.author || "",
tags: metadata.tags || [],
alias: file,
date: parseDateFromFileName(file),
image: metadata.featuredImage,
});
}
});
fs.writeFileSync(
`./src/contents/posts.json`,
JSON.stringify(postsJson, null, 2)
);