Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, we mutated the .pagination object which was a copy of last year's pagination object -- that included the .pagination.next property.

Now that we're always re-creating a new .pagination object, we're ensuring that the last year's .pagination.next isn't present anymore.

That property is only set in the next iteration, which doesn't happen for the oldest year -- voila, bug fixed.

lastYear.pagination.next = currentYear;
currentYear.pagination = {
year,
prev: lastYear,
posts: posts.map(iteratee)
}

files[cloneName] = clone;
files[cloneName] = currentYear;

last = clone;
lastYear = currentYear;
});
}

Expand Down
20 changes: 16 additions & 4 deletions test/pagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -59,7 +67,7 @@ describe('Yearly pagination', () => {
}
}

cPages.should.equal(2);
cPages.should.equal(3);
done();
});
});
Expand Down Expand Up @@ -95,7 +103,7 @@ describe('Yearly pagination', () => {
}
}

cPages.should.equal(1);
cPages.should.equal(2);
done();
});
});
Expand Down Expand Up @@ -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();
});
});
Expand All @@ -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();
});
});
Expand All @@ -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();
Expand Down