maia.crimew.gay/.eleventy.js

103 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-05-19 12:12:14 +00:00
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
2023-01-21 17:21:09 +00:00
const pluginRss = require('@11ty/eleventy-plugin-rss');
2023-05-19 11:56:16 +00:00
const timeToRead = require('eleventy-plugin-time-to-read');
2023-05-19 12:12:14 +00:00
const safeLinks = require('@sardine/eleventy-plugin-external-links');
2023-05-19 12:36:10 +00:00
const eleventySass = require("@11tyrocks/eleventy-plugin-sass-lightningcss");
const related = require("eleventy-plugin-related");
2023-07-24 13:38:03 +00:00
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
2022-05-31 13:09:45 +00:00
module.exports = function (eleventyConfig) {
const parseDate = (str) => {
if (str instanceof Date) {
return str;
}
2022-05-31 14:10:07 +00:00
return Date.parse(str);
2022-05-31 13:09:45 +00:00
};
2022-05-31 14:10:07 +00:00
const formatPart = (part, date) =>
new Intl.DateTimeFormat("en", part).format(date);
2023-05-19 12:20:25 +00:00
eleventyConfig.setNunjucksEnvironmentOptions({
lstripBlocks: true,
trimBlocks: true
});
2023-07-24 13:38:03 +00:00
eleventyConfig.setLibrary("md", markdownIt({ "html": true }).use(markdownItAnchor, { "level": 2 }));
2023-05-19 11:56:16 +00:00
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.addPlugin(timeToRead);
2023-05-19 12:12:14 +00:00
eleventyConfig.addPlugin(safeLinks);
2023-05-19 12:36:10 +00:00
eleventyConfig.addPlugin(eleventySass);
2022-05-31 14:10:07 +00:00
2022-05-31 13:09:45 +00:00
eleventyConfig.addPassthroughCopy({ "src/static": "/" });
eleventyConfig.addFilter("date_to_datetime", (obj) => {
const date = parseDate(obj);
2022-05-31 14:10:07 +00:00
return date.toISOString();
2022-05-31 13:09:45 +00:00
});
2022-05-31 14:10:07 +00:00
eleventyConfig.addFilter("date_formatted", (obj) => {
const date = parseDate(obj);
const month = formatPart({ month: "short" }, date);
const day = formatPart({ day: "numeric" }, date);
const year = formatPart({ year: "numeric" }, date);
const hours = date.getUTCHours();
const minutes = date.getUTCMinutes();
if (hours != 0 && minutes != 0) {
return `${month} ${day}, ${year} - ${hours}:${minutes} UTC`;
}
2022-05-31 14:10:07 +00:00
return `${month} ${day}, ${year}`;
});
2023-01-21 17:21:09 +00:00
eleventyConfig.addFilter('urlescape', str => {
return str.split('/').map(part => encodeURI(part)).join('/')
});
eleventyConfig.addFilter("related", function(obj) {
const post = this.ctx;
const posts = this.ctx.collections.posts.map(post => post.data);
const tagScore = (a, b) => {
const total = a.tags.length + b.tags.length;
const intersection = a.tags.filter(tag => b.tags.includes(tag)).length;
return (intersection * 2) / total;
}
const results = related.related({
serializer: (doc) => [doc.title, doc.description],
weights: [10, 10],
})(post, posts).map(result => {
return {
relative: result.relative + tagScore(post, result.document),
document: result.document
}
});
return results.filter(result => result.relative > 0.2).sort((a, b) => a.relative - b.relative).reverse().slice(0,3);
});
2023-01-21 17:21:09 +00:00
2022-05-31 14:10:07 +00:00
eleventyConfig.addCollection('posts', collection => {
return collection.getFilteredByGlob('src/posts/*.md').reverse()
});
2022-05-31 14:10:07 +00:00
2022-05-31 13:09:45 +00:00
return {
templateFormats: ["njk", "md", "html"],
dir: {
input: "src",
includes: "_includes",
data: "_data",
output: "www",
},
markdownTemplateEngine: "njk",
htmlTemplateEngine: "njk",
dataTemplateEngine: "njk",
passthroughFileCopy: false,
};
};