Skip to content

Commit

Permalink
Add showFuture as an option to fix #47, fix getPageCount to work with…
Browse files Browse the repository at this point in the history
… drafts to fix #48
  • Loading branch information
jsantell committed Sep 19, 2013
1 parent c0b13b2 commit 358cc4a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
1 change: 1 addition & 0 deletions lib/poet/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function createDefaults () {
postsPerPage: 5,
posts: './_posts/',
showDrafts: process.env.NODE_ENV !== 'production',
showFuture: process.env.NODE_ENV !== 'production',
metaFormat: 'json',
readMoreLink: readMoreLink,
readMoreTag: '<!--more-->',
Expand Down
30 changes: 11 additions & 19 deletions lib/poet/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ function createHelpers (poet) {
getPost: function (title) { return poet.posts[title]; },
getPosts: function (from, to) {
var posts = getPosts(poet);
if (!options.showDrafts) {
posts = posts.filter(function (post) {
return !post.draft;
});
}

if (from != null && to != null)
posts = posts.slice(from, to);

Expand All @@ -36,23 +30,11 @@ function createHelpers (poet) {
return Math.ceil(getPosts(poet).length / options.postsPerPage);
},
postsWithTag: function (tag) {
if (!options.showDrafts) {
return getPosts(poet).filter(function (post) {
return post.tags && ~post.tags.indexOf(tag) && !post.draft;
});
}

return getPosts(poet).filter(function (post) {
return post.tags && ~post.tags.indexOf(tag);
});
},
postsWithCategory: function (category) {
if (!options.showDrafts) {
return getPosts(poet).filter(function (post) {
return post.category === category && !post.draft;
});
}

return getPosts(poet).filter(function (post) {
return post.category === category;
});
Expand Down Expand Up @@ -86,7 +68,17 @@ module.exports = createHelpers;
*/

function getPosts (poet) {
return poet.cache.posts || (poet.cache.posts = utils.sortPosts(poet.posts));
if (poet.cache.posts)
return poet.cache.posts;

var posts = utils.sortPosts(poet.posts).filter(function (post) {
// Filter out draft posts if showDrafts is false
return (poet.options.showDrafts || !post.draft) &&
// Filter out posts in the future if showFuture is false
(poet.options.showFuture || post.date < Date.now());
});

return poet.cache.posts = posts;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/_postsJson/test2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"title" : "Test Post Two",
"tags" : [ "a", "b", "c" ],
"category" : "testing",
"date" : "7-15-2012",
"date" : "7-15-2020",
"preview" : "*some content*"
}}}

Expand Down
54 changes: 46 additions & 8 deletions test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ describe('helpers.getPostCount()', function () {
done();
}, done);
});

it('should return correct count of posts with `showDrafts` false', function (done) {
setup(function (poet) {
poet.options.showDrafts = false;
expect(poet.helpers.getPostCount()).to.be.equal(5);
expect(poet.helpers.getPosts().length).to.be.equal(5);
done();
}, done);
});

it('should return correct count of posts with `showFuture` false', function (done) {
setup(function (poet) {
poet.options.showFuture = false;
expect(poet.helpers.getPostCount()).to.be.equal(5);
expect(poet.helpers.getPosts().length).to.be.equal(5);
done();
}, done);
});
});

describe('helpers.getPost(title)', function () {
Expand Down Expand Up @@ -97,6 +115,15 @@ describe('helpers.getPageCount()', function () {
done();
}, done);
});

it('returns the correct number of pages based off of drafts', function (done) {
setup(function (poet) {
// Based off of 5 non-draft posts and default postsPerPage of 5
poet.options.showDrafts = false;
expect(poet.helpers.getPageCount()).to.be.equal(1);
done();
}, done);
});
});

describe('helpers.postsWithTag()', function () {
Expand All @@ -108,14 +135,19 @@ describe('helpers.postsWithTag()', function () {
done();
}, done);
});
});

describe('helpers.postsWithTag()', function () {
it('should not see drafts on production', function (done) {
it('should not see drafts when `showDrafts` false', function (done) {
setup(function (poet) {
poet.options.showDrafts = false;
var posts = poet.helpers.postsWithTag('d');
(posts.length).should.equal(1);
expect(poet.helpers.postsWithTag('d').length).to.be.equal(1);
done();
}, done);
});

it('should not see future posts when `showFuture` false', function (done) {
setup(function (poet) {
poet.options.showFuture = false;
expect(poet.helpers.postsWithTag('c').length).to.be.equal(0);
done();
}, done);
});
Expand All @@ -130,17 +162,23 @@ describe('helpers.postsWithCategories()', function () {
done();
}, done);
});
});

describe('helpers.postsWithCategories()', function () {
it('should not see drafts on production', function (done) {
it('should not see drafts when `showDrafts` false', function (done) {
setup(function (poet) {
poet.options.showDrafts = false;
var posts = poet.helpers.postsWithCategory('other cat');
(posts.length).should.equal(1);
done();
}, done);
});

it('should not see future posts when `showFuture` false', function (done) {
setup(function (poet) {
poet.options.showFuture = false;
expect(poet.helpers.postsWithCategory('testing').length).to.be.equal(2);
done();
}, done);
});
});

function setup (callback, done) {
Expand Down

0 comments on commit 358cc4a

Please sign in to comment.