override icon shortcode to make the SVGs accessible

This commit is contained in:
Lyn Fugmann 2024-09-27 15:35:51 +02:00
parent 35cb6c7def
commit 79e9051db6

View file

@ -19,6 +19,22 @@ module.exports = function(eleventyConfig) {
{ name: "solid", path: `${fontawesomePath}/solid` },
{ name: "brands", path: `${fontawesomePath}/brands` },
],
icon: {
shortcode: "rawIcon",
},
});
// override icon shortcode to make the SVGs accessible
eleventyConfig.addAsyncShortcode("icon", async function(input, attrs) {
if (!attrs || !("title" in attrs)) {
return eleventyConfig.nunjucksAsyncShortcodes.rawIcon(input, attrs);
}
const { title, ...newAttrs } = attrs;
newAttrs.role = "img";
const svgStr = await eleventyConfig.nunjucksAsyncShortcodes.rawIcon(input, newAttrs);
const match = svgStr.match(/(<svg.*?>)(.*$)/s);
return match[1] + "<title>" + title + "</title>" + match[2];
});
// process the logo files