-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-blog.js
38 lines (34 loc) · 1.06 KB
/
build-blog.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
const pug = require('pug');
const pugJSON = require('./src/pug.json');
const fs = require('fs');
const { marked } = require('marked');
const hljs = require('highlight.js');
const blogArticleTemplate = pug.compileFile('./src/partials/blog-item.pug', { pretty: true });
// code highlighting
marked.setOptions({
highlight: function (code, language) {
if (!language) {
return hljs.highlightAuto(code).value;
}
return hljs.highlight(code, { language }).value;
},
});
// lazy loading images
marked.use({
extensions: [
{
name: 'image',
level: 'inline',
renderer(token) {
const html = this.parser.renderer.image(token.href, token.title, token.text);
return html.replace(/^<img /, '<img loading="lazy" ');
},
},
],
});
for (let article of pugJSON.data.blogArticles) {
article.content = marked.parse(fs.readFileSync(`src/views/blog/posts/${article.slug}.md`).toString());
article.url = '/blog/' + article.slug;
const html = blogArticleTemplate(article);
fs.writeFileSync(`dist/blog/${article.slug}.html`, html);
}