devon.lol/.eleventy.js

139 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-12-03 14:22:08 -08:00
const path = require("path");
const markdownIt = require("markdown-it");
const slugify = require("@sindresorhus/slugify");
const markdownItAnchor = require("markdown-it-anchor");
const markdownItObsidian = require("markdown-it-obsidian");
const markdownItAttrs = require("markdown-it-attrs");
2022-12-03 14:22:08 -08:00
const markdownItEleventyImg = require("markdown-it-eleventy-img");
2022-11-28 18:34:15 -08:00
const pluginRss = require("@11ty/eleventy-plugin-rss");
2023-01-18 19:26:39 -08:00
const embedYouTube = require("eleventy-plugin-youtube-embed");
2024-06-20 14:25:29 -04:00
const sanitizeHtml = require("sanitize-html");
const metadata = require("./src/_data/metadata.json");
const sanitize = (html) => {
return sanitizeHtml(html, {
allowedTags: ['b', 'i', 'em', 'strong', 'a'],
allowedAttributes: { 'a': ['href'] }
});
};
2022-11-26 15:20:28 -08:00
2023-07-28 22:53:14 -05:00
module.exports = function(eleventyConfig) {
2023-01-18 19:26:39 -08:00
eleventyConfig.addPlugin(embedYouTube);
2022-11-28 18:34:15 -08:00
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPassthroughCopy("css");
eleventyConfig.addPassthroughCopy("fonts");
eleventyConfig.addPassthroughCopy("images/**/*.{gif,png,jpg,jpeg}");
eleventyConfig.addPassthroughCopy({ "favicon": "/" });
2022-11-26 15:20:28 -08:00
eleventyConfig.addPassthroughCopy({
2022-11-28 18:34:47 -08:00
"node_modules/simpledotcss/simple.css": "css/simple.css",
2022-11-26 15:20:28 -08:00
});
2023-08-14 17:59:44 -04:00
eleventyConfig.addPassthroughCopy({ "src/robots.txt": "robots.txt" });
2022-11-26 15:20:28 -08:00
eleventyConfig.addFilter("buildPageTitle", function(title) {
if (this.page.url === '/') {
return title;
} else {
return title + ' ⇢ Devon.LoL 😆';
}
});
2023-07-28 22:53:14 -05:00
eleventyConfig.addFilter("stringToDate", function(value) {
return new Date(value);
2022-11-28 18:30:34 -08:00
});
2023-07-28 22:53:14 -05:00
eleventyConfig.addFilter("formatDatetimeForHumans", function(value) {
2022-11-28 18:30:34 -08:00
const dt = new Date(value);
const formattedDt = new Intl.DateTimeFormat("en", {
dateStyle: "medium",
timeStyle: "medium",
}).format(dt);
return formattedDt;
});
2023-07-28 22:53:14 -05:00
eleventyConfig.addFilter("formatDateForHumans", function(value) {
2022-11-28 18:30:34 -08:00
const dt = new Date(value);
const formattedDt = new Intl.DateTimeFormat("en", {
dateStyle: "medium",
}).format(dt);
return formattedDt;
});
eleventyConfig.addFilter("formatDateForRobots", function(value) {
const dt = new Date(value);
return dt.toISOString();
});
2023-07-28 22:53:14 -05:00
eleventyConfig.addFilter("convertYouTubeURLsToEmbeds", function(value) {
const youtubeUrlRegex = /<p>\s*(?:https?:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?!channel\/|@)([^\s&<>"']*)\s*<\/p>/gim;
return value.replace(youtubeUrlRegex, function(match, videoId) {
return '<iframe width="560" height="315" src="https://www.youtube.com/embed/' + videoId + '" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
});
});
2024-06-20 14:25:29 -04:00
eleventyConfig.addFilter('webmentionsByUrl', function(webmentions, url) {
const allowedTypes = ['in-reply-to', 'like-of', 'repost-of'];
const hasRequiredFields = mention => {
const { author, published, content } = mention;
return author.name && published && content;
};
const matchesUrl = mention => {
return mention['wm-target'] === url;
};
2023-07-28 22:53:14 -05:00
2024-06-20 14:25:29 -04:00
const filteredMentions = webmentions
.filter(mention => {
const isAllowedType = allowedTypes.includes(mention['wm-property']);
if (isAllowedType) {
const isReply = mention['wm-property'] === 'in-reply-to';
if (isReply) {
return matchesUrl(mention) && hasRequiredFields(mention);
}
return matchesUrl(mention);
}
}
);
const sanitizedMentions = filteredMentions.map((mention) => {
if (mention?.content?.html) {
mention.content.html = sanitize(mention.content.html);
const mastodonInstance = metadata.author.mastodon.instance;
const mastodonUsername = metadata.author.mastodon.username;
const mentionLink = `<a href="https://${mastodonInstance}/@${mastodonUsername}">@${metadata.author.mastodon.username}</a> `;
if (mention.content.html.startsWith(mentionLink)) {
mention.content.html = mention.content.html.substring(mentionLink.length)
}
}
return mention;
});
const sortedMentions = sanitizedMentions.reduce((accumulator, mention) => {
const key = mention['wm-property'];
const curGroup = accumulator[key] ?? [];
return { ...accumulator, [key]: [...curGroup, mention] };
}, {});
return sortedMentions;
});
2022-11-28 18:30:34 -08:00
2023-10-03 18:32:31 -05:00
const markdownLibrary = markdownIt({ html: true })
2022-11-26 15:20:28 -08:00
.use(markdownItAnchor, { slugify })
.use(
markdownItObsidian({
slugifyOnPageLinks: slugify,
})
)
2022-12-03 14:22:08 -08:00
.use(markdownItAttrs)
.use(markdownItEleventyImg, {
imgOptions: {
2023-10-22 14:28:50 -05:00
widths: [75, 150, 300, 600, 900, 1100],
2022-12-03 14:22:08 -08:00
outputDir: path.join("dist", "img"),
},
globalAttributes: {
loading: "lazy",
decoding: "async",
2023-10-22 14:28:50 -05:00
sizes: "100vw",
2022-12-03 14:22:08 -08:00
},
});
eleventyConfig.setLibrary("md", markdownLibrary);
2022-11-26 15:20:28 -08:00
return {
dir: {
2022-11-28 18:34:47 -08:00
input: "src",
output: "dist",
2022-11-26 15:20:28 -08:00
},
};
};