jade filter :markdown for an object with a

2019-02-26 00:33发布

问题:

I have an object from the database with some markdown markup I would like to render with jade. But how? When I apply the :markdown filter I can't use the object as object anymore, but it get's treated as text.

I started here:

p
   :markdown
      entry.content

Which renders to plain:

entry.content

So I tried putting = and - in front or wrapping #{} arround it. Is it possible at all?

回答1:

Filters are compile-time, so if you want to run a markdown filter on a run-time variable, you'll have to render the markdown yourself and pass it to your jade view:

  • https://groups.google.com/forum/?fromgroups=#!topic/express-js/8H4HNcoeekk


回答2:

I found a simple way of doing this, as explained in this answer. It uses marked library so first install it.

$ npm install marked --save

In router page

var markdown = require('marked');
var text = '**new text**';
res.render('template', {text:text, markdown:markdown});

In template.jade, try any of following lines

!= markdown(text);
p!= markdown(text);

This is cleanest way of implementing dynamic filters for markdown, in my opinion.



标签: markdown pug