Files
blog/eleventy.config.js
T

87 lines
2.5 KiB
JavaScript

import syntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight";
import markdownIt from "markdown-it";
import markdownItAnchor from "markdown-it-anchor";
import { filters } from "./src/_filters/index.js";
import { postComparator, isPostPublic } from "./src/_utils/sort-posts.js";
export default function (eleventyConfig) {
// 1. Static Asset Passthrough Copy
eleventyConfig.addPassthroughCopy({ "src/assets": "assets" });
// 2. Syntax Highlighting Plugin
eleventyConfig.addPlugin(syntaxHighlight);
// 3. Markdown-It Library Customization
const markdownOptions = {
html: true,
breaks: false,
linkify: true
};
const markdownLib = markdownIt(markdownOptions).use(markdownItAnchor, {
permalink: markdownItAnchor.permalink.headerLink({
safariReaderFix: true
}),
slugify: (s) =>
s
.trim()
.toLowerCase()
.replace(/[^a-z0-9 -]/g, "")
.replace(/\s+/g, "-")
.replace(/-+/g, "-")
});
eleventyConfig.setLibrary("md", markdownLib);
// 4. Custom Filters (imported from modular src/_filters/index.js)
for (const [name, fn] of Object.entries(filters)) {
eleventyConfig.addFilter(name, fn);
}
// 5. Custom Collections
// Pinned / featured prioritized, chronological blog posts collection (excludes drafts and hidden posts)
eleventyConfig.addCollection("posts", (collectionApi) => {
return collectionApi
.getFilteredByGlob("src/posts/*.md")
.filter((post) => isPostPublic(post))
.sort(postComparator);
});
// Dynamic Tag List Collection across all public posts (excludes drafts and hidden posts)
eleventyConfig.addCollection("tagList", (collectionApi) => {
const tagSet = new Set();
collectionApi
.getFilteredByGlob("src/posts/*.md")
.filter((post) => isPostPublic(post))
.forEach((item) => {
if ("tags" in item.data) {
let tags = item.data.tags;
if (typeof tags === "string") tags = [tags];
for (const tag of tags) {
const cleanTag = tag.replace(/^#/, "");
tagSet.add(cleanTag);
}
}
});
return [...tagSet].sort();
});
// 6. Dev Server Options (dynamic port binding)
eleventyConfig.setServerOptions({
port: parseInt(process.env.PORT || "8080", 10)
});
return {
dir: {
input: "src",
includes: "_includes",
data: "_data",
output: "_site"
},
templateFormats: ["njk", "md", "html"],
htmlTemplateEngine: "njk",
markdownTemplateEngine: "njk"
};
}