-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.mjs
More file actions
119 lines (99 loc) · 3.48 KB
/
Copy pathbuild.mjs
File metadata and controls
119 lines (99 loc) · 3.48 KB
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
import MarkdownIt from 'markdown-it';
import sass from 'node-sass';
import fs from 'fs';
import formatDate from './util/formatDate.mjs';
import copyFiles from './util/copyFiles.mjs';
import minifyHtml from './util/minifyHtml.mjs';
import parseFrontMatter from './util/parseFrontMatter.mjs';
const defaultTitle = 'knezevicdev';
const defaultDescription = 'Software Engineering at its worst.';
const md = new MarkdownIt();
const build = () => {
fs.rmSync('./build', {recursive: true, force: true});
fs.mkdirSync('./build');
const templateHtml = fs.readFileSync('./template.html', 'utf8');
const posts = [];
console.log('Building posts...');
const postsFiles = fs.readdirSync('./posts');
for (const postFile of postsFiles) {
const postMd = fs.readFileSync(`./posts/${postFile}`, 'utf8');
const {markdown, frontMatter} = parseFrontMatter(postMd);
const postHtml = md.render(markdown);
if (!frontMatter.published) continue;
const postSlug = postFile.replace('.md', '');
const title = `${frontMatter.title} | ${defaultTitle}`;
const post = templateHtml
.replace('{{content}}', `
<article>
<h1 class="post-title">{{post-title}}</h1>
<p class="post-info">
<span>
Published on
<time dateTime="{{date-raw}}" itemProp="datePublished">{{date}}</time>
</span>
</p>
{{content}}
</article>
`)
.replace('{{content}}', postHtml)
.replaceAll('{{title}}', title)
.replace('{{post-title}}', frontMatter.title)
.replace('{{date-raw}}', frontMatter.date)
.replace('{{date}}', formatDate(frontMatter.date))
.replaceAll('{{description}}', frontMatter.description);
fs.mkdirSync(`./build/${postSlug}`);
fs.writeFileSync(`./build/${postSlug}/index.html`, minifyHtml(post));
posts.push({
href: `/${postSlug}`,
title: frontMatter.title,
date: frontMatter.date,
description: frontMatter.description
});
}
const buildPostsList = () => {
return posts.reduce((postsHtml, post) => {
return postsHtml + `
<li class="article-item">
<article>
<a href="${post.href}">
${post.title}
</a>
<small><time>${formatDate(post.date)}</time></small>
<p>${post.description}</p>
</article>
</li>
`;
}, '');
}
console.log('Building pages...');
const pagesFiles = fs.readdirSync('./pages');
for (const pageFile of pagesFiles) {
const pageHtml = fs.readFileSync(`./pages/${pageFile}`, 'utf8');
const pageSlug = pageFile.replace('.html', '');
const page = templateHtml
.replace('{{content}}', pageHtml)
.replaceAll('{{title}}', defaultTitle)
.replaceAll('{{description}}', defaultDescription);
if (pageSlug === 'index') {
const index = page.replace('{{posts}}', buildPostsList());
fs.writeFileSync(`./build/index.html`, minifyHtml(index));
continue;
}
fs.mkdirSync(`./build/${pageSlug}`);
fs.writeFileSync(`./build/${pageSlug}/index.html`, minifyHtml(page));
}
console.log('Building styles...');
const result = sass.renderSync({
file: './style.scss',
outputStyle: 'compressed',
importer: (url, prev, done) => ({
contents: fs.readFileSync(url, 'utf8')
})
});
fs.writeFileSync('./build/style.css', result.css);
copyFiles('./fonts', './build');
copyFiles('./static', './build');
console.log('Build finished!');
}
build();
export default build;