-
Notifications
You must be signed in to change notification settings - Fork 11
/
metalsmith.js
201 lines (175 loc) · 4.91 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const metalsmith = require('metalsmith');
const collectons = require('@metalsmith/collections');
const debug = require('metalsmith-debug');
const define = require('metalsmith-define');
const layouts = require('@metalsmith/layouts');
const markdown = require('metalsmith-markdownit');
const stylus = require('metalsmith-stylus');
const lunr = require('@pirxpilot/metalsmith-lunr-index');
const esbuild = require('@pirxpilot/metalsmith-esbuild');
const {
destination,
port = 3040,
preview = false
} = require('minimist')(process.argv.slice(2));
/* exported locals */
const locals = {
siteTitle: "America's Scenic Byways",
siteDescription: "Descriptions, maps and links to related information for over 800 America's most scenic roads.",
siteUrl: "http://scenicbyways.info",
email: "[email protected]",
furkotUrl: process.env.FURKOT_URL || "https://trips.furkot.com",
booksUrl: process.env.BOOKS_URL,
js: 'js',
colors: {
"All-American Road": "#AB0534",
"National Scenic Byway": "#003768",
"National Forest Scenic Byway": "#006E24",
"Parkway": "#955A32",
"BLM Back Country Byway": "#007BFF",
"Other Scenic Road": "#E3BE16"
},
package: require('./package.json'),
};
const collectionsData = {
byways: { pattern: ['byway/*'], refer: false, sortBy: 'name' },
states: { pattern: ['state/*'], refer: false, sortBy: 'name' }
};
function setUrls(files) {
Object.entries(files).forEach(function ([path, file]) {
const url = path.replace(/\.md$/, '.html');
file.url = `/${url}`;
file.slug = path.split('/').pop().split('.')[0];
});
}
function handleYaml(files) {
Object.keys(files).forEach(function (path) {
if (path.endsWith('.yaml')) {
let strippedPath = path.slice(0, -5);
files[strippedPath] = files[path];
delete files[path];
}
});
}
function handleJSON(files) {
Object.keys(files).forEach(function (path) {
if (path.endsWith('.json')) {
let strippedPath = path.slice(0, -5);
let file = files[path];
Object.assign(file, JSON.parse(file.contents.toString()));
file.contents = Buffer.allocUnsafe(0);
files[strippedPath + '.html'] = files[path];
delete files[path];
}
});
}
function adjustProperties(files) {
Object.values(files).forEach(function (file) {
if (file.path) {
delete file.path;
}
if (file.template) {
file.layout = file.template;
delete file.template;
}
if (file.layout) {
file.layout += '.pug';
}
if (file.name) {
// title is used by lunr
file.title = file.name;
}
});
}
function collectDesignations(files, metalsmith) {
function bywaysByDesignation(byways, names) {
const designation2list = names.reduce(function (result, name) {
result[name] = [];
return result;
}, Object.create(null));
// last by
const others = designation2list[names[names.length - 1]];
byways
.filter(byway => !byway['part of'])
.forEach(function (byway) {
const { designations = [] } = byway;
let addToOthers = true;
designations.forEach(function (dsg) {
let list = designation2list[dsg];
if (list) {
list.push(byway);
addToOthers = false;
}
});
if (addToOthers) {
others.push(byway);
}
});
return designation2list;
}
const metadata = metalsmith.metadata();
const names = [
'All-American Road',
'National Scenic Byway',
'Parkway',
'National Forest Scenic Byway',
'BLM Back Country Byway',
'Other Scenic Road'
];
metadata.designations = bywaysByDesignation(metadata.byways, names);
}
function collectById(files, metalsmith) {
const metadata = metalsmith.metadata();
function addBySlug(result, item) {
result[item.slug] = item;
return result;
}
metadata.statesById = metadata.states.reduce(addBySlug, Object.create(null));
metadata.bywaysById = metadata.byways.reduce(addBySlug, Object.create(null));
}
const ms = metalsmith(__dirname)
.env({ NODE_ENV: process.env.NODE_ENV })
.source('contents')
.destination(destination)
.clean(false)
.use(debug())
.use(define(locals))
.use(handleYaml)
.use(handleJSON)
.use(adjustProperties)
.use(setUrls)
.use(collectons(collectionsData))
.use(collectDesignations)
.use(collectById)
.use(stylus({
compress: true
}))
.use(esbuild({
entries: {
'scripts/index': 'contents/scripts/index.js',
'scripts/search': 'contents/scripts/search.js'
}
}))
.use(markdown({
html: true
}))
.use(lunr({
pattern: ['byway/*.html', 'about.html'],
refKey: 'url'
}))
.use(layouts({
directory: 'templates',
default: 'byway.pug',
pattern: ['**/*.html', '**/*.xml']
}));
if (preview) {
const serve = require('metalsmith-serve');
ms.use(serve({ port }));
}
/* global console */
ms.build(function (err) {
if (err) {
console.error(err);
process.exit(1);
}
});