-
Notifications
You must be signed in to change notification settings - Fork 23
/
.eleventy.js
350 lines (297 loc) · 13.3 KB
/
.eleventy.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
const fs = require('fs');
const { EleventyRenderPlugin } = require("@11ty/eleventy");
const pluginRss = require('@11ty/eleventy-plugin-rss');
const pluginNavigation = require('@11ty/eleventy-navigation');
const markdownIt = require('markdown-it');
const markdownItAttrs = require('markdown-it-attrs');
const markdownItAnchor = require('markdown-it-anchor');
const markdownItFootnote = require('markdown-it-footnote');
const { readableDate, htmlDateString, head, min, filterTagList } = require("./config/filters");
const { headingLinks } = require("./config/headingLinks");
const { contrastRatio, humanReadableContrastRatio } = require("./config/wcagColorContrast");
const privateLinks = require ('./config/privateLinksList.js');
const svgSprite = require("eleventy-plugin-svg-sprite");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const yaml = require("js-yaml");
const { imageShortcode, imageWithClassShortcode, imageUrl } = require('./config');
const siteData = yaml.load(fs.readFileSync('./_data/site.yaml', 'utf8'));
module.exports = function (config) {
// Set pathPrefix for site
let pathPrefix = '/';
config.addPlugin(EleventyRenderPlugin);
// Copy the `admin` folders to the output
config.addPassthroughCopy('admin');
// Copy the robots.txt file to the output
config.addPassthroughCopy('robots.txt');
// Copy USWDS init JS so we can load it in HEAD to prevent banner flashing
config.addPassthroughCopy({'./node_modules/@uswds/uswds/dist/js/uswds-init.js': 'assets/js/uswds-init.js'});
// Specific scripts to guides
config.addPassthroughCopy("./assets/_common/js/*");
config.addPassthroughCopy("./assets/**/js/*");
config.addPassthroughCopy({'./assets/_common/_img/favicons/favicon.ico': './favicon.ico' });
config.addPassthroughCopy({'./assets/_common/_img/favicons': './img/favicons' });
// Set download paths
// Place files for download in assets/{guide}/dist/{filename.ext}
config.addPassthroughCopy("./assets/**/dist/*");
// methods pdfs
config.addPassthroughCopy({ "./content/methods/assets/downloads/": "./methods/assets/downloads/" });
// Add plugins
config.addPlugin(pluginRss);
config.addPlugin(pluginNavigation);
//// SVG Sprite Plugin for USWDS USWDS icons
config.addPlugin(svgSprite, {
path: "./node_modules/@uswds/uswds/dist/img/uswds-icons",
svgSpriteShortcode: 'uswds_icons_sprite',
svgShortcode: 'uswds_icons'
});
//// SVG Sprite Plugin for USWDS USA icons
config.addPlugin(svgSprite, {
path: "./node_modules/@uswds/uswds/dist/img/usa-icons",
svgSpriteShortcode: 'usa_icons_sprite',
svgShortcode: 'usa_icons'
});
// Plugin to style code blocks
config.addPlugin(syntaxHighlight);
// Allow yaml to be used in the _data dir
config.addDataExtension("yaml", contents => yaml.load(contents));
// Filters
// Add filter function defintions to config/filters.js, then add the functions
// to the import statement above and define like the other filters.
config.addFilter('readableDate', readableDate);
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
config.addFilter('htmlDateString', htmlDateString);
config.addFilter('head', head); // Get the first `n` elements of a collection.
config.addFilter('min', min); // Return the smallest number argument
config.addFilter('filterTagList', filterTagList);
// Color contrast checkers for the color matrix in the Brand guide
config.addFilter('contrastRatio', contrastRatio);
config.addFilter('humanReadableContrastRatio', humanReadableContrastRatio);
// Create absolute urls
config.addFilter('asAbsoluteUrl', (relativeUrl) => {
const host = siteData.host;
return new URL(relativeUrl, host).href;
});
config.addFilter("makeUppercase", (value) => {
return value.toUpperCase();
});
config.addFilter("capitalize", (value) =>{
return value.charAt(0).toUpperCase() + value.slice(1);
});
// Get the URL for an image given the relative path to its source
config.addFilter('imageUrl', (imageSource) => {
return imageUrl(imageSource);
});
// Create an array of all tags
config.addCollection('tagList', function (collection) {
let tagSet = new Set();
collection.getAll().forEach((item) => {
(item.data.tags || []).forEach((tag) => tagSet.add(tag));
});
return filterTagList([...tagSet]);
});
config.addCollection('methods', (collectionApi) => {
/* sort all methods in alpha order */
return collectionApi.getFilteredByTag("methods").sort((a, b) => {
if(a.data.title < b.data.title) { return -1; }
if(a.data.title > b.data.title) { return 1; }
return 0;
});
});
// Customize Markdown library and settings
let markdownLibrary = markdownIt({
html: true,
breaks: false,
linkify: false,
}).use(markdownItAnchor, {
permalink: headingLinks,
slugify: config.getFilter('slugify'),
}).use(markdownItAttrs).use(markdownItFootnote);
config.setLibrary('md', markdownLibrary);
// Override Footnote opener
markdownLibrary.renderer.rules.footnote_block_open = () => (
'<section class="footnotes">\n' +
'<ol class="footnotes-list">\n'
);
// Add icons for links with locked resources and external links
// https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md
// Token methods: https://github.com/markdown-it/markdown-it/blob/master/lib/token.js#L125
const openDefaultRender = markdownLibrary.renderer.rules.link_open ||
function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
markdownLibrary.renderer.rules.link_open = (tokens, idx, options, env, self) => {
const token = tokens[idx];
let prefixIcon = '';
if (privateLinks.some((link) => token.attrGet('href').indexOf(link) >= 0)) {
prefixIcon = '<span class="usa-sr-only"> 18F only, </span>' +
'<svg class="usa-icon margin-top-2px margin-right-2px top-2px" ' +
'aria-hidden="true" role="img">' +
'<use xlink:href="#svg-lock_outline"></use>' +
'</svg>'
}
// Check for external URLs. External means any site that is not a federal .gov url
// This check can't detect state/local .gov domains. Those will need to be
// manually adjusted
const baseURL = new URL('https://guides.18f.gov/');
const hrefValue = token.attrGet('href');
if (!(new URL(hrefValue, baseURL).hostname.endsWith(".gov"))) {
// Add the external link class if it hasn't been added yet
if (!(token.attrGet('class')) || !(token.attrGet('class').includes('usa-link--external'))) {
token.attrJoin('class', 'usa-link usa-link--external');
}
// Set rel=noreferrer if it hasn't been set yet
if (!(token.attrGet('rel')) || !(token.attrGet('rel').includes('noreferrer'))) {
token.attrJoin('rel', 'noreferrer');
}
}
return openDefaultRender(tokens, idx, options, env, self) + `${prefixIcon}`;
};
const defaultHtmlBlockRender = markdownLibrary.renderer.rules.html_block ||
function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
markdownLibrary.renderer.rules.html_block = (tokens, idx, options, env, self) => {
const token = tokens[idx];
let content = token.content;
// Capture the class portion of the element if it exists so it can be interacted with later
// https://regexr.com/7udrd
const hrefRE = /<a href=\"[^"]*\" class=\"([^"]*)\">|href=\"([^"]*)\"/g;
const htmlIncludesLinks = content.includes('http') && hrefRE.test(token.content);
if (htmlIncludesLinks) {
const matches = content.match(hrefRE);
matches.forEach(anchorElement => {
if (!anchorElement.includes('.gov')) {
if (!anchorElement.includes('class=')) {
if (!anchorElement.includes('usa-link--external')) {
// Since no class is present, we can safely just append our classes after the href property
const newUrl = anchorElement + ' class="usa-link usa-link--external"';
content = content.replace(anchorElement, newUrl);
tokens[idx].content = content;
}
} else {
// Handle URLs with classes already present
const classRE = /class="([^"]*)"/;
const [classString, oldClassList] = anchorElement.match(classRE);
const newClassList = oldClassList + ' usa-link usa-link--external';
// If someone uses the class property but doesn't actually put any classes in it, the class list will be empty
if (classString === 'class=""') {
content = content.replace(classString, 'class="usa-link usa-link--external"');
} else {
content = content.replace(oldClassList, newClassList);
}
tokens[idx].content = content;
}
}
});
}
return defaultHtmlBlockRender(tokens, idx, options, env, self);
}
// Also need to add icon links to any html style links
const inlineHTMLDefaultRender = markdownLibrary.renderer.rules.html_inline ||
function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
const linkOpenRE = /^<a[>\s]/i;
markdownLibrary.renderer.rules.html_inline = (tokens, idx, options, env, self) => {
const token=tokens[idx];
if (linkOpenRE.test(token.content) && token.content.includes('http')) {
let content = token.content;
//Add private link icon
const hrefRE = /href=\"([^"]*)/;
// get the matching capture group
const contentUrl = content.match(hrefRE)[1];
if (privateLinks.some((privateLink) => contentUrl.indexOf(privateLink) >= 0)) {
const prefixIcon = '<span class="usa-sr-only"> 18F only, </span>' +
'<svg class="usa-icon margin-top-2px margin-right-2px top-2px" ' +
'aria-hidden="true" role="img">' +
'<use xlink:href="#svg-lock_outline"></use>' +
'</svg>'
content = content.replace('>', `> ${prefixIcon}`);
tokens[idx].content = content;
}
// Add external links, as definied above
if (!content.includes('.gov')) {
if (content.includes('class=')) {
if (!content.includes('usa-link--external')) {
content = content.replace('class="', 'class="usa-link usa-link--external ');
tokens[idx].content = content;
}
}
else {
content = content.replace('>', ' class="usa-link usa-link--external">');
tokens[idx].content = content;
}
if (content.includes('rel=')) {
if (!content.include('noreferrer')) {
content = content.replace('rel=', 'rel="noreferrer ">');
tokens[idx].content = content;
}
}
else {
content = content.replace('>', ' rel="noreferrer">');
tokens[idx].content = content;
}
}
}
return inlineHTMLDefaultRender(tokens, idx, options, env, self);
}
// Override Browsersync defaults (used only with --serve)
config.setBrowserSyncConfig({
callbacks: {
ready: function (err, browserSync) {
const content_404 = fs.readFileSync('_site/404/index.html');
browserSync.addMiddleware('*', (req, res) => {
// Provides the 404 content without redirect.
res.writeHead(404, { 'Content-Type': 'text/html; charset=UTF-8' });
res.write(content_404);
res.end();
});
},
},
ui: false,
ghostMode: false,
});
// Set image shortcodes
config.addLiquidShortcode('image', imageShortcode);
config.addLiquidShortcode('image_with_class', imageWithClassShortcode);
config.addLiquidShortcode("uswds_icon", function (name) {
return `
<svg class="usa-icon" aria-hidden="true" role="img">
<use xlink:href="#svg-${name}"></use>
</svg>`;
});
// If BASEURL env variable exists, update pathPrefix to the BASEURL
if (process.env.BASEURL) {
pathPrefix = process.env.BASEURL
}
return {
// Control which files Eleventy will process
// e.g.: *.md, *.njk, *.html, *.liquid
templateFormats: ['md', 'njk', 'html', 'liquid'],
// Pre-process *.md files with: (default: `liquid`)
// Other template engines are available
// See https://www.11ty.dev/docs/languages/ for other engines.
markdownTemplateEngine: 'liquid',
// Pre-process *.html files with: (default: `liquid`)
// Other template engines are available
// See https://www.11ty.dev/docs/languages/ for other engines.
htmlTemplateEngine: 'liquid',
// -----------------------------------------------------------------
// If your site deploys to a subdirectory, change `pathPrefix`.
// Don’t worry about leading and trailing slashes, we normalize these.
// If you don’t have a subdirectory, use "" or "/" (they do the same thing)
// This is only used for link URLs (it does not affect your file structure)
// Best paired with the `url` filter: https://www.11ty.dev/docs/filters/url/
// You can also pass this in on the command line using `--pathprefix`
// Optional (default is shown)
pathPrefix: pathPrefix,
// -----------------------------------------------------------------
// These are all optional (defaults are shown):
dir: {
input: '.',
includes: '_includes',
data: '_data',
output: '_site',
},
};
};