diff --git a/index.js b/index.js index d3cf638..04df880 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ function paginate (filePath, collection, fileName, files, iteratee) { const years = Object.keys(postsByYear).sort().reverse(); const latestYear = years[0]; - let last = origFile; + let lastYear = origFile; // no posts had date field :( if (!years.length) { @@ -38,20 +38,22 @@ function paginate (filePath, collection, fileName, files, iteratee) { const posts = postsByYear[year]; const cloneName = `${baseName}-${year}${ext}`; - const clone = cloneDeepWith(origFile, (value) => { + const currentYear = cloneDeepWith(origFile, (value) => { if (Buffer.isBuffer(value)) { return value.slice(); } }); - last.pagination.next = clone; - clone.pagination.year = year; - clone.pagination.prev = last; - clone.pagination.posts = posts.map(iteratee); + lastYear.pagination.next = currentYear; + currentYear.pagination = { + year, + prev: lastYear, + posts: posts.map(iteratee) + } - files[cloneName] = clone; + files[cloneName] = currentYear; - last = clone; + lastYear = currentYear; }); } diff --git a/test/pagination.test.js b/test/pagination.test.js index 21ec373..fce455e 100644 --- a/test/pagination.test.js +++ b/test/pagination.test.js @@ -30,7 +30,15 @@ describe('Yearly pagination', () => { for (let i = 0; i < 10; i++) { const name = `content/posts/post-${i}.md`; - const date = (i < 5) ? getDate(2016) : getDate(2015); + let date; + + if (i <= 3) { + date = getDate(2016); + } else if (i > 3 && i <= 6) { + date = getDate(2015); + } else { + date = getDate(2014); + } files[name] = { title: `Post Number ${i}`, @@ -59,7 +67,7 @@ describe('Yearly pagination', () => { } } - cPages.should.equal(2); + cPages.should.equal(3); done(); }); }); @@ -95,7 +103,7 @@ describe('Yearly pagination', () => { } } - cPages.should.equal(1); + cPages.should.equal(2); done(); }); }); @@ -123,8 +131,10 @@ describe('Yearly pagination', () => { pagination()(files, metalsmith, () => { const firstPage = files['blog.md']; const secondPage = files['blog-2015.md']; + const thirdPage = files['blog-2014.md']; firstPage.pagination.next.should.equal(secondPage); + secondPage.pagination.next.should.equal(thirdPage); done(); }); }); @@ -133,8 +143,10 @@ describe('Yearly pagination', () => { pagination()(files, metalsmith, () => { const firstPage = files['blog.md']; const secondPage = files['blog-2015.md']; + const thirdPage = files['blog-2014.md']; secondPage.pagination.prev.should.equal(firstPage); + thirdPage.pagination.prev.should.equal(secondPage); done(); }); }); @@ -150,7 +162,7 @@ describe('Yearly pagination', () => { it('pagination.next is not defined for the last page', (done) => { pagination()(files, metalsmith, () => { - const lastPage = files['blog-2015.md']; + const lastPage = files['blog-2014.md']; lastPage.pagination.should.not.have.property('next'); done();