-
Notifications
You must be signed in to change notification settings - Fork 253
/
build.js
140 lines (123 loc) · 3.59 KB
/
build.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
var Metalsmith = require('metalsmith'),
drafts = require('metalsmith-drafts'),
layouts = require('metalsmith-layouts'),
markdown = require('metalsmith-markdown'),
assets = require('metalsmith-assets'),
collections = require('metalsmith-collections'),
autotoc = require('metalsmith-autotoc'),
argv = require('minimist')(process.argv);
var serve = require('metalsmith-serve');
var watch = require('metalsmith-watch');
var sass = require('metalsmith-sass');
const hljs = require('highlight.js');
const marked = require('marked');
var renderer = new marked.Renderer();
renderer.code = function (code, infostring, escaped) {
/** Copied from the default implementation, with the following changes.
* 1. Find a series of 4 spaces and convert it into tabs
* 2. Add "hljs" to the code class. I don't get why, but metalsmith-metallic was doing this, so I copied the pattern.
* 3. Remove this.options.langPrefix, because metalsmith-metallic didn't have this.
*/
code = code.replace(/ {4}/g, '\t'); // Convert spaces back into tabs
var lang = (infostring || '').match(/\S*/)[0];
if (this.options.highlight) {
var out = this.options.highlight(code, lang);
if (out != null && out !== code) {
escaped = true;
code = out;
}
}
if (!lang) {
return '<pre><code>' + (escaped ? code : escape(code, true)) + '</code></pre>';
}
return (
'<pre><code class="hljs ' +
// this.options.langPrefix +
escape(lang, true) +
'">' +
(escaped ? code : escape(code, true)) +
'</code></pre>\n'
);
};
build(function () {
console.log('Done building.');
});
function build(callback) {
const smith = Metalsmith(__dirname)
// This is the source directory
.source('./src')
// This is where I want to build my files to
.destination('./docs')
// Clean the build directory before running any plugins
.clean(true)
// Use the drafts plugin
.use(drafts())
// Use Github Flavored Markdown for content
.use(
markdown({
gfm: true,
tables: true,
highlight: function (code, lang) {
// TODO what if there is no lang? highlightAuto..
return hljs.highlight(lang, code).value;
},
renderer,
}),
)
// Generate a table of contents JSON for every heading.
.use(
autotoc({
selector: 'h1, h2, h3, h4, h5, h6',
headerIdPrefix: 'subhead',
}),
)
// Group my content into 4 distinct collections. These collection names
// are defined as `collection: <name>` inside the markdown YAML.
// .use(
// collections({
// "Get Started": { sortBy: "date" },
// Tutorials: { sortBy: "date" },
// "User Authentication": { sortBy: "date" },
// "Building with React & Flux": { sortBy: "date" },
// })
// )
// Use handlebars as layout engine.
//.use(layouts('handlebars'))
.use(layouts({ engine: 'ejs' }))
.use(
sass({
outputStyle: 'expanded',
}),
)
// Use the assets plugin to specify where assets are stored
.use(
assets({
source: './assets',
destination: './assets',
}),
);
if (argv.dev) {
smith
.use(
serve({
port: 8001,
verbose: true,
}),
)
.use(
watch({
paths: {
'src/*.md': true,
'src/*': '**/**',
'layouts/*': '**/*',
'assets/*.css': '**/*',
},
}),
);
}
smith.build(function (err) {
var message = err ? err : 'Build complete';
console.log(message);
callback();
});
}