Replicating Jekyll's `markdownify` filter in Nunjucks with Eleventy
Published:
Tags: Eleventy
Jekyll has a useful filter called markdownify
that converts a Markdown string into HTML. Nunjucks doesn’t have this filter.
I’ve replicated this in Eleventy by adding this into my .eleventy.js
file.
module.exports = function (eleventyConfig) {
const md = require("markdown-it")({
html: false,
breaks: true,
linkify: true,
});
eleventyConfig.addNunjucksFilter("markdownify", (markdownString) =>
md.render(markdownString)
);
};
{/_ text _/}
In a Nunjucks template, you can now use {{ someMarkdownString | markdownify | safe }}
to convert any Markdown string to HTML.
I’m not 100% sure about the | safe
bit, but it’s working for me like this.