-
Notifications
You must be signed in to change notification settings - Fork 0
/
metalsmith.js
197 lines (195 loc) · 5.24 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
import markdownRenderer from "./.metalsmith/markdownRenderer.js";
import nunjucksOptions from "./.metalsmith/nunjucksOptions.js";
import { __dirname } from "./.metalsmith/config.js";
import Metalsmith from "metalsmith";
import inplace from "@metalsmith/in-place";
import layouts from "@metalsmith/layouts";
import markdown from "@metalsmith/markdown";
import permalinks from "@metalsmith/permalinks";
import renamer from "metalsmith-renamer";
import collections from "@metalsmith/collections";
import jsBundle from "@metalsmith/js-bundle";
import sass from "@metalsmith/sass";
import sitemap from "metalsmith-sitemap";
import jsTransformerNunjucks from "jstransformer-nunjucks";
import packageInfo from "./package.json" with { type: "json" };
import { readFileSync } from "fs";
import { join } from "path";
import { glob } from "glob";
const t1 = performance.now();
Metalsmith(__dirname)
.source("./src")
.destination("./design-system")
.clean(true)
.env({
DEBUG: process.env.DEBUG,
})
.metadata({
sitename: "The National Archives Design System",
siteurl: "https://nationalarchives.github.io/design-system",
description:
"Design your service using National Archives styles, components and patterns",
generatorname: "Metalsmith",
generatorurl: "https://metalsmith.io/",
metalsmithVersion: packageInfo.devDependencies.metalsmith.replace(
/^[\^]/,
"",
),
tnaFrontendVersion: packageInfo.dependencies[
"@nationalarchives/frontend"
].replace(/^[\^]/, ""),
nodeVersion: process.version,
})
.use(async (files, metalsmith, done) => {
async function copyAssets(pattern, options) {
const assets = await glob(pattern, options);
for (const asset of assets) {
const input = join(options.cwd, asset);
const output = join(options.dest, asset);
files[output] = {
contents: readFileSync(input),
};
}
}
await Promise.all([
copyAssets("images/*.{svg,png,ico}", {
cwd: "node_modules/@nationalarchives/frontend/nationalarchives/assets/",
dest: "static/assets",
}),
copyAssets("fonts/*", {
cwd: "node_modules/@nationalarchives/frontend/nationalarchives/assets/",
dest: "static/assets",
}),
copyAssets("*.js", {
cwd: "node_modules/@nationalarchives/frontend/nationalarchives/",
dest: "static/scripts",
}),
copyAssets("*", {
cwd: "lib/static/",
dest: "static",
}),
]);
done();
})
.use(
renamer({
markdown: {
pattern: "**/*.md",
rename: (name) => {
return `${name}.njk`;
},
},
}),
)
.use(
inplace({
transform: jsTransformerNunjucks, // resolved
pattern: "**/*.njk",
engineOptions: nunjucksOptions,
}),
)
.use(
markdown({
renderer: markdownRenderer,
pedantic: false,
gfm: true,
tables: true,
breaks: false,
sanitize: false,
smartLists: true,
smartypants: false,
xhtml: false,
}),
)
.use(
collections({
home: {
pattern: "index.html",
refer: false,
},
top: {
pattern:
// "(get-started|styles|components|content|performance)/index.html",
"(get-started|styles|components|content)/index.html",
sortBy: "order",
refer: false,
},
front_page: {
// pattern: "(styles|components|content|performance)/index.html",
pattern: "(styles|components|content)/index.html",
sortBy: "order",
refer: false,
},
"get-started": {
pattern: "get-started/*/index.html",
filterBy: (file) => file.path !== "get-started/index.html",
sortBy: "order",
refer: false,
},
styles: {
pattern: "styles/*/index.html",
filterBy: (file) => file.path !== "styles/index.html",
refer: false,
},
components: {
pattern: "components/*/index.html",
filterBy: (file) => file.path !== "components/index.html",
refer: false,
},
content: {
pattern: "content/*/index.html",
filterBy: (file) => file.path !== "content/index.html",
refer: false,
},
performance: {
pattern: "performance/*/index.html",
filterBy: (file) => file.path !== "performance/index.html",
refer: false,
},
}),
)
.use(
permalinks({
relative: false,
}),
)
.use(
layouts({
engineOptions: nunjucksOptions,
}),
)
.use(
sass({
entries: {
"lib/index.scss": "css/index.css",
"lib/all.scss": "css/all.css",
"lib/print.scss": "css/print.css",
"lib/fa.scss": "css/fa.css",
"lib/ie.scss": "css/ie.css",
},
}),
)
.use(
jsBundle({
minify: true,
sourcemap: true,
drop: ["debugger"],
entries: {
index: "lib/index.js",
collection: "lib/collection.js",
sidebar: "lib/sidebar.js",
},
}),
)
.use(
sitemap({
hostname: "https://nationalarchives.github.io/design-system",
omitIndex: true,
}),
)
.build((err) => {
if (err) throw err;
console.log(
`Build success in ${((performance.now() - t1) / 1000).toFixed(1)}s`,
);
});