-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreatePage.js
105 lines (97 loc) · 3.11 KB
/
createPage.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
const fs = require("fs");
const fse = require("fs-extra");
const showdown = require("showdown");
const yaml = require("js-yaml");
const conv = new showdown.Converter({
metadata: true,
parseImgDimension: true
});
// Globals
const contentDir = [__dirname, "/content"].join("");
const parDir = [__dirname, "/partial"].join("");
const publicDir = [__dirname, "/public"].join("");
if (!fs.existsSync(publicDir)) {
fs.mkdirSync(publicDir);
}
try {
console.log("Copying static assets...")
fse.copySync(__dirname + '/cssjs', publicDir);
fse.copySync(__dirname + '/assets', publicDir);
console.log('Copied static assets!')
} catch (err) {
console.error(err)
}
const pars = {
head: fs.readFileSync([parDir, "/head.par"].join(""), "utf-8"),
nav: fs.readFileSync([parDir, "/nav.par"].join(""), "utf-8"),
footer: fs.readFileSync([parDir, "/footer.par"].join(""), "utf-8")
};
const getTitleEle = title => ["<title>", title, "</title>"].join("");
const getDesEle = des =>
[`<meta name="description" content="`, des, `"/>`].join("");
const getHeadingEle = heading => ["<h1>", heading, "</h1>"].join("");
const parseIncludes = (str, tpl, includeStr) => {
const tplIndex = str.indexOf(tpl);
return [
str.slice(0, tplIndex),
includeStr,
str.slice(tplIndex + tpl.length)
].join("");
};
const createPage = (mdFilePath, publicDir) => {
const md = fs.readFileSync(mdFilePath, "utf-8");
const html = conv.makeHtml(md);
const meta = yaml.safeLoad(conv.getMetadata("raw"));
if (meta.template === "post") {
const pageHtml = [
"<!DOCTYPE html><html>",
parseIncludes(
parseIncludes(pars.head, "<?title --text/>", getTitleEle(meta.title)),
"<?Description/>",
getDesEle(meta.description ? meta.description : meta.title)
),
"<body>",
pars.nav,
'<main class="content">',
getHeadingEle(meta.title),
html,
"</main>",
pars.footer,
meta.prism
? '<link rel="stylesheet" href="css/prism.css" /><script src="js/prism.js"></script></body></html>'
: ""
].join("");
fs.writeFileSync([publicDir, "/", meta.slug, ".html"].join(""), pageHtml);
} else {
const pageHtml = [
"<!DOCTYPE html><html>",
parseIncludes(
parseIncludes(pars.head, "<?title --text/>", getTitleEle(meta.title)),
"<?Description/>",
getDesEle(meta.description ? meta.description : meta.title)
),
"<body>",
pars.nav,
'<main class="content">',
html,
"</main>",
pars.footer,
meta.css ? `<link rel="stylesheet" href="css/${meta.css}"/>` : "",
meta.js ? `<script src="js/${meta.js}"></script>` : "",
"</body></html>"
].join("");
fs.writeFileSync([publicDir, "/", meta.slug, ".html"].join(""), pageHtml);
}
};
try {
// create pages
fs.readdirSync([contentDir, "/pages"].join("")).forEach(mdFile => {
createPage([contentDir, "/pages/", mdFile].join(""), publicDir);
});
// create posts
fs.readdirSync([contentDir, "/posts"].join("")).forEach(mdFile => {
createPage([contentDir, "/posts/", mdFile].join(""), publicDir);
});
} catch (err) {
console.error(err);
}