|
1 |
| -const rssPlugin = require('@11ty/eleventy-plugin-rss'); |
2 |
| -const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight'); |
3 |
| -const fs = require('fs'); |
4 |
| - |
5 |
| -const pluginTOC = require('eleventy-plugin-toc') |
6 |
| - |
7 |
| - |
8 |
| -const filters = require('./src/11ty/filters'); |
9 |
| -const filtersMethods = Object.entries(filters); |
10 |
| - |
11 |
| -// Import transforms |
12 |
| -const parseTransform = require('./src/11ty/transforms/parse-transform.js'); |
13 |
| - |
14 |
| -const markdownConfig = require('./src/11ty/utils/markdown.js'); |
15 |
| - |
16 |
| -// Import data files |
17 |
| -const site = require('./src/_data/site.json'); |
18 |
| - |
19 |
| -const passthroughItems = [ |
20 |
| - "src/_redirects", |
21 |
| - "src/fonts", |
22 |
| - "src/images", |
23 |
| - "src/js", |
24 |
| - "src/css", |
25 |
| - "src/uploads", |
26 |
| - "node_modules/nunjucks/browser/nunjucks-slim.js" |
27 |
| -] |
28 |
| - |
29 |
| -module.exports = function (config) { |
30 |
| - // Filters |
31 |
| - filtersMethods.forEach(([name, filter]) => { |
32 |
| - config.addFilter(name, filter) |
| 1 | +const { DateTime } = require("luxon"); |
| 2 | +const CleanCSS = require("clean-css"); |
| 3 | +const UglifyJS = require("uglify-es"); |
| 4 | +const htmlmin = require("html-minifier"); |
| 5 | +const eleventyNavigationPlugin = require("@11ty/eleventy-navigation"); |
| 6 | +const slugify = require('slugify'); |
| 7 | + |
| 8 | +module.exports = function(eleventyConfig) { |
| 9 | + |
| 10 | + // Eleventy Navigation https://www.11ty.dev/docs/plugins/navigation/ |
| 11 | + eleventyConfig.addPlugin(eleventyNavigationPlugin); |
| 12 | + |
| 13 | + // Configuration API: use eleventyConfig.addLayoutAlias(from, to) to add |
| 14 | + // layout aliases! Say you have a bunch of existing content using |
| 15 | + // layout: post. If you don’t want to rewrite all of those values, just map |
| 16 | + // post to a new file like this: |
| 17 | + // eleventyConfig.addLayoutAlias("post", "layouts/my_new_post_layout.njk"); |
| 18 | + |
| 19 | + // Merge data instead of overriding |
| 20 | + // https://www.11ty.dev/docs/data-deep-merge/ |
| 21 | + eleventyConfig.setDataDeepMerge(true); |
| 22 | + |
| 23 | + // Add support for maintenance-free post authors |
| 24 | + // Adds an authors collection using the author key in our post frontmatter |
| 25 | + // Thanks to @pdehaan: https://github.com/pdehaan |
| 26 | + eleventyConfig.addCollection("authors", collection => { |
| 27 | + const blogs = collection.getFilteredByGlob("src/posts/*.md"); |
| 28 | + return blogs.reduce((coll, post) => { |
| 29 | + const author = slugify(post.data.author); |
| 30 | + if (!author) { |
| 31 | + return coll; |
| 32 | + } |
| 33 | + if (!coll.hasOwnProperty(author)) { |
| 34 | + coll[author] = []; |
| 35 | + } |
| 36 | + coll[author].push(post.data); |
| 37 | + return coll; |
| 38 | + }, {}); |
33 | 39 | });
|
34 | 40 |
|
35 |
| - // Transforms |
36 |
| - config.addTransform('parse', parseTransform); |
37 |
| - |
38 |
| - passthroughItems.forEach(item => { |
39 |
| - config.addPassthroughCopy(item); |
40 |
| - }) |
| 41 | + // Date formatting (human readable) |
| 42 | + eleventyConfig.addFilter("readableDate", dateObj => { |
| 43 | + return DateTime.fromJSDate(dateObj).toFormat("dd/MM/yyyy"); |
| 44 | + }); |
41 | 45 |
|
42 |
| - config.addCollection('posts', collection => { |
43 |
| - return [ |
44 |
| - ...collection.getFilteredByGlob('./src/posts/*.md') |
45 |
| - ].reverse(); |
| 46 | + // Date formatting (machine readable) |
| 47 | + eleventyConfig.addFilter("machineDate", dateObj => { |
| 48 | + return DateTime.fromJSDate(dateObj).toFormat("yyyy-MM-dd"); |
46 | 49 | });
|
47 | 50 |
|
48 |
| - config.addCollection('work', collection => { |
49 |
| - return collection.getFilteredByGlob('./src/work/*.md') |
50 |
| - .sort((a, b) => b.data.start - a.data.start); |
| 51 | + // Minify CSS |
| 52 | + eleventyConfig.addFilter("cssmin", function(code) { |
| 53 | + return new CleanCSS({}).minify(code).styles; |
51 | 54 | });
|
52 | 55 |
|
53 |
| - // Plugins |
54 |
| - config.addPlugin(rssPlugin); |
55 |
| - config.addPlugin(syntaxHighlight); |
56 |
| - config.addPlugin(pluginTOC, { |
57 |
| - tags: ['h2', 'h3', 'h4'], |
58 |
| - wrapper: 'nav', |
59 |
| - wrapperClass: 'toc' |
| 56 | + // Minify JS |
| 57 | + eleventyConfig.addFilter("jsmin", function(code) { |
| 58 | + let minified = UglifyJS.minify(code); |
| 59 | + if (minified.error) { |
| 60 | + console.log("UglifyJS error: ", minified.error); |
| 61 | + return code; |
| 62 | + } |
| 63 | + return minified.code; |
60 | 64 | });
|
61 | 65 |
|
62 |
| - // 404 |
63 |
| - config.setBrowserSyncConfig({ |
64 |
| - ui: false, |
65 |
| - // Replicate click on all opened tabs |
66 |
| - ghostMode: false, |
67 |
| - callbacks: { |
68 |
| - ready: function (err, browserSync) { |
69 |
| - const content_404 = fs.readFileSync('dist/404.html'); |
70 |
| - |
71 |
| - browserSync.addMiddleware('*', (req, res) => { |
72 |
| - // Provides the 404 content without redirect. |
73 |
| - res.write(content_404); |
74 |
| - res.end(); |
75 |
| - }); |
76 |
| - } |
| 66 | + // Minify HTML output |
| 67 | + eleventyConfig.addTransform("htmlmin", function(content, outputPath) { |
| 68 | + if (outputPath && outputPath.indexOf(".html") > -1) { |
| 69 | + let minified = htmlmin.minify(content, { |
| 70 | + useShortDoctype: true, |
| 71 | + removeComments: true, |
| 72 | + collapseWhitespace: true |
| 73 | + }); |
| 74 | + return minified; |
77 | 75 | }
|
| 76 | + return content; |
78 | 77 | });
|
79 | 78 |
|
80 |
| - config.setLibrary("md", markdownConfig) |
| 79 | + // Don't process folders with static assets e.g. images |
| 80 | + eleventyConfig.addPassthroughCopy("src/favicon.ico"); |
| 81 | + eleventyConfig.addPassthroughCopy("src/static/img"); |
| 82 | + eleventyConfig.addPassthroughCopy("src/admin"); |
| 83 | + eleventyConfig.addPassthroughCopy("src/_includes/assets/"); |
| 84 | + |
| 85 | + /* Markdown Plugins */ |
| 86 | + let markdownIt = require("markdown-it"); |
| 87 | + let markdownItAnchor = require("markdown-it-anchor"); |
| 88 | + let options = { |
| 89 | + html: true, |
| 90 | + breaks: true, |
| 91 | + linkify: true |
| 92 | + }; |
| 93 | + let opts = { |
| 94 | + permalink: false |
| 95 | + }; |
81 | 96 |
|
82 |
| - config.setFrontMatterParsingOptions({ |
83 |
| - excerpt: true, |
84 |
| - excerpt_separator: "---" |
85 |
| - }); |
| 97 | + eleventyConfig.setLibrary("md", markdownIt(options) |
| 98 | + .use(markdownItAnchor, opts) |
| 99 | + ); |
86 | 100 |
|
87 | 101 | return {
|
88 |
| - templateFormats: [ |
89 |
| - "md", |
90 |
| - "njk", |
91 |
| - ], |
92 |
| - markdownTemplateEngine: "njk", |
| 102 | + templateFormats: ["md", "njk", "html", "liquid"], |
| 103 | + |
| 104 | + // If your site lives in a different subdirectory, change this. |
| 105 | + // Leading or trailing slashes are all normalized away, so don’t worry about it. |
| 106 | + // If you don’t have a subdirectory, use "" or "/" (they do the same thing) |
| 107 | + // This is only used for URLs (it does not affect your file structure) |
| 108 | + pathPrefix: "/", |
| 109 | + |
| 110 | + markdownTemplateEngine: "liquid", |
93 | 111 | htmlTemplateEngine: "njk",
|
94 | 112 | dataTemplateEngine: "njk",
|
95 | 113 | dir: {
|
96 |
| - input: 'src', |
97 |
| - output: 'dist' |
98 |
| - }, |
99 |
| - passthroughFileCopy: true |
| 114 | + input: "src", |
| 115 | + includes: "_includes", |
| 116 | + data: "_data", |
| 117 | + output: "dist" |
| 118 | + } |
100 | 119 | };
|
101 | 120 | };
|
0 commit comments