|
| 1 | +const lodashGet = require("lodash/get"); |
| 2 | +const yaml = require("js-yaml"); |
| 3 | +const { EleventyServerlessBundlerPlugin } = require("@11ty/eleventy"); |
| 4 | + |
| 5 | +module.exports = function (eleventyConfig) { |
| 6 | + // Support yaml data files |
| 7 | + eleventyConfig.addDataExtension("yaml", contents => yaml.safeLoad(contents)) |
| 8 | + |
| 9 | + eleventyConfig.addWatchTarget("src/site/survey/**/*.js"); |
| 10 | + |
| 11 | + // ODB for Discord events |
| 12 | + eleventyConfig.addPlugin(EleventyServerlessBundlerPlugin, { |
| 13 | + name: "community", |
| 14 | + functionsDir: './netlify/functions', |
| 15 | + inputDir: "./src/site", |
| 16 | + redirects: "netlify-toml-builders", |
| 17 | + copy: ["algolia.config.js"] |
| 18 | + }); |
| 19 | + |
| 20 | + |
| 21 | + // pass images directly through to the output |
| 22 | + eleventyConfig.addPassthroughCopy("src/site/img"); |
| 23 | + eleventyConfig.addPassthroughCopy({ |
| 24 | + "src/js": "js", |
| 25 | + "node_modules/@zachleat/filter-container/*.js": "js", |
| 26 | + "src/css": "css", |
| 27 | + }); |
| 28 | + |
| 29 | + // Date helper |
| 30 | + const { DateTime } = require('luxon'); |
| 31 | + eleventyConfig.addFilter('formatDate', (dateObj, formatStr) => { |
| 32 | + // convert any date strings to read dates |
| 33 | + if (typeof (dateObj) == "string") { |
| 34 | + dateObj = new Date(dateObj); |
| 35 | + } |
| 36 | + const format = formatStr ? formatStr : 'LLLL d, y'; |
| 37 | + return DateTime.fromJSDate(dateObj, { |
| 38 | + zone: 'utc' |
| 39 | + }).toFormat(format); |
| 40 | + }); |
| 41 | + |
| 42 | + // A handy markdown shortcode for blocks of markdown |
| 43 | + // coming from our data sources |
| 44 | + const markdownIt = require('markdown-it'); |
| 45 | + const md = new markdownIt({ |
| 46 | + html: true |
| 47 | + }); |
| 48 | + eleventyConfig.addPairedShortcode('markdown', (content) => { |
| 49 | + return md.render(content); |
| 50 | + }); |
| 51 | + |
| 52 | + |
| 53 | + eleventyConfig.addFilter('convertFromEpoc', (time) => { |
| 54 | + let date = new Date(0); |
| 55 | + return date.setUTCSeconds(time); |
| 56 | + }); |
| 57 | + |
| 58 | + |
| 59 | + eleventyConfig.addCollection("resources", function (collectionApi) { |
| 60 | + return collectionApi.getFilteredByGlob("src/site/resources/*.md"); |
| 61 | + }); |
| 62 | + |
| 63 | + |
| 64 | + // Filter a collection based on items flagged as "featured" |
| 65 | + eleventyConfig.addFilter('featured', (items) => { |
| 66 | + return items.filter(item => item.data.featured); |
| 67 | + }); |
| 68 | + |
| 69 | + // Filter a collection based on the value of a data attribute |
| 70 | + eleventyConfig.addFilter('dataAttr', (items, attr, value) => { |
| 71 | + return items.filter(item => item.data[attr] == value); |
| 72 | + }); |
| 73 | + |
| 74 | + // Filter a collection based on the value of a data attribute |
| 75 | + eleventyConfig.addFilter('whereData', (items, expression) => { |
| 76 | + let key = expression.split('=')[0]; |
| 77 | + let val = expression.split('=')[1]; |
| 78 | + return items.filter(item => item.data[key] == val); |
| 79 | + }); |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + // filter a data array based on the value of a property |
| 85 | + eleventyConfig.addFilter('select', (array, clause) => { |
| 86 | + if (clause.indexOf("=") > -1) { |
| 87 | + const property = clause.split("=")[0]; |
| 88 | + const value = clause.split("=")[1]; |
| 89 | + return array.filter(item => lodashGet(item, property).includes(value)); |
| 90 | + } else { |
| 91 | + return array.map(item => lodashGet(item, clause)); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + eleventyConfig.addFilter('flatten', (array) => { |
| 96 | + let results = []; |
| 97 | + for (let result of array) { |
| 98 | + if (result) { |
| 99 | + if (Array.isArray(result)) { |
| 100 | + results = [...results, ...result]; |
| 101 | + } else { |
| 102 | + results.push(result); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return results; |
| 107 | + }); |
| 108 | + |
| 109 | + eleventyConfig.addFilter('unique', (array) => { |
| 110 | + let caseInsensitive = {}; |
| 111 | + for (let val of array) { |
| 112 | + if (typeof val === "string") { |
| 113 | + caseInsensitive[val.toLowerCase()] = val; |
| 114 | + } |
| 115 | + } |
| 116 | + return Object.values(caseInsensitive); |
| 117 | + }); |
| 118 | + |
| 119 | + // Get a random selection of items from an array |
| 120 | + eleventyConfig.addFilter('luckydip', (array, count) => { |
| 121 | + if (count > array.length) { |
| 122 | + count = array.length; |
| 123 | + } |
| 124 | + const shuffled = array.sort(() => 0.5 - Math.random()); |
| 125 | + return shuffled.slice(0, count); |
| 126 | + }); |
| 127 | + |
| 128 | + // Convert an associative array into an indexable, iterable array |
| 129 | + eleventyConfig.addFilter('iterable', (obj) => { |
| 130 | + var iterableArray = new Array(); |
| 131 | + for (var item in obj) { |
| 132 | + iterableArray.push(obj[item]); |
| 133 | + } |
| 134 | + return iterableArray; |
| 135 | + }); |
| 136 | + |
| 137 | + // format a url for display |
| 138 | + eleventyConfig.addFilter('domain', (str) => { |
| 139 | + var url = new URL(str); |
| 140 | + return url.hostname; |
| 141 | + }); |
| 142 | + |
| 143 | + |
| 144 | + // convert json to yaml |
| 145 | + eleventyConfig.addFilter('dumpasyaml', obj => { |
| 146 | + return yaml.safeDump(obj) |
| 147 | + }); |
| 148 | + |
| 149 | + // sort an array of objects by one object key value |
| 150 | + // can also get fancy and do a lodash.get selector string too |
| 151 | + // see https://lodash.com/docs/4.17.15#get |
| 152 | + eleventyConfig.addFilter('sortKey', (arr, selector) => { |
| 153 | + return arr.sort((a, b) => { |
| 154 | + let aKey = lodashGet(a, selector).toLowerCase(); |
| 155 | + let bKey = lodashGet(b, selector).toLowerCase(); |
| 156 | + if (aKey < bKey) { |
| 157 | + return -1; |
| 158 | + } else if (aKey > bKey) { |
| 159 | + return 1; |
| 160 | + } |
| 161 | + return 0; |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + eleventyConfig.addFilter('sortTools', (arr, githubData) => { |
| 166 | + return arr.sort((a, b) => { |
| 167 | + let aKey = githubData[a.data.repo] ? (githubData[a.data.repo].stars || 0) : 0; |
| 168 | + let bKey = githubData[b.data.repo] ? (githubData[b.data.repo].stars || 0) : 0; |
| 169 | + if (aKey < bKey) { |
| 170 | + return 1; |
| 171 | + } else if (aKey > bKey) { |
| 172 | + return -1; |
| 173 | + } |
| 174 | + return 0; |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + |
| 179 | + // Format a path to avoid any Cloudinary URL API miss-steps. |
| 180 | + eleventyConfig.addFilter("cloudinaryifyPath", (str) => { |
| 181 | + |
| 182 | + if (str) { |
| 183 | + |
| 184 | + // add generic url encoding |
| 185 | + str = encodeURI(str); |
| 186 | + |
| 187 | + // we also need to double escape some characters which might appear in text |
| 188 | + // but are meaningful in cloudinary URLs |
| 189 | + str = str.replace(/,/g, '%252C'); |
| 190 | + str = str.replace(/\//g, '%252F'); |
| 191 | + |
| 192 | + } |
| 193 | + return str; |
| 194 | + }); |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + // favicons files |
| 199 | + eleventyConfig.addPassthroughCopy("src/site/browserconfig.xml"); |
| 200 | + eleventyConfig.addPassthroughCopy("src/site/site.webmanifest"); |
| 201 | + eleventyConfig.addPassthroughCopy("src/site/survey/2021/community-survey-2021-methodology.pdf"); |
| 202 | + |
| 203 | + return { |
| 204 | + dir: { |
| 205 | + input: "src/site", |
| 206 | + inludes: "_includes", |
| 207 | + output: "dist" |
| 208 | + }, |
| 209 | + passthroughFileCopy: true, |
| 210 | + markdownTemplateEngine: false, |
| 211 | + htmlTemplateEngine: "njk" |
| 212 | + }; |
| 213 | + |
| 214 | +}; |
0 commit comments