Parsing Markdown files in Node.js using the node-markdown module

The following example shows how you can parse Markdown files in Node.js using the node-markdown module.

For more information on Markdown, see http://daringfireball.net/projects/markdown/.


First, install the node-markdown module using the following Terminal command:

npm install node-markdown

Next, create a new Markdown file, books.md, in the current directory:

# Books

- [Async JavaScript](http://www.amazon.com/Async-JavaScript-Trevor-Burnham/dp/1475247362/ref=sr_1_6?ie=UTF8&qid=1347828493&sr=8-6&keywords=nodejs)
- [Hands-on Node.js](http://www.amazon.com/Hands-on-Node-js-ebook/dp/B004ZURMXY/ref=sr_1_15?ie=UTF8&qid=1347828493&sr=8-15&keywords=nodejs)
- [Node for Front-End Developers](http://www.amazon.com/Node-Front-End-Developers-Garann-Means/dp/1449318835/ref=sr_1_28?ie=UTF8&qid=1347829122&sr=8-28&keywords=nodejs)
- [Node Web Development](http://www.amazon.com/Node-Web-Development-David-Herron/dp/184951514X/ref=sr_1_4?ie=UTF8&qid=1347828493&sr=8-4&keywords=nodejs)
- [Node.js in Action](http://www.amazon.com/Node-js-Action-Mike-Cantelon/dp/1617290572/ref=sr_1_3?ie=UTF8&qid=1347828493&sr=8-3&keywords=nodejs)
- [Node: Up and Running: Scalable Server-Side Code with JavaScript](http://www.amazon.com/Node-Running-Scalable-Server-Side-JavaScript/dp/1449398588/ref=sr_1_1?ie=UTF8&qid=1347828493&sr=8-1&keywords=nodejs)
- [Professional Node.js: Building JavaScript Based Scalable Software](http://www.amazon.com/Professional-Node-js-Building-Javascript-Scalable/dp/1118185463/ref=sr_1_32?ie=UTF8&qid=1347829122&sr=8-32&keywords=nodejs)
- [Programming Node.js](http://www.amazon.com/Programming-Node-js-Travis-Swicegood/dp/1934356891/ref=sr_1_11?ie=UTF8&qid=1347828493&sr=8-11&keywords=nodejs)
- [Smashing Node.js: JavaScript Everywhere](http://smashingnode.com/)
- [The Node Beginner Book](http://www.amazon.com/Node-Beginner-Book-Manuel-Kiessling/dp/1471628442/ref=sr_1_9? ie=UTF8&qid=1347828493&sr=8-9&keywords=nodejs)

Finally, create the main JavaScript file, app.js, and enter the following text:

#!/usr/bin/env node

var fs = require("fs");

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

fs.readFile("books.md", function (err, data) {
    if (err) {
        throw err;
    }

    console.log(md(data.toString()));
});

If you run the previous code from the Terminal (node ./app.js) you can see that the Markdown is converted to HTML:

<h1>Books</h1>

<ul>
<li><a href="http://www.amazon.com/Async-JavaScript-Trevor-Burnham/dp/1475247362/ref=sr_1_6?ie=UTF8&amp;qid=1347828493&amp;sr=8-6&amp;keywords=nodejs">Async JavaScript</a></li>
<li><a href="http://www.amazon.com/Hands-on-Node-js-ebook/dp/B004ZURMXY/ref=sr_1_15?ie=UTF8&amp;qid=1347828493&amp;sr=8-15&amp;keywords=nodejs">Hands-on Node.js</a></li>
<li><a href="http://www.amazon.com/Node-Front-End-Developers-Garann-Means/dp/1449318835/ref=sr_1_28?ie=UTF8&amp;qid=1347829122&amp;sr=8-28&amp;keywords=nodejs">Node for Front-End Developers</a></li>
<li><a href="http://www.amazon.com/Node-Web-Development-David-Herron/dp/184951514X/ref=sr_1_4?ie=UTF8&amp;qid=1347828493&amp;sr=8-4&amp;keywords=nodejs">Node Web Development</a></li>
<li><a href="http://www.amazon.com/Node-js-Action-Mike-Cantelon/dp/1617290572/ref=sr_1_3?ie=UTF8&amp;qid=1347828493&amp;sr=8-3&amp;keywords=nodejs">Node.js in Action</a></li>
<li><a href="http://www.amazon.com/Node-Running-Scalable-Server-Side-JavaScript/dp/1449398588/ref=sr_1_1?ie=UTF8&amp;qid=1347828493&amp;sr=8-1&amp;keywords=nodejs">Node: Up and Running: Scalable Server-Side Code with JavaScript</a></li>
<li><a href="http://www.amazon.com/Professional-Node-js-Building-Javascript-Scalable/dp/1118185463/ref=sr_1_32?ie=UTF8&amp;qid=1347829122&amp;sr=8-32&amp;keywords=nodejs">Professional Node.js: Building JavaScript Based Scalable Software</a></li>
<li><a href="http://www.amazon.com/Programming-Node-js-Travis-Swicegood/dp/1934356891/ref=sr_1_11?ie=UTF8&amp;qid=1347828493&amp;sr=8-11&amp;keywords=nodejs">Programming Node.js</a></li>
<li><a href="http://smashingnode.com/">Smashing Node.js: JavaScript Everywhere</a></li>
<li><a href="http://www.amazon.com/Node-Beginner-Book-Manuel-Kiessling/dp/1471628442/ref=sr_1_9?ie=UTF8&amp;qid=1347828493&amp;sr=8-9&amp;keywords=nodejs">The Node Beginner Book</a></li>
</ul>

Leave a Reply

Your email address will not be published.