Skip to content

Commit

Permalink
Allow generating html given a filename instead of a post
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
TehShrike committed Oct 1, 2015
1 parent 74f24db commit c5f8f7b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ var Ractive = require('ractive')
var extend = require('xtend')
Ractive.DEBUG = false

module.exports = function getRenderedPostWithTemplates(post, options, cb) {
module.exports = getRenderedPostWithTemplates

function getRenderedPostWithTemplates(post, options, cb) {
if (typeof post === 'string') return loadFilename(post, options, cb)

options.data = options.data || {}
cb = dezalgo(cb)
buildMapOfAllPostDependencies(post, options.linkifier, options.butler.getPost, function(err, mapOfPosts) {
Expand All @@ -27,6 +31,16 @@ module.exports = function getRenderedPostWithTemplates(post, options, cb) {
})
}

function loadFilename(filename, options, cb) {
options.butler.getPost(filename, function(err, post) {
if (err) {
cb(err)
} else {
getRenderedPostWithTemplates(post, options, cb)
}
})
}

function getContainedFileNames(ast) {
return Object.keys(ast.filter(function(chunk) {
return chunk.type === 'template'
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ butler.getPost('excellent-missive.md', function(err, post) {

# render(post, options, cb)

- `post`: a Noddity post object returned by a Noddity Butler
- `post`: either a Noddity post object, or the file name of a post to be loaded
- `options`: all the other arguments
- `butler`: a [Noddity Butler](https://www.npmjs.com/package/noddity-butler)
- `linkifier`: a [Noddity Linkifier](https://www.npmjs.com/package/noddity-linkifier)
Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,18 @@ test('filename starting with a number', function(t) {
})
})
})

test('loading based on file name', function(t) {
var state = makeTestState()

state.retrieval.addPost('file1.md', { title: 'Some title', date: new Date() }, 'This is a ::file2.md:: post that I *totally* wrote')
state.retrieval.addPost('file2.md', { title: 'Some title', date: new Date() }, 'lol yeah ::herp|wat:: ::herp|huh::')
state.retrieval.addPost('herp', { title: 'Some title', date: new Date(), markdown: false }, 'lookit {{1}}')

state.render('file1.md', {}, function(err, html) {
t.notOk(err, 'no error')
t.equal(html, '<p>This is a <p>lol yeah lookit wat lookit huh</p> post that I <em>totally</em> wrote</p>')
t.end()
})

})

0 comments on commit c5f8f7b

Please sign in to comment.