contentful api markdown conversion to HTML

后端 未结 5 1964
滥情空心
滥情空心 2021-02-19 05:37

Is there any simple way to convert markdown text from contentful api to render into html code to be display on html page. I have tried using pagedown and some similar techniques

5条回答
  •  醉梦人生
    2021-02-19 06:01

    I know I'm late but here's the solution using handlebars:

    var marked = require('marked');
    marked.setOptions({
      renderer: new marked.Renderer(),
      sanitize: true,
      smartLists: true,
      smartypants: true
    });
    
    //Home
    router.get('/', (req, res) => {
      client.getEntry('')
      .then( (entry)=> {
        entry.fields.body = marked(entry.fields.body);
        res.render('static/index',
         {
           entry: entry,
           user: req.user
         });
      }).catch( (err) => {
        console.log(err);
      })
    });
    

    Then in our index.hbs template we can call the markdown variable in this case (entry.fields.body) by using {{{}}} to prevent escaping.

    {{{entry.fields.body}}}
    

提交回复
热议问题