markdown global rendering engine and link customization

This commit is contained in:
Lyn Fugmann 2024-10-02 15:19:22 +02:00
parent b0994e0465
commit b09d4a8591
3 changed files with 18 additions and 15 deletions

View file

@ -4,7 +4,6 @@ title: Programm
tags: mainNav
eleventyNavigation:
order: 1
templateEngineOverride: njk,md
---
# Programm

View file

@ -4,7 +4,6 @@ title: Program
tags: mainNav
eleventyNavigation:
order: 1
templateEngineOverride: njk,md
---
# Program

View file

@ -92,28 +92,32 @@ module.exports = function(eleventyConfig) {
return value.filter(item => item.page.lang === this.page.lang)
});
const md_link_open = function (tokens, idx, options, env, self) {
const href = tokens[idx].attrGet('href');
if (href.startsWith('http')) {
tokens[idx].attrSet('target', '_blank');
tokens[idx].attrSet('rel', 'noreferrer');
} else {
const newHref = eleventyConfig.javascriptFunctions.locale_url(href);
tokens[idx].attrSet('href', newHref);
}
return self.renderToken(tokens, idx, options);
};
eleventyConfig.addAsyncFilter('mdInline', async function(value) {
// TODO replace with ESM import once we switch to Eleventy v3
const md = (await import('markdown-it/index.mjs')).default();
// customize link rendering
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
const href = tokens[idx].attrGet('href');
if (href.startsWith('http')) {
tokens[idx].attrSet('target', '_blank');
tokens[idx].attrSet('rel', 'noreferrer');
} else {
const newHref = eleventyConfig.javascriptFunctions.locale_url(href);
tokens[idx].attrSet('href', newHref);
}
return self.renderToken(tokens, idx, options);
};
md.renderer.rules.link_open = md_link_open;
return md.renderInline(value);
});
eleventyConfig.amendLibrary("md", mdLib => mdLib.renderer.rules.link_open = md_link_open);
// copy aileron font
eleventyConfig.addPassthroughCopy({'./submodules/aileron/fonts/*.(ttf|woff|woff2)': 'fonts'})
eleventyConfig.addPassthroughCopy({'./submodules/aileron/aileron.lite.min.css': 'aileron.lite.min.css'})
@ -140,5 +144,6 @@ module.exports = function(eleventyConfig) {
includes: "../_includes",
},
pathPrefix: `/${eseData.year}/`,
markdownTemplateEngine: "njk",
}
};