0

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?

Stev_k
  • 2,118
  • 3
  • 22
  • 36
  • Answered here using Contentful, Markdown and Handelbars : https://stackoverflow.com/questions/34197407/contentful-api-markdown-conversion-to-html/47384680#47384680 – Kyriediculous Nov 20 '17 at 03:47

1 Answers1

1

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

stefan judis
  • 3,416
  • 14
  • 22
  • Actually, it was working all along, I was just looking for the output in the wrong place. Thanks – Stev_k Jul 06 '17 at 21:47