|
| 1 | +const { DateTime } = require("luxon"); |
| 2 | +const CleanCSS = require("clean-css"); |
| 3 | +const UglifyJS = require("uglify-es"); |
| 4 | +const sanitizeHTML = require('sanitize-html') |
| 5 | +const htmlmin = require("html-minifier"); |
| 6 | +const widont = require("widont"); |
| 7 | +const md = require("markdown-it")({ |
| 8 | + html: true, |
| 9 | + linkify: true, |
| 10 | + typographer: true, |
| 11 | +}); |
| 12 | +const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight"); |
| 13 | +const inclusiveLangPlugin = require("@11ty/eleventy-plugin-inclusive-language"); |
| 14 | + |
| 15 | +module.exports = function(eleventyConfig) { |
| 16 | + |
| 17 | + // Date formatting (human readable) |
| 18 | + eleventyConfig.addFilter("readableDate", date => { |
| 19 | + return DateTime.fromJSDate(date).toFormat("dd LLL yyyy"); |
| 20 | + }); |
| 21 | + |
| 22 | + // Date formatting (machine readable) |
| 23 | + eleventyConfig.addFilter("machineDate", date => { |
| 24 | + return DateTime.fromJSDate(date).toISO(); |
| 25 | + }); |
| 26 | + |
| 27 | + // Markdownify |
| 28 | + eleventyConfig.addFilter("markdownify", text => { |
| 29 | + return md.renderInline( text ); |
| 30 | + }); |
| 31 | + |
| 32 | + // unSluggify |
| 33 | + eleventyConfig.addFilter("unslug", text => { |
| 34 | + text = text.charAt(0).toUpperCase() + text.slice(1); |
| 35 | + return text.replace(/-/g, ' '); |
| 36 | + }); |
| 37 | + |
| 38 | + // Name List |
| 39 | + eleventyConfig.addShortcode("NameList", names => { |
| 40 | + let strings = [], |
| 41 | + string = "", |
| 42 | + count = names.length; |
| 43 | + while ( count-- ) |
| 44 | + { |
| 45 | + let url = names[count].url || `https://twitter.com/${names[count].twitter}/`; |
| 46 | + strings.unshift( `<a href="${url}">${names[count].name}</a>` ); |
| 47 | + } |
| 48 | + count = strings.length; |
| 49 | + if ( count > 2 ) |
| 50 | + { |
| 51 | + strings[count-1] = "and " + strings[count-1]; |
| 52 | + string = strings.join(", "); |
| 53 | + } |
| 54 | + else if ( count == 2 ) |
| 55 | + { |
| 56 | + string = `${strings[0]} and ${strings[1]}`; |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + string = strings[0]; |
| 61 | + } |
| 62 | + return `${string}`; |
| 63 | + }); |
| 64 | + |
| 65 | + // Fix proper nouns |
| 66 | + eleventyConfig.addFilter("fixNames", text => { |
| 67 | + let test = text.toLowerCase(), |
| 68 | + acronyms = [ "html", "css", "svg" ], |
| 69 | + camel_case = [ "JavaScript" ], |
| 70 | + i, proper_name; |
| 71 | + |
| 72 | + if ( acronyms.indexOf( test ) > -1 ) |
| 73 | + { |
| 74 | + return text.toUpperCase(); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + for ( i in camel_case ) |
| 79 | + { |
| 80 | + proper_name = camel_case[i]; |
| 81 | + if ( proper_name.toLowerCase() == test ) |
| 82 | + { |
| 83 | + return proper_name; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return text; |
| 88 | + }); |
| 89 | + |
| 90 | + // Widont |
| 91 | + eleventyConfig.addFilter("widont", function(text) { |
| 92 | + return `${widont( text )}`; |
| 93 | + }); |
| 94 | + |
| 95 | + |
| 96 | + // Minify CSS |
| 97 | + eleventyConfig.addFilter("cssmin", function(code) { |
| 98 | + return new CleanCSS({}).minify(code).styles; |
| 99 | + }); |
| 100 | + |
| 101 | + // Minify JS |
| 102 | + eleventyConfig.addFilter("jsmin", function(code) { |
| 103 | + let minified = UglifyJS.minify(code); |
| 104 | + if (minified.error) { |
| 105 | + console.log("UglifyJS error: ", minified.error); |
| 106 | + return code; |
| 107 | + } |
| 108 | + return minified.code; |
| 109 | + }); |
| 110 | + |
| 111 | + // Minify HTML output |
| 112 | + eleventyConfig.addTransform("htmlmin", function(content, outputPath) { |
| 113 | + if (outputPath.indexOf(".html") > -1) { |
| 114 | + let minified = htmlmin.minify(content, { |
| 115 | + useShortDoctype: true, |
| 116 | + removeComments: true, |
| 117 | + collapseWhitespace: true, |
| 118 | + collapseBooleanAttributes: true |
| 119 | + }); |
| 120 | + minified = minified.replace('\u00a0', '<b class="shy">\u00a0</b>'); |
| 121 | + return minified; |
| 122 | + } |
| 123 | + return content; |
| 124 | + }); |
| 125 | + |
| 126 | + // Minify JS output |
| 127 | + eleventyConfig.addTransform("jsmin", function(content, outputPath) { |
| 128 | + if (outputPath.indexOf(".js") > -1) { |
| 129 | + let minified = UglifyJS.minify(content); |
| 130 | + return minified; |
| 131 | + } |
| 132 | + return content; |
| 133 | + }); |
| 134 | + |
| 135 | + // limit filter |
| 136 | + eleventyConfig.addNunjucksFilter("limit", function(array, limit) { |
| 137 | + return array.slice(0, limit); |
| 138 | + }); |
| 139 | + |
| 140 | + eleventyConfig.addCollection("devs", collection => { |
| 141 | + // get unsorted items |
| 142 | + return collection.getAll().filter( item => { |
| 143 | + return item.inputPath.indexOf("devs/") > -1; |
| 144 | + }); |
| 145 | + }); |
| 146 | + eleventyConfig.addFilter("extractID", url => { |
| 147 | + url = url.split("/"); |
| 148 | + return url[2]; |
| 149 | + }); |
| 150 | + |
| 151 | + eleventyConfig.addCollection("tags", function(collection) { |
| 152 | + // get unsorted items |
| 153 | + var tags = []; |
| 154 | + collection.getAll() |
| 155 | + .map( item => { |
| 156 | + if ( item.inputPath.indexOf("wants/") > -1 ) |
| 157 | + { |
| 158 | + item.data.tags.map( tag => { |
| 159 | + if ( tags.indexOf( tag ) < 0 ) |
| 160 | + { |
| 161 | + tags.push( tag ); |
| 162 | + } |
| 163 | + }); |
| 164 | + } |
| 165 | + }); |
| 166 | + return tags.sort(); |
| 167 | + }); |
| 168 | + |
| 169 | + eleventyConfig.addFilter("toString", function(collection, separator, props) { |
| 170 | + var ret = [], |
| 171 | + i = collection.length; |
| 172 | + while ( i-- ) |
| 173 | + { |
| 174 | + let str = [], |
| 175 | + j = props.length; |
| 176 | + while ( j-- ) |
| 177 | + { |
| 178 | + let text = collection[i].data[props[j]]; |
| 179 | + if ( props[j].indexOf("date") > -1 ) |
| 180 | + { |
| 181 | + text = new Date( text ); |
| 182 | + text = DateTime.fromJSDate(text).toFormat("dd LLL yyyy"); |
| 183 | + } |
| 184 | + str.unshift( text ); |
| 185 | + } |
| 186 | + ret.unshift( str.join( separator ) ); |
| 187 | + } |
| 188 | + return ret; |
| 189 | + }); |
| 190 | + |
| 191 | + eleventyConfig.addFilter("getDirectory", function(url) { |
| 192 | + url = url.split('/'); |
| 193 | + return url[1]; |
| 194 | + }); |
| 195 | + |
| 196 | + // Don't process folders with static assets e.g. images |
| 197 | + eleventyConfig.addPassthroughCopy("sw.js"); |
| 198 | + eleventyConfig.addPassthroughCopy("static/img"); |
| 199 | + eleventyConfig.addPassthroughCopy("static/js"); |
| 200 | + eleventyConfig.addPassthroughCopy("manifest.json"); |
| 201 | + eleventyConfig.addPassthroughCopy("admin"); |
| 202 | + // eleventyConfig.addPassthroughCopy("_includes/assets/"); |
| 203 | + |
| 204 | + /* Markdown Plugins */ |
| 205 | + let markdownIt = require("markdown-it"); |
| 206 | + let markdownItAnchor = require("markdown-it-anchor"); |
| 207 | + let options = { |
| 208 | + html: true, |
| 209 | + breaks: true, |
| 210 | + linkify: true |
| 211 | + }; |
| 212 | + let opts = { |
| 213 | + permalink: false |
| 214 | + }; |
| 215 | + |
| 216 | + eleventyConfig.setLibrary("md", markdownIt(options) |
| 217 | + .use(markdownItAnchor, opts) |
| 218 | + ); |
| 219 | + |
| 220 | + eleventyConfig.addPlugin(syntaxHighlight); |
| 221 | + |
| 222 | + //eleventyConfig.addPlugin(inclusiveLangPlugin); |
| 223 | + |
| 224 | + return { |
| 225 | + templateFormats: ["md", "njk", "html", "liquid"], |
| 226 | + |
| 227 | + // If your site lives in a different subdirectory, change this. |
| 228 | + // Leading or trailing slashes are all normalized away, so don’t worry about it. |
| 229 | + // If you don’t have a subdirectory, use "" or "/" (they do the same thing) |
| 230 | + // This is only used for URLs (it does not affect your file structure) |
| 231 | + pathPrefix: "/", |
| 232 | + |
| 233 | + markdownTemplateEngine: "liquid", |
| 234 | + htmlTemplateEngine: "njk", |
| 235 | + dataTemplateEngine: "njk", |
| 236 | + passthroughFileCopy: true, |
| 237 | + dir: { |
| 238 | + input: ".", |
| 239 | + includes: "_includes", |
| 240 | + data: "_data", |
| 241 | + output: "_site" |
| 242 | + } |
| 243 | + }; |
| 244 | +}; |
0 commit comments