UNPKG

docpad-plugin-navlinks

Version:

Docpad plugin which adds the ability to generate a navigation bar for documents with links to the next and previous document of a specified collection

85 lines (75 loc) 3.43 kB
// Generated by CoffeeScript 1.7.1 (function() { 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 NavLinksPlugin; return NavLinksPlugin = (function(_super) { __extends(NavLinksPlugin, _super); function NavLinksPlugin() { return NavLinksPlugin.__super__.constructor.apply(this, arguments); } NavLinksPlugin.prototype.name = 'navlinks'; NavLinksPlugin.prototype.config = { collections: {} }; NavLinksPlugin.prototype.renderBefore = function(opts, next) { var collection, collectionName, config, index, sorting, templateData, _ref; collection = opts.collection, templateData = opts.templateData; config = this.config; _ref = config.collections; for (collectionName in _ref) { sorting = _ref[collectionName]; collection = this.docpad.getCollection(collectionName); if (collection != null) { this.docpad.log('info', 'Adding navlinks for collection: ', collectionName); index = 0; collection.forEach(function(document) { var navlinks, _ref1, _ref2, _ref3, _ref4; navlinks = { navlinks: { title: { next: (_ref1 = collection.at(index + sorting)) != null ? _ref1.get('title') : void 0, prev: (_ref2 = collection.at(index - sorting)) != null ? _ref2.get('title') : void 0 }, url: { next: (_ref3 = collection.at(index + sorting)) != null ? _ref3.get('url') : void 0, prev: (_ref4 = collection.at(index - sorting)) != null ? _ref4.get('url') : void 0 } } }; document.set(navlinks); return index++; }); } } return next(); return this; }; NavLinksPlugin.prototype.extendTemplateData = function(_arg) { var templateData; templateData = _arg.templateData; templateData.getNavLinks = function(styles) { var navLinks, nextLink, prevLink; if (styles == null) { styles = []; } styles = styles.concat(['navlinks']).join(' '); navLinks = this.getDocument().get('navlinks'); if (navLinks != null) { prevLink = nextLink = ''; if (navLinks.url.prev != null) { prevLink = "<a href=\"" + navLinks.url.prev + "\" title=\"Previous Post: " + navLinks.title.prev + "\" class=\"navlinks-prev\">&laquo; " + navLinks.title.prev + "</a>"; } if (navLinks.url.next != null) { nextLink = "<a href=\"" + navLinks.url.next + "\" title=\"Next Post: " + navLinks.title.next + "\" class=\"navlinks-next\">" + navLinks.title.next + " &raquo;</a>"; } return "<div class=\"" + styles + "\">\n " + prevLink + "\n " + nextLink + "\n</div>"; } }; return this; }; return NavLinksPlugin; })(BasePlugin); }; }).call(this);