|
| 1 | +const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight") |
| 2 | +const CleanCSS = require("clean-css") |
| 3 | +const htmlMinifier = require("html-minifier") |
| 4 | +const markdownIt = require('markdown-it') |
| 5 | +const slugify = require("slugify") |
| 6 | +const pluginTOC = require('eleventy-plugin-nesting-toc') |
| 7 | +const markdownItAnchor = require('markdown-it-anchor') |
| 8 | +const markdownItToc = require('markdown-it-table-of-contents') |
| 9 | +const markdownItEmoji = require('markdown-it-emoji') |
| 10 | + |
| 11 | +module.exports = function(eleventyConfig) { |
| 12 | + eleventyConfig.addTransform("htmlmin", function(content, outputPath) { |
| 13 | + // Eleventy 1.0+: use this.inputPath and this.outputPath instead |
| 14 | + if (outputPath.endsWith(".html")) { |
| 15 | + let minified = htmlMinifier.minify(content, { |
| 16 | + useShortDoctype: true, |
| 17 | + removeComments: true, |
| 18 | + collapseWhitespace: true |
| 19 | + }) |
| 20 | + return minified |
| 21 | + } |
| 22 | + return content |
| 23 | + }) |
| 24 | + |
| 25 | + eleventyConfig.addFilter("cssmin", function(code) { |
| 26 | + return new CleanCSS({}).minify(code).styles |
| 27 | + }) |
| 28 | + |
| 29 | + eleventyConfig.addShortcode("serviceWorker", function () { |
| 30 | + if (process.env.NODE_ENV === 'production') { |
| 31 | + return `<script>window.onload=function(){"serviceWorker"in navigator&&navigator.serviceWorker.register('/sw.js')};</script>` |
| 32 | + } |
| 33 | + return '' |
| 34 | + }) |
| 35 | + |
| 36 | + eleventyConfig.addPlugin(syntaxHighlight) |
| 37 | + |
| 38 | + function removeExtraText(s) { |
| 39 | + let newStr = String(s).replace(/New\ in\ v\d+\.\d+\.\d+/, "") |
| 40 | + newStr = newStr.replace(/Coming\ soon\ in\ v\d+\.\d+\.\d+/, "") |
| 41 | + newStr = newStr.replace(/⚠️/g, "") |
| 42 | + newStr = newStr.replace(/[?!]/g, "") |
| 43 | + newStr = newStr.replace(/<[^>]*>/g, "") |
| 44 | + return newStr; |
| 45 | + } |
| 46 | + |
| 47 | + function markdownItSlugify(s) { |
| 48 | + return slugify(removeExtraText(s), { lower: true, remove: /[:’'`,]/g }) |
| 49 | + } |
| 50 | + |
| 51 | + let mdIt = markdownIt({ |
| 52 | + html: true, |
| 53 | + breaks: true |
| 54 | + }) |
| 55 | + .use(markdownItAnchor, { |
| 56 | + permalink: true, |
| 57 | + slugify: markdownItSlugify, |
| 58 | + permalinkBefore: false, |
| 59 | + permalinkClass: "direct-link", |
| 60 | + permalinkSymbol: "", |
| 61 | + level: [1,2,3,4,5] |
| 62 | + }) |
| 63 | + .use(markdownItToc, { |
| 64 | + includeLevel: [2, 3, 4, 5], |
| 65 | + slugify: markdownItSlugify, |
| 66 | + format: function(heading) { |
| 67 | + return removeExtraText(heading) |
| 68 | + }, |
| 69 | + transformLink: function(link) { |
| 70 | + // remove backticks from markdown code |
| 71 | + return link.replace(/\%60/g, "") |
| 72 | + } |
| 73 | + }) |
| 74 | + .use(markdownItEmoji); |
| 75 | + |
| 76 | + eleventyConfig.setLibrary("md", mdIt) |
| 77 | + |
| 78 | + eleventyConfig.addPlugin(pluginTOC, { |
| 79 | + tags: ['h2', 'h3', 'h4', 'h5'], |
| 80 | + wrapperClass: 'toc__nav', |
| 81 | + ul: true |
| 82 | + }) |
| 83 | +} |
0 commit comments