Skip to content

Instantly share code, notes, and snippets.

@balupton
Last active April 3, 2017 04:18
Show Gist options
  • Save balupton/4555732 to your computer and use it in GitHub Desktop.
Save balupton/4555732 to your computer and use it in GitHub Desktop.
DocPad: Query custom category objects (via parse event)
# Prepare
_ = require('underscore')
categories = {}
# Configure DocPad
docpadConfig =
templateData: ->
getDocumentsForCategory: (categoryId) ->
return @getCollection('documents').findAll(categories: $has: categoryId)
getCategoriesForDocument: (document) ->
document ?= @getDocument()
categoryIds = document.get('categories')
categories = @categories[categoryId] for categoryId in categoryIds
return categories
getCategories: ->
return categories
events:
parseAfter: ->
@getCollection('documents').forEach (document) ->
documentCategories = document.getMeta().get('categories')
for documentCategory in documentCategories
# Update the global categories listing with our document's categories
categories[documentCategory.id] ?= {}
_.extend(categories[documentCategory.id], documentCategory)
# Update our document's categories with a simple id listing
document.set(categories: _.pluck(documentCategories, 'id'))
# Export
module.exports = docpadConfig
title: 'Happy new year!'
layout: 'post'
categories: [
		id: 'a'
		description: 'a is the first letter of the alphabet'
	,
		id: 'b'
		description: 'b is the second letter of the alphabet'
]

Happy new year. Boom boom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment