- Filtering Markdown content in Jade / Node.js
- html = function(req, res, next) {
- if (!/.html$/.test(res.viewPath)) return next();
- return fs.readFile(res.viewPath, "utf8", function(error, htmlText) {
- res.html = htmlText;
- return next(error);
- });
- };
- markdownToHTML = function(req, res, next) {
- if (!/.md$/.test(res.viewPath)) return next();
- return fs.readFile(res.viewPath, "utf8", function(error, markdownText) {
- res.html = markdown(markdownText);
- return next(error);
- });
- };
- blogArticle = function(req, res, next) {
- var footerPath, post;
- post = res.post;
- footerPath = path.join(__dirname, "..", "templates", "blog_layout.jade");
- return fs.readFile(footerPath, "utf8", function(error, jadeText) {
- var footerFunc;
- if (error) return next(error);
- footerFunc = jade.compile(jadeText);
- res.html = footerFunc({
- post: post,
- body: res.html
- });
- return next();
- });
- };
- exports.layout = function(req, res, next) {
- var layoutPath;
- layoutPath = path.join(__dirname, "..", "templates", "layout.jade");
- return fs.readFile(layoutPath, "utf8", function(error, jadeText) {
- var layoutFunc, locals;
- layoutFunc = jade.compile(jadeText, {
- filename: layoutPath
- });
- locals = {
- config: config,
- title: "",
- body: res.html || ""
- };
- res.html = layoutFunc(locals);
- return next(error);
- });
- };
- exports.domify = function(req, res, next) {
- return jsdom.env(res.html, [jqueryPath], function(error, dom) {
- if (error) return next(error);
- res.dom = dom;
- dom.toMarkup = function() {
- this.window.$("script").last().remove();
- return this.window.document.doctype + this.window.document.innerHTML;
- };
- return next(error);
- });
- };
- exports.flickr = function(req, res, next) {
- var $ = res.dom.window.$;
- $("flickrshow").each(function(index, elem) {
- var $elem, URLs;
- $elem = $(elem);
- URLs = $elem.attr("href");
- return $elem.replaceWith(flickrshowTemplate.replace(/{URLs}/g, URLs));
- });
- return next();
- };
- exports.youtube = function(req, res, next) {
- var $ = res.dom.window.$;
- $("youtube").each(function(index, elem) {
- var $elem, URL;
- $elem = $(elem);
- URL = $elem.attr("href");
- return $elem.replaceWith(youtubeTemplate.replace(/{URL}/, URL));
- });
- return next();
- };
- postTitle = function(req, res, next) {
- var $;
- $ = res.dom.window.$;
- $("title").text(res.post.title + " | Peter Lyons");
- return next();
- };
- exports.undomify = function(req, res, next) {
- res.html = res.dom.toMarkup();
- return next();
- };
- exports.send = function(req, res) {
- return res.send(res.html);
- };
- postMiddleware = [
- loadPost,
- html,
- markdownToHTML,
- blogArticle,
- layout,
- domify,
- postTitle,
- flickr,
- youtube,
- undomify,
- send
- ]
- app.get("/your/uri", postMiddleware);