const fs = require('fs');
const { DateTime } = require('luxon');
const pluginRss = require('@11ty/eleventy-plugin-rss');
const pluginSyntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const pluginNavigation = require('@11ty/eleventy-navigation');
const Image = require('@11ty/eleventy-img');
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');
const markdownItAttrs = require('markdown-it-attrs');
const { EleventyI18nPlugin } = require('@11ty/eleventy');
function hyphenize(input) {
return input
.replace(/[^\w- ]/, '')
.replace(/[_ ]/, '-')
.toLowerCase();
}
module.exports = function (eleventyConfig) {
const siteName = process.env.SITE;
if (!siteName) {
throw new Error(
'Cannot determine the name of the site to build. Make sure to set the SITE environment variable.'
);
}
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginSyntaxHighlight);
eleventyConfig.addPlugin(pluginNavigation);
eleventyConfig.addPlugin(EleventyI18nPlugin, {
defaultLanguage: "de",
});
eleventyConfig.setDataDeepMerge(true);
//
// Filters
//
eleventyConfig.addFilter('readableDate', (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat(
'dd LLL yyyy'
);
});
eleventyConfig.addFilter('htmlDateString', (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat('yyyy-LL-dd');
});
eleventyConfig.addFilter('head', (array, n) =>
n < 0 ? array.slice(n) : array.slice(0, n)
);
eleventyConfig.addFilter('min', (...numbers) =>
Math.min.apply(null, numbers)
);
function filterTagList(tags) {
return (tags || []).filter(
(tag) => ['all', 'nav', 'post', 'posts'].indexOf(tag) === -1
);
}
eleventyConfig.addFilter('filterTagList', filterTagList);
// Create separate collections for the different locales
function sortByLocales(items) {
return items.reduce((sorted, item) => ({
...sorted,
[`${item.page.lang}`]: [
...(sorted[item.page.lang] ?? []),
item
],
}), {})
}
// Build collections for the top and bottom navigation - the first one
// contains the main sites and the latter contains legal pages.
eleventyConfig.addCollection('topNavigation', (collection) => {
const topNavigation = collection
.getAll()
.filter((item) => !(item.data.tags || []).includes('legal'));
return sortByLocales(topNavigation);
});
eleventyConfig.addCollection('bottomNavigation', (collection) => {
const bottomNavigation = collection
.getAll()
.filter((item) => (item.data.tags || []).includes('legal'));
return sortByLocales(bottomNavigation);
});
eleventyConfig.addCollection('posts', (collection) => {
const posts = collection.getAll().filter((item) => item.data.layout === 'post');
return sortByLocales(posts);
});
//
// Widgets
//
eleventyConfig.addPairedShortcode(
'section',
(content, inverted) => `