|
| 1 | +const { DateTime } = require('luxon'); |
| 2 | +const readingTime = require('eleventy-plugin-reading-time'); |
| 3 | +const pluginRss = require('@11ty/eleventy-plugin-rss'); |
| 4 | +const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight'); |
| 5 | +const htmlmin = require('html-minifier') |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | + |
| 9 | +const isDev = process.env.APP_ENV === 'development'; |
| 10 | +const isProd = process.env.ELEVENTY_ENV === 'production' |
| 11 | + |
| 12 | +const manifestPath = path.resolve( |
| 13 | + __dirname, |
| 14 | + 'public', |
| 15 | + 'assets', |
| 16 | + 'manifest.json' |
| 17 | +); |
| 18 | + |
| 19 | +const manifest = isDev |
| 20 | + ? { |
| 21 | + 'main.js': '/assets/main.js', |
| 22 | + 'main.css': '/assets/main.css', |
| 23 | + } |
| 24 | + : JSON.parse(fs.readFileSync(manifestPath, { encoding: 'utf8' })); |
| 25 | + |
| 26 | +module.exports = function (eleventyConfig) { |
| 27 | + eleventyConfig.addPlugin(readingTime); |
| 28 | + eleventyConfig.addPlugin(pluginRss); |
| 29 | + eleventyConfig.addPlugin(syntaxHighlight); |
| 30 | + |
| 31 | + // setup mermaid markdown highlighter |
| 32 | + const highlighter = eleventyConfig.markdownHighlighter; |
| 33 | + eleventyConfig.addMarkdownHighlighter((str, language) => { |
| 34 | + if (language === 'mermaid') { |
| 35 | + return `<pre class="mermaid">${str}</pre>`; |
| 36 | + } |
| 37 | + return highlighter(str, language); |
| 38 | + }); |
| 39 | + |
| 40 | + eleventyConfig.setDataDeepMerge(true); |
| 41 | + eleventyConfig.addPassthroughCopy({ 'src/images': 'images' }); |
| 42 | + eleventyConfig.setBrowserSyncConfig({ files: [manifestPath] }); |
| 43 | + |
| 44 | + eleventyConfig.addShortcode('audscss', function () { |
| 45 | + return manifest['main.css'] |
| 46 | + ? `<link href="${manifest['pancake.min.css']}" rel="stylesheet" />` |
| 47 | + : ''; |
| 48 | + }); |
| 49 | + |
| 50 | + eleventyConfig.addShortcode('bundledcss', function () { |
| 51 | + return manifest['main.css'] |
| 52 | + ? `<link href="${manifest['main.css']}" rel="stylesheet" />` |
| 53 | + : ''; |
| 54 | + }); |
| 55 | + |
| 56 | + eleventyConfig.addShortcode('bundledjs', function () { |
| 57 | + return manifest['main.js'] |
| 58 | + ? `<script src="${manifest['main.js']}"></script>` |
| 59 | + : ''; |
| 60 | + }); |
| 61 | + |
| 62 | + eleventyConfig.addShortcode('audsjs', function () { |
| 63 | + return manifest['main.js'] |
| 64 | + ? `<script src="${manifest['pancake.min.js']}"></script>` |
| 65 | + : ''; |
| 66 | + }); |
| 67 | + |
| 68 | + eleventyConfig.addFilter('excerpt', (post) => { |
| 69 | + const content = post.replace(/(<([^>]+)>)/gi, ''); |
| 70 | + return content.substr(0, content.lastIndexOf(' ', 200)) + '...'; |
| 71 | + }); |
| 72 | + |
| 73 | + eleventyConfig.addFilter('readableDate', (dateObj) => { |
| 74 | + return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat( |
| 75 | + 'dd LLL yyyy' |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + eleventyConfig.addFilter('htmlDateString', (dateObj) => { |
| 80 | + return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat('yyyy-LL-dd'); |
| 81 | + }); |
| 82 | + |
| 83 | + eleventyConfig.addFilter('dateToIso', (dateString) => { |
| 84 | + return new Date(dateString).toISOString() |
| 85 | + }); |
| 86 | + |
| 87 | + eleventyConfig.addFilter('head', (array, n) => { |
| 88 | + if (n < 0) { |
| 89 | + return array.slice(n); |
| 90 | + } |
| 91 | + |
| 92 | + return array.slice(0, n); |
| 93 | + }); |
| 94 | + |
| 95 | + eleventyConfig.addCollection('tagList', function (collection) { |
| 96 | + let tagSet = new Set(); |
| 97 | + collection.getAll().forEach(function (item) { |
| 98 | + if ('tags' in item.data) { |
| 99 | + let tags = item.data.tags; |
| 100 | + |
| 101 | + tags = tags.filter(function (item) { |
| 102 | + switch (item) { |
| 103 | + case 'all': |
| 104 | + case 'nav': |
| 105 | + case 'post': |
| 106 | + case 'posts': |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + return true; |
| 111 | + }); |
| 112 | + |
| 113 | + for (const tag of tags) { |
| 114 | + tagSet.add(tag); |
| 115 | + } |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + return [...tagSet]; |
| 120 | + }); |
| 121 | + |
| 122 | + eleventyConfig.addFilter('pageTags', (tags) => { |
| 123 | + const generalTags = ['all', 'nav', 'post', 'posts']; |
| 124 | + |
| 125 | + return tags |
| 126 | + .toString() |
| 127 | + .split(',') |
| 128 | + .filter((tag) => { |
| 129 | + return !generalTags.includes(tag); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + eleventyConfig.addTransform('htmlmin', function(content, outputPath) { |
| 134 | + if ( outputPath && outputPath.endsWith(".html") && isProd) { |
| 135 | + return htmlmin.minify(content, { |
| 136 | + removeComments: true, |
| 137 | + collapseWhitespace: true, |
| 138 | + useShortDoctype: true, |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + return content; |
| 143 | + }); |
| 144 | + |
| 145 | + return { |
| 146 | + dir: { |
| 147 | + input: 'src', |
| 148 | + output: 'public', |
| 149 | + includes: 'includes', |
| 150 | + data: 'data', |
| 151 | + layouts: 'layouts', |
| 152 | + passthroughFileCopy: true, |
| 153 | + templateFormats: ['html', 'njk', 'md'], |
| 154 | + htmlTemplateEngine: 'njk', |
| 155 | + markdownTemplateEngine: 'njk', |
| 156 | + }, |
| 157 | + }; |
| 158 | +}; |
0 commit comments