Passing raw Markdown text to Jade

nickf picture nickf · Sep 26, 2011 · Viewed 10.3k times · Source

I'm playing around with my first Node.js Express application, and as every programmer knows, the first thing you should build when testing out a new framework is a blog! Anyway, I'd like to write the articles in Markdown and then render it in the view. I saw that Jade allows for this to be done inside the view itself, using filters, but I can't get that working.

To simplify the situation, here's an example of what I'm talking about.

//app.js
res.render("article", {
    md : "Hello World!\n\n*Woo*"
});

//article.jade
section
    :markdown
        #{md}

But, that outputs this: <section><h1>{md}</h1></section>... it isn't substituting in the variables I've passed to it.

Then I tried this:

//article.jade
section
    :markdown
        !{md}

And the output is this:

<section><p>Hello World!

*Woo*</p></section>

So, now it's not parsing the markdown!

I have been able to get this to work by parsing the markdown in the app.js file and then passing the HTML to the view to display, but I dunno, that seems a bit messier.

Is there a way to pass variables into Jade filters?

Answer

martyman picture martyman · Sep 14, 2012

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)