0
0
website/.eleventy.js
2024-10-16 12:39:17 +08:00

102 lines
3.0 KiB
JavaScript

const { DateTime } = require("luxon");
//const CleanCSS = require("clean-css");
const { minify } = require("html-minifier-terser");
const markdownIt = require("markdown-it");
const markdownItAttrs = require("markdown-it-attrs");
const markdownItAnchor = require("markdown-it-anchor");
module.exports = function (eleventyConfig) {
eleventyConfig.setUseGitIgnore(false);
const mdOptions = {
html: true,
breaks: true,
linkify: true,
};
const markdownItAnchorOptions = {
level: 2, // minimum level header -- anchors will only be applied to h2 level headers and below but not h1
permalink: true,
};
const markdownLib = markdownIt(mdOptions)
.use(markdownItAnchor, markdownItAnchorOptions)
.use(markdownItAttrs)
.use(require("markdown-it-bracketed-spans"))
.disable("code");
eleventyConfig.setLibrary("md", markdownLib);
eleventyConfig.setTemplateFormats([
"md",
"webmanifest",
"xml",
"ico",
"avif",
"webp",
"webm",
"svg",
"png",
"jpg",
"jpeg",
"txt",
"woff",
"woff2",
"css",
"pdf",
]);
eleventyConfig.addFilter("readablePostDate", (dateObj) => {
return DateTime.fromJSDate(dateObj, {
zone: "Asia/Singapore",
})
.setLocale("en-GB")
.toLocaleString({ day: "numeric", month: "short", year: "numeric" });
});
eleventyConfig.addFilter("postDate", (dateObj) => {
return DateTime.fromJSDate(dateObj, {
zone: "Asia/Singapore",
})
.setLocale("en-GB")
.toISODate();
});
eleventyConfig.addTransform("minifyHTML", function (content, outputPath) {
// Only minify HTML files
if (outputPath && outputPath.endsWith(".html")) {
return minify(content, {
collapseWhitespace: true, // Collapses whitespace between tags
removeComments: true, // Removes HTML comments
minifyCSS: true, // Minifies inline CSS
minifyJS: true, // Minifies inline JavaScript
removeAttributeQuotes: true, // Removes quotes around attributes when possible
removeOptionalTags: true, // Removes optional HTML tags (<html>, <head>, <body>)
collapseBooleanAttributes: true, // Converts boolean attributes to HTML5 style
removeEmptyAttributes: true, // Removes empty attributes
minifyURLs: true, // Minifies URLs in attributes
html5: true, // Enables HTML5 parsing
// caseSensitive: true, // Treats tags and attributes as case-sensitive
// keepClosingSlash: true, // Keeps trailing slash on self-closing tags
// quoteCharacter: '"', // Specifies quote character for attributes
// processConditionalComments: true, // Processes conditional comments in IE
// trimCustomFragments: true, // Trims custom HTML fragments
});
}
return content;
});
return {
dir: {
input: "src",
includes: "_includes",
output: "site",
},
};
eleventyConfig.addPassthroughCopy("/src/css");
return {
passthroughFileCopy: true,
};
};