Eleventy: Parse markdown inside global data files

Created on 1 May 2020  路  4Comments  路  Source: 11ty/eleventy

Is there a way to parse markdown inside a JSON global data file using liquid?

education

Most helpful comment

Wanted to share the filter I ended up using, kudos from #684

// .eleventy.js
module.exports = function(eleventyConfig) { 
 ...
  const MarkdownIt = require("markdown-it");
  const mdRender = new MarkdownIt();
  eleventyConfig.addFilter("renderUsingMarkdown", function(rawString) {
    return mdRender.render(rawString);
  });
  ...
}

And in the liquid file
{{ index.rawMarkdown | renderUsingMarkdown }}

All 4 comments

What I have done in this case is to make a filter that can take the string from JSON and return HTML (remember to mark the return value as safe or html will be stepped out). You can use the same markdown library as 11ty (markdownit if I remember correctly). If you made universal filters before, it is quite easy. If not, I would recommend reading through someone else's plugin to get inspired.

The package is called markdown-it.

Thanks both, using filters with that library looks perfect 馃憣

Wanted to share the filter I ended up using, kudos from #684

// .eleventy.js
module.exports = function(eleventyConfig) { 
 ...
  const MarkdownIt = require("markdown-it");
  const mdRender = new MarkdownIt();
  eleventyConfig.addFilter("renderUsingMarkdown", function(rawString) {
    return mdRender.render(rawString);
  });
  ...
}

And in the liquid file
{{ index.rawMarkdown | renderUsingMarkdown }}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

michrome picture michrome  路  3Comments

jamrelian picture jamrelian  路  3Comments

michrome picture michrome  路  3Comments

proog picture proog  路  3Comments

zellwk picture zellwk  路  3Comments