Skip to content

Instantly share code, notes, and snippets.

@mikeumus
Created August 7, 2013 22:21
Show Gist options
  • Save mikeumus/6179396 to your computer and use it in GitHub Desktop.
Save mikeumus/6179396 to your computer and use it in GitHub Desktop.
DocPad NativeComments Plugin translation from coffee to js via js2coffee.org
var __hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
module.exports = function(BasePlugin) {
var NativeCommentsPlugin;
return NativeCommentsPlugin = (function(_super) {
__extends(NativeCommentsPlugin, _super);
function NativeCommentsPlugin() {
return NativeCommentsPlugin.__super__.constructor.apply(this, arguments);
}
NativeCommentsPlugin.prototype.name = 'nativecomments';
NativeCommentsPlugin.prototype.config = {
collectionName: 'comments',
relativeDirPath: 'comments',
postUrl: '/comment',
extension: '.html.md',
blockHtml: "<section class=\"comments\">\n\n <div class=\"comments-new\">\n <h2>New Comment</h2>\n\n <p>Enter your comment here. Markdown supported.</p>\n\n <form action=\"/comment\" method=\"POST\">\n <input type=\"hidden\" name=\"for\" value=\"<%= @document.relativeBase %>\" />\n <label>Author: <input type=\"author\" name=\"author\" /></label>\n <label>Title: <input type=\"text\" name=\"title\" /></label>\n <label>Body: <textarea name=\"body\"></textarea></label>\n <input type=\"submit\" value=\"Post Comment\" />\n </form>\n </div>\n\n <div class=\"comments-list\">\n <h2>Comments</h2>\n <% if @getComments().length is 0: %>\n <p>No comments yet</p>\n <% else: %>\n <ul>\n <% for comment in @getComments().toJSON(): %>\n <li>\n <a href=\"<%=comment.url%>\"><%=comment.title or comment.contentRenderedWithoutLayouts%></a>\n </li>\n <% end %>\n </ul>\n <% end %>\n </div>\n\n</section>".replace(/^\s+|\n\s*|\s+$/g, '')
};
NativeCommentsPlugin.prototype.extendTemplateData = function(_arg) {
var docpad, plugin, templateData;
templateData = _arg.templateData;
plugin = this;
docpad = this.docpad;
templateData.getCommentsBlock = function() {
this.referencesOthers();
return plugin.getConfig().blockHtml;
};
templateData.getComments = function() {
return docpad.getCollection(plugin.getConfig().collectionName).findAll({
"for": this.document.relativeBase
});
};
return this;
};
NativeCommentsPlugin.prototype.extendCollections = function() {
var comments, config, database, docpad;
config = this.getConfig();
docpad = this.docpad;
database = docpad.getDatabase();
comments = database.findAllLive({
relativeDirPath: {
$startsWith: config.relativeDirPath
}
}, [
{
date: -1
}
]);
docpad.setCollection(config.collectionName, comments);
return this;
};
NativeCommentsPlugin.prototype.serverExtend = function(opts) {
var database, docpad, plugin, server;
server = opts.server;
plugin = this;
docpad = this.docpad;
database = docpad.getDatabase();
server.all(this.getConfig().postUrl, function(req, res, next) {
var config, document, documentAttributes, now, nowString, nowTime, redirect, _ref, _ref1, _ref2;
config = plugin.getConfig();
now = new Date(req.body.date || null);
nowTime = now.getTime();
nowString = now.toString();
redirect = (_ref = (_ref1 = req.body.redirect) != null ? _ref1 : req.query.redirect) != null ? _ref : 'back';
documentAttributes = {
data: req.body.body || '',
meta: {
title: req.body.title || ("Comment at " + nowString),
"for": req.body["for"] || '',
author: req.body.author || '',
date: now,
fullPath: docpad.config.documentsPaths[0] + ("/" + config.relativeDirPath + "/" + nowTime + config.extension)
}
};
document = docpad.createDocument(documentAttributes);
if ((_ref2 = config.injectDocumentHelper) != null) {
_ref2.call(me, document);
}
database.add(document);
docpad.once('generateAfter', function() {
if (redirect === 'back') {
return res.redirect('back');
} else if (redirect === 'document') {
return res.redirect(document.get('url'));
} else {
return res.json(documentAttributes);
}
});
return document.writeSource({
cleanAttributes: true
}, function(err) {
if (err) {
return next(err);
}
});
});
return this;
};
return NativeCommentsPlugin;
})(BasePlugin);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment