Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing raw Markdown text to Jade

Tags:

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 don't know, that seems a bit messier.

Is there a way to pass variables into Jade filters?

like image 467
nickf Avatar asked Sep 26 '11 00:09

nickf


2 Answers

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)
like image 57
martyman Avatar answered Oct 14 '22 02:10

martyman


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)
like image 42
Fizer Khan Avatar answered Oct 14 '22 04:10

Fizer Khan