|
| 1 | +const { DateTime } = require("luxon"); |
| 2 | +const fs = require("fs"); |
| 3 | +const markdownIt = require("markdown-it"); |
| 4 | +const htmlmin = require("html-minifier"); |
| 5 | +const pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight"); |
| 6 | +const dataHighlight = require("./src/_includes/shortcodes/dataHighlight"); |
| 7 | +const badge = require("./src/_includes/shortcodes/badge"); |
| 8 | +const lazyImagesPlugin = require("eleventy-plugin-lazyimages"); |
| 9 | +const isPostPublished = (post) => !post.data.draft; |
| 10 | + |
| 11 | +let getSvgContent = function (file) { |
| 12 | + let relativeFilePath = `./src/assets/svg/${file}.svg`; |
| 13 | + let data = fs.readFileSync(relativeFilePath, function (err, contents) { |
| 14 | + if (err) return err; |
| 15 | + return contents; |
| 16 | + }); |
| 17 | + |
| 18 | + return data.toString("utf8"); |
| 19 | +}; |
| 20 | + |
| 21 | +module.exports = function (eleventyConfig) { |
| 22 | + eleventyConfig.addShortcode("dataHighlight", dataHighlight); |
| 23 | + eleventyConfig.addShortcode("svg", getSvgContent); |
| 24 | + eleventyConfig.addShortcode("badge", badge); |
| 25 | + |
| 26 | + eleventyConfig.addPlugin(lazyImagesPlugin); |
| 27 | + eleventyConfig.addPlugin(pluginSyntaxHighlight); |
| 28 | + |
| 29 | + eleventyConfig.addPassthroughCopy({ |
| 30 | + "./node_modules/alpinejs/dist/cdn.js": "./js/alpine.js", |
| 31 | + }); |
| 32 | + eleventyConfig.addPassthroughCopy("src/assets/images"); |
| 33 | + |
| 34 | + const md = new markdownIt({ |
| 35 | + html: true, |
| 36 | + breaks: true, |
| 37 | + linkify: true, |
| 38 | + }); |
| 39 | + |
| 40 | + eleventyConfig.addPairedShortcode("markdown", (content) => { |
| 41 | + return md.render(content); |
| 42 | + }); |
| 43 | + |
| 44 | + if (process.env.ELEVENTY_PRODUCTION) { |
| 45 | + eleventyConfig.addTransform("htmlmin", htmlminTransform); |
| 46 | + } else { |
| 47 | + eleventyConfig.setBrowserSyncConfig({ |
| 48 | + callbacks: { ready: browserSyncReady }, |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + eleventyConfig.addCollection("page", function (collections) { |
| 53 | + return collections.getFilteredByTag("page").sort(function (a, b) { |
| 54 | + return a.data.order - b.data.order; |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + eleventyConfig.addCollection("blog", (collection) => { |
| 59 | + return collection.getFilteredByGlob("./src/blog/*.md").filter(isPostPublished); |
| 60 | + }); |
| 61 | + |
| 62 | + eleventyConfig.addShortcode("currentDate", (date = DateTime.now()) => { |
| 63 | + return date; |
| 64 | + }); |
| 65 | + |
| 66 | + eleventyConfig.addFilter("dateFormating", (dateObj) => { |
| 67 | + return DateTime.fromJSDate(dateObj).toLocaleString(DateTime.DATE_MED); |
| 68 | + }); |
| 69 | + |
| 70 | + eleventyConfig.addWatchTarget("./src/styles/"); |
| 71 | + |
| 72 | + var pathPrefix = ""; |
| 73 | + if (process.env.GITHUB_REPOSITORY) { |
| 74 | + pathPrefix = process.env.GITHUB_REPOSITORY.split("/")[1]; |
| 75 | + } |
| 76 | + |
| 77 | + return { |
| 78 | + dir: { |
| 79 | + input: "src", |
| 80 | + data: "_data", |
| 81 | + includes: "_includes", |
| 82 | + layouts: "_layouts", |
| 83 | + }, |
| 84 | + pathPrefix, |
| 85 | + }; |
| 86 | +}; |
| 87 | + |
| 88 | +function browserSyncReady(err, bs) { |
| 89 | + bs.addMiddleware("*", (req, res) => { |
| 90 | + const content_404 = fs.readFileSync("_site/404.html"); |
| 91 | + // Add 404 http status code in request header. |
| 92 | + res.writeHead(404, { "Content-Type": "text/html; charset=ETF-8" }); |
| 93 | + // Provides the 404 content without redirect. |
| 94 | + res.write(content_404); |
| 95 | + res.end(); |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +function htmlminTransform(content, outputPath) { |
| 100 | + if (outputPath.endsWith(".html")) { |
| 101 | + let minified = htmlmin.minify(content, { |
| 102 | + useShortDoctype: true, |
| 103 | + removeComments: true, |
| 104 | + collapseWhitespace: true, |
| 105 | + }); |
| 106 | + return minified; |
| 107 | + } |
| 108 | + return content; |
| 109 | +} |
0 commit comments