Search code examples
markdowncontentfulmetalsmith

Get metalsmith, contentful and markdown to work together


I am trying to use Metalsmith to render content from the Contentful platform (using the metalsmith-contentful platform and metalsmith-layout as per the example here).

One of my contentful fields is markdown text, so I want to render it as HTML in the final template. My initial setup was similar to the example above but only read the markdown text as plain text.

I am now trying to convert the markdown in a handlebars helper, i.e.

 handlebars.registerHelper('markdown', function(object) {
 var text = marked(object);
 return new handlebars.SafeString(text);
 })

and calling with {{{ markdown mycontentfulobject}}}

but this doesn't work either.

Any ideas?


Solution

  • Do you get any exceptions?

    I've got exactly the same setup right now. And I think you're really close already.

    What I do is, I call registerHelper.

    const marked = require( 'marked' )
    
    handlebars.registerHelper('marked', function (text) {
      return marked(text);
    })
    

    And I use it in my templates like that.

    <section>{{#marked fields.excerpt}}{{/marked}}</section>
    

    This works pretty well for me. :)

    You can find an example project here 👉🏻 https://github.com/stefanjudis/stefan-judis-website/blob/master/build.js#L22-L25