Passing raw Markdown text to Jade

元气小坏坏 提交于 2019-12-02 20:22:20

You can do this with a function passed in to jade from node:

var md = require("node-markdown").Markdown;

Then pass it into the view as a local:

res.render('view', { md:md, markdownContent:data });

Then render it in the jade view by calling the function:

!= md(markdownContent)

The node module node-markdown is deprecated. The marked is advanced new version. You can try like this

var md = require('marked');

Inside your router

res.render('template', { md: md });

Inside your jade template

div!= md(note.string)

I don't think jade can do this out of the box. One way to accomplish it that might feel slightly cleaner than pre-rendering the markdown is to create a helper function called markdown that takes a markdown string and returns HTML. Then you could do something like

section
    != markdown(md)

The markdown function should be included in the locals data when you render the jade template and can directly use a markdown library to convert the markdown syntax to HTML.

If you are using Scalate's Jade support you can enter:

section
    :&markdown
        #{md}

You can also import external files with:

section
    :&markdown
        #{include("MyFile.md")}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!