-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
126 lines (116 loc) · 3.5 KB
/
main.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
const Metalsmith = require('metalsmith');
const collections = require('@metalsmith/collections');
const layouts = require('@metalsmith/layouts');
const inplace = require('metalsmith-in-place');
const markdown = require('@metalsmith/markdown');
const permalinks = require('@metalsmith/permalinks');
const remove = require('@metalsmith/remove');
const post_dates = require('./plugins/post_dates');
const post_permalink = require('./plugins/post_permalink');
const rss = require('./plugins/rss');
const sass = require('./plugins/sass');
const page_ref = require('./plugins/page_ref');
const toc = require('./plugins/toc');
const SRC = process.env['SRC'] || 'src';
const DEST = process.env['DEST'] || 'dist';
const URL = process.env['URL'] || 'https://basisproject.net';
const DRAFTS = process.env['DRAFTS'] === '1';
const NUNJUCK_OPTS = {
autoescape: false,
};
Metalsmith(__dirname)
.metadata({
site: {
url: URL,
base: '',
title: 'Basis',
description: 'Documentation and information about the Basis project',
asset_version: 11,
},
})
.source(`${SRC}/`)
.destination(`${DEST}/`)
.clean(true)
// we never process these directly, so no point in having them LOL
.use(remove([
'layouts/**/*',
'includes/**/*',
'css/includes/**/*',
]))
// deal with drafts
.use((files, metalsmith, done) => {
if(!DRAFTS) {
const fn = remove([
'drafts/**/*',
]);
return fn(files, metalsmith, done);
} else {
return done();
}
})
.use((files) => {
Object.keys(files).forEach((path) => {
if(path.indexOf('drafts/') === 0) {
const newpath = path.replace(/drafts\//, 'posts/');
files[newpath] = files[path];
delete files[path];
}
});
})
// -------------------------------------------------------------------------
// setup and metadata
// -------------------------------------------------------------------------
.use(page_ref())
.use(post_dates({
pattern: ['posts/**/*.md', 'posts/**/*.md.njk'],
}))
.use(post_permalink({
pattern: ['posts/**/*.md', 'posts/**/*.md.njk'],
}))
.use(collections({
posts: {
pattern: ['posts/**/*.md', 'posts/**/*.md.njk'],
sortBy: function(a, b) {
return new Date(b.date) - new Date(a.date);
},
}
}))
// -------------------------------------------------------------------------
// rendering
// -------------------------------------------------------------------------
// render posts first, so they can be templated into other pages (index/news)
.use(inplace({
engineOptions: NUNJUCK_OPTS,
pattern: ['posts/**/*.njk'],
}))
.use(markdown({gfm: true, tables: true}))
// now render everything else
.use(inplace({
engineOptions: NUNJUCK_OPTS,
pattern: ['**/*.njk'],
}))
.use(markdown({gfm: true, tables: true}))
// rss happens before layouts so we have access to post content WITHOUT all
// the HTML gibberish
.use(function(files, metalsmith, done) {
const meta = metalsmith._metadata;
return rss({feedOptions: {...meta.site, site_url: meta.site.url}, destination: 'feed.xml'})(files, metalsmith, done);
})
// table of contents (use `generate_toc: true` in front matter)
.use(toc())
// layouts are last in the rendering
.use(layouts({
engineOptions: NUNJUCK_OPTS,
pattern: ['**/*.html'],
directory: `${SRC}/layouts`,
}))
// -------------------------------------------------------------------------
// post-render
// -------------------------------------------------------------------------
.use(permalinks({
relative: false,
}))
.use(sass({pattern: ['css/**/*.scss']}))
.build((err) => {
if(err) throw err;
});