|
| 1 | +const { DateTime } = require('luxon'); |
| 2 | +const fs = require('fs'); |
| 3 | +const pluginRss = require('@11ty/eleventy-plugin-rss'); |
| 4 | +const pluginNavigation = require('@11ty/eleventy-navigation'); |
| 5 | +const markdownIt = require('markdown-it'); |
| 6 | +const markdownItAnchor = require('markdown-it-anchor'); |
| 7 | +const yaml = require("js-yaml"); |
| 8 | +const svgSprite = require("eleventy-plugin-svg-sprite"); |
| 9 | +const { imageShortcode, imageWithClassShortcode } = require('./config'); |
| 10 | + |
| 11 | +module.exports = function (config) { |
| 12 | + // Set pathPrefix for site |
| 13 | + let pathPrefix = '/'; |
| 14 | + |
| 15 | + // Copy the `admin` folders to the output |
| 16 | + config.addPassthroughCopy('admin'); |
| 17 | + |
| 18 | + // Copy USWDS init JS so we can load it in HEAD to prevent banner flashing |
| 19 | + config.addPassthroughCopy({'./node_modules/@uswds/uswds/dist/js/uswds-init.js': 'assets/js/uswds-init.js'}); |
| 20 | + |
| 21 | + // Add plugins |
| 22 | + config.addPlugin(pluginRss); |
| 23 | + config.addPlugin(pluginNavigation); |
| 24 | + |
| 25 | + //// SVG Sprite Plugin for USWDS USWDS icons |
| 26 | + config.addPlugin(svgSprite, { |
| 27 | + path: "./node_modules/@uswds/uswds/dist/img/uswds-icons", |
| 28 | + svgSpriteShortcode: 'uswds_icons_sprite', |
| 29 | + svgShortcode: 'uswds_icons' |
| 30 | + }); |
| 31 | + |
| 32 | + //// SVG Sprite Plugin for USWDS USA icons |
| 33 | + config.addPlugin(svgSprite, { |
| 34 | + path: "./node_modules/@uswds/uswds/dist/img/usa-icons", |
| 35 | + svgSpriteShortcode: 'usa_icons_sprite', |
| 36 | + svgShortcode: 'usa_icons' |
| 37 | + }); |
| 38 | + |
| 39 | + // Allow yaml to be used in the _data dir |
| 40 | + config.addDataExtension("yaml", contents => yaml.load(contents)); |
| 41 | + |
| 42 | + config.addFilter('readableDate', (dateObj) => { |
| 43 | + return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat( |
| 44 | + 'dd LLL yyyy' |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string |
| 49 | + config.addFilter('htmlDateString', (dateObj) => { |
| 50 | + return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat('yyyy-LL-dd'); |
| 51 | + }); |
| 52 | + |
| 53 | + // Get the first `n` elements of a collection. |
| 54 | + config.addFilter('head', (array, n) => { |
| 55 | + if (!Array.isArray(array) || array.length === 0) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + if (n < 0) { |
| 59 | + return array.slice(n); |
| 60 | + } |
| 61 | + |
| 62 | + return array.slice(0, n); |
| 63 | + }); |
| 64 | + |
| 65 | + // Return the smallest number argument |
| 66 | + config.addFilter('min', (...numbers) => { |
| 67 | + return Math.min.apply(null, numbers); |
| 68 | + }); |
| 69 | + |
| 70 | + function filterTagList(tags) { |
| 71 | + return (tags || []).filter( |
| 72 | + (tag) => ['all', 'nav', 'post', 'posts'].indexOf(tag) === -1 |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + config.addFilter('filterTagList', filterTagList); |
| 77 | + |
| 78 | + // Create an array of all tags |
| 79 | + config.addCollection('tagList', function (collection) { |
| 80 | + let tagSet = new Set(); |
| 81 | + collection.getAll().forEach((item) => { |
| 82 | + (item.data.tags || []).forEach((tag) => tagSet.add(tag)); |
| 83 | + }); |
| 84 | + |
| 85 | + return filterTagList([...tagSet]); |
| 86 | + }); |
| 87 | + |
| 88 | + // Customize Markdown library and settings: |
| 89 | + let markdownLibrary = markdownIt({ |
| 90 | + html: true, |
| 91 | + breaks: true, |
| 92 | + linkify: true, |
| 93 | + }).use(markdownItAnchor, { |
| 94 | + permalink: markdownItAnchor.permalink.ariaHidden({ |
| 95 | + placement: 'after', |
| 96 | + class: 'direct-link', |
| 97 | + symbol: '#', |
| 98 | + level: [1, 2, 3, 4], |
| 99 | + }), |
| 100 | + slugify: config.getFilter('slug'), |
| 101 | + }); |
| 102 | + config.setLibrary('md', markdownLibrary); |
| 103 | + |
| 104 | + // Override Browsersync defaults (used only with --serve) |
| 105 | + config.setBrowserSyncConfig({ |
| 106 | + callbacks: { |
| 107 | + ready: function (err, browserSync) { |
| 108 | + const content_404 = fs.readFileSync('_site/404/index.html'); |
| 109 | + |
| 110 | + browserSync.addMiddleware('*', (req, res) => { |
| 111 | + // Provides the 404 content without redirect. |
| 112 | + res.writeHead(404, { 'Content-Type': 'text/html; charset=UTF-8' }); |
| 113 | + res.write(content_404); |
| 114 | + res.end(); |
| 115 | + }); |
| 116 | + }, |
| 117 | + }, |
| 118 | + ui: false, |
| 119 | + ghostMode: false, |
| 120 | + }); |
| 121 | + |
| 122 | + // Set image shortcodes |
| 123 | + config.addLiquidShortcode('image', imageShortcode); |
| 124 | + config.addLiquidShortcode('image_with_class', imageWithClassShortcode); |
| 125 | + config.addLiquidShortcode("uswds_icon", function (name) { |
| 126 | + return ` |
| 127 | + <svg class="usa-icon" aria-hidden="true" role="img"> |
| 128 | + <use xlink:href="#svg-${name}"></use> |
| 129 | + </svg>`; |
| 130 | + }); |
| 131 | + |
| 132 | + // If BASEURL env variable exists, update pathPrefix to the BASEURL |
| 133 | + if (process.env.BASEURL) { |
| 134 | + pathPrefix = process.env.BASEURL |
| 135 | + } |
| 136 | + |
| 137 | + return { |
| 138 | + // Control which files Eleventy will process |
| 139 | + // e.g.: *.md, *.njk, *.html, *.liquid |
| 140 | + templateFormats: ['md', 'njk', 'html', 'liquid'], |
| 141 | + |
| 142 | + // Pre-process *.md files with: (default: `liquid`) |
| 143 | + // Other template engines are available |
| 144 | + // See https://www.11ty.dev/docs/languages/ for other engines. |
| 145 | + markdownTemplateEngine: 'liquid', |
| 146 | + |
| 147 | + // Pre-process *.html files with: (default: `liquid`) |
| 148 | + // Other template engines are available |
| 149 | + // See https://www.11ty.dev/docs/languages/ for other engines. |
| 150 | + htmlTemplateEngine: 'liquid', |
| 151 | + |
| 152 | + // ----------------------------------------------------------------- |
| 153 | + // If your site deploys to a subdirectory, change `pathPrefix`. |
| 154 | + // Don’t worry about leading and trailing slashes, we normalize these. |
| 155 | + |
| 156 | + // If you don’t have a subdirectory, use "" or "/" (they do the same thing) |
| 157 | + // This is only used for link URLs (it does not affect your file structure) |
| 158 | + // Best paired with the `url` filter: https://www.11ty.dev/docs/filters/url/ |
| 159 | + |
| 160 | + // You can also pass this in on the command line using `--pathprefix` |
| 161 | + |
| 162 | + // Optional (default is shown) |
| 163 | + pathPrefix: pathPrefix, |
| 164 | + // ----------------------------------------------------------------- |
| 165 | + |
| 166 | + // These are all optional (defaults are shown): |
| 167 | + dir: { |
| 168 | + input: '.', |
| 169 | + includes: '_includes', |
| 170 | + data: '_data', |
| 171 | + output: '_site', |
| 172 | + }, |
| 173 | + }; |
| 174 | +}; |
0 commit comments