-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetalsmith.js
70 lines (65 loc) · 2.03 KB
/
metalsmith.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
const argv = require("yargs").argv;
const htmlMinifier = require("metalsmith-html-minifier");
const jsdom = require("jsdom");
const katex = require("katex");
const Metalsmith = require("metalsmith");
const minimatch = require("minimatch");
const postcss = require("metalsmith-postcss");
const sass = require("metalsmith-sass");
const superstatic = require("superstatic").server;
const { JSDOM } = jsdom;
const metalsmith = new Metalsmith(__dirname);
const server = superstatic({
port: Number(argv.port || 8000),
cwd: __dirname,
config: {
public: "./build"
}
});
metalsmith.source("website").clean(false)
.use(sass({
includePaths: [ "node_modules/" ],
outputDir: "css/",
outputStyle: "expanded"
}))
.use(postcss({
plugins: {
autoprefixer: {},
cssnano: {},
},
}))
.use((files, _, done) => {
setImmediate(done);
Object.keys(files)
.filter(minimatch.filter("*.html", {"matchBase": true}))
.forEach((file) => {
const data = files[file];
const dom = new JSDOM(data.contents.toString());
const {window: {document}} = dom;
Array.from(document.querySelectorAll("[data-katex]"))
.forEach((el) => {
el.innerHTML = katex.renderToString(el.textContent);
el.parentNode.replaceChild(el.firstChild, el);
});
data.contents = new Buffer(dom.serialize(), "utf8");
});
})
.use(htmlMinifier())
.build((err, files) => {
if (err) {
console.error(err);
return;
}
if (!argv.serve) {
return;
}
server.listen(function (e) {
if (e) {
console.error("Server error");
console.error(e);
return;
}
const {port} = this.address();
console.log(`Server started on port ${port}`);
});
});