Add target and class to external links

This commit is contained in:
Ole Eskild Steensen 2022-02-23 16:20:57 +01:00
parent ebb4746426
commit 9241b5431f

View File

@ -26,6 +26,29 @@ module.exports = function(eleventyConfig) {
// Other languages
return origRule(tokens, idx, options, env, slf);
};
const defaultRender = md.renderer.rules.link_open || function (tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
const aIndex = tokens[idx].attrIndex('target');
const classIndex = tokens[idx].attrIndex('class');
if (aIndex < 0) {
tokens[idx].attrPush(['target', '_blank']);
} else {
tokens[idx].attrs[aIndex][1] = '_blank';
}
if (classIndex< 0) {
tokens[idx].attrPush(['class', 'external-link']);
} else {
tokens[idx].attrs[classIndex][1] = 'external-link';
}
return defaultRender(tokens, idx, options, env, self);
};
});
eleventyConfig.setLibrary("md", markdownLib);