leilukin-site/eleventy.config.js

135 lines
4.4 KiB
JavaScript
Raw Normal View History

2024-05-06 10:43:37 +00:00
// Installed Plugins
2024-04-11 15:32:47 +00:00
const { EleventyRenderPlugin } = require("@11ty/eleventy");
2024-04-11 12:18:03 +00:00
const pluginRss = require("@11ty/eleventy-plugin-rss");
2024-04-18 12:22:49 +00:00
const eleventyNavigationPlugin = require("@11ty/eleventy-navigation");
2024-04-11 12:17:11 +00:00
const metagen = require('eleventy-plugin-metagen');
const pluginTOC = require('eleventy-plugin-nesting-toc');
const emojiReadTime = require("@11tyrocks/eleventy-plugin-emoji-readtime");
const embedEverything = require("eleventy-plugin-embed-everything");
2024-04-11 12:16:05 +00:00
// Configure slug filter
const slugify = require("slugify");
2024-05-06 10:43:37 +00:00
// Configure markdown-it plugins
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
module.exports = function (eleventyConfig) {
2024-04-11 12:16:05 +00:00
// Copy files
eleventyConfig.addPassthroughCopy("./src/assets/");
eleventyConfig.addWatchTarget("./src/assets/");
2024-04-11 12:16:05 +00:00
// Installed Plug-ins
2024-04-11 15:32:47 +00:00
eleventyConfig.addPlugin(EleventyRenderPlugin);
2024-04-11 12:18:03 +00:00
eleventyConfig.addPlugin(pluginRss);
2024-04-18 12:22:49 +00:00
eleventyConfig.addPlugin(eleventyNavigationPlugin);
2024-04-11 12:17:11 +00:00
eleventyConfig.addPlugin(metagen);
eleventyConfig.addPlugin(pluginTOC, { tags: ['h2', 'h3', 'h4', 'h5'] });
eleventyConfig.addPlugin(emojiReadTime);
eleventyConfig.addPlugin(embedEverything, { add: ['soundcloud'] });
2024-04-11 12:16:05 +00:00
// Configure slug filter
eleventyConfig.addFilter("slug", (str) => {
if (!str) {
return;
}
return slugify(str, {
lower: true,
remove: /["]/g,
});
});
2024-05-06 10:43:37 +00:00
// Configure markdown-it plugins
eleventyConfig.setLibrary('md', markdownIt().use(markdownItAnchor))
const linkAfterHeader = markdownItAnchor.permalink.linkAfterHeader({
class: "heading-anchor",
symbol: "<span hidden>#</span>",
style: "aria-labelledby",
});
const markdownItAnchorOptions = {
level: [1, 2, 3, 4, 5],
slugify: (str) =>
slugify(str, {
lower: true,
strict: true,
remove: /["]/g,
2024-05-06 10:43:37 +00:00
}),
tabIndex: false,
permalink(slug, opts, state, idx) {
2024-05-06 10:43:37 +00:00
state.tokens.splice(idx, 0,
Object.assign(new state.Token("div_open", "div", 1), {
// Add class "header-wrapper [h1 or h2 or h3]"
attrs: [["class", `heading-wrapper ${state.tokens[idx].tag}`]],
block: true,
})
);
2024-05-06 10:43:37 +00:00
state.tokens.splice(idx + 4, 0,
Object.assign(new state.Token("div_close", "div", -1), {
block: true,
})
);
linkAfterHeader(slug, opts, state, idx + 1);
},
};
/* Markdown Overrides */
let markdownLibrary = markdownIt({
html: true,
2024-05-06 10:43:37 +00:00
})
.use(markdownItAnchor, markdownItAnchorOptions)
.use(require("markdown-it-attrs"))
2024-05-06 12:26:53 +00:00
.use(require("markdown-it-bracketed-spans"))
.use(require("markdown-it-deflist"));
/* This is the part that tells 11ty to swap to our custom config */
eleventyConfig.setLibrary("md", markdownLibrary);
// Add content categories to a collection
eleventyConfig.addCollection("categories", function(collectionApi) {
let categories = new Set();
let contents = collectionApi.getFilteredByTag('contents');
contents.forEach(p => {
let cats = p.data.categories;
cats.forEach(c => categories.add(c));
});
return Array.from(categories).sort();
});
// Filter contents by category
eleventyConfig.addFilter("filterByCategory", function(contents, cat) {
cat = cat.toLowerCase();
let result = contents.filter(item => {
let cats = item.data.categories.map(c => c.toLowerCase());
return cats.includes(cat);
});
return result;
});
// Filter: Format dates
const dateOptions = {
year: 'numeric',
month: 'long',
day: 'numeric',
};
const dateTimeLocale = new Intl.DateTimeFormat("en-GB", dateOptions);
eleventyConfig.addFilter("niceDate", function(date) {
return dateTimeLocale.format(date);
});
2024-04-16 16:03:41 +00:00
// Filter: Limit number of items displayed
eleventyConfig.addFilter("itemLimit", function(array, itemLimit) {
return array.slice(0, itemLimit);
});
2024-05-06 14:44:07 +00:00
// Shortcode: <cite> tag
eleventyConfig.addShortcode('cite', (str) => `<cite>${str}</cite>`);
return {
dir: {
input: "src"
}
};
};