-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetalsmith.js
More file actions
202 lines (198 loc) · 5.45 KB
/
Copy pathmetalsmith.js
File metadata and controls
202 lines (198 loc) · 5.45 KB
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
202
import { readFileSync } from "fs";
import { join, relative } from "path";
import collections from "@metalsmith/collections";
import inplace from "@metalsmith/in-place";
import jsBundle from "@metalsmith/js-bundle";
import layouts from "@metalsmith/layouts";
import markdown from "@metalsmith/markdown";
import permalinks from "@metalsmith/permalinks";
import sass from "@metalsmith/sass";
import { glob } from "glob";
import jsTransformerNunjucks from "jstransformer-nunjucks";
import Metalsmith from "metalsmith";
import renamer from "metalsmith-renamer";
import sitemap from "metalsmith-sitemap";
import { __dirname } from "./.metalsmith/config.js";
import markdownRenderer from "./.metalsmith/markdownRenderer.js";
import nunjucksOptions from "./.metalsmith/nunjucksOptions.js";
import packageInfo from "./package-lock.json" with { type: "json" };
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://design-system.nationalarchives.gov.uk/",
defaultDescription:
"Design your service using National Archives styles, components and patterns",
generatorname: "Metalsmith",
generatorurl: "https://metalsmith.io/",
tnaFrontendVersion:
packageInfo.packages["node_modules/@nationalarchives/frontend"].version,
nodeVersion: process.version,
})
.use(async (files, metalsmith, done) => {
/* eslint-disable func-style */
async function copyAssets(pattern, options) {
const assets = await glob(pattern, options);
for (const asset of assets) {
const input = join(options.cwd, asset);
if (relative(options.cwd, input).startsWith("..")) {
throw new Error(`Path traversal detected: ${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?(.map)", {
cwd: "node_modules/@nationalarchives/frontend/nationalarchives/",
dest: "static/scripts",
}),
copyAssets("*", {
cwd: "lib/static/",
dest: "",
}),
]);
done();
})
.use(
renamer({
markdown: {
pattern: "**/*.md",
rename: (name) => `${name}.njk`,
},
}),
)
.use(
inplace({
transform: jsTransformerNunjucks,
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,
},
frontPage: {
// 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({
match: ["**/*.html", "!google9f936c84a60090b4.html"],
relative: false,
}),
)
.use(
layouts({
engineOptions: nunjucksOptions,
}),
)
.use(
sass({
quietDeps: true,
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: {
code: "lib/code.js",
index: "lib/index.js",
sidebar: "lib/sidebar.js",
},
}),
)
.use(
sitemap({
hostname: "https://design-system.nationalarchives.gov.uk/",
omitIndex: true,
pattern: ["**/*.html", "!google9f936c84a60090b4.html"],
}),
)
.build((err) => {
if (err) {
throw err;
}
/* eslint-disable no-console */
console.log(
`Build success in ${((performance.now() - t1) / 1000).toFixed(1)}s`,
);
});