Skip to content

Commit

Permalink
Expose getPages utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Dec 4, 2015
1 parent 5207bce commit 77d4fe5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Within the template you specified, you will have access to pagination specific h

* **pagination.num** The current page number.
* **pagination.index** The current page index (`num - 1`).
* **pagination.getPages(num)** Get an array of `num` pages with the current page as centered as possible
* **pagination.name** The page name from `groupBy`. If no `groupBy` was used, it is the current page number as a string.
* **pagination.files** All the files to render in the current page (E.g. array of `x` articles).
* **pagination.pages** References to every page in the collection (E.g. used to render pagination numbers).
Expand Down
31 changes: 28 additions & 3 deletions metalsmith-pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ module.exports = function (options) {
index: length,
num: length + 1,
pages: pages,
files: []
files: [],
getPages: createPagesUtility(pages, length)
}

// Generate the page data.
Expand Down Expand Up @@ -138,8 +139,8 @@ module.exports = function (options) {
pageMap[name].files.push(file)
})

// Add references to the first and last pages.
pages.forEach(function (page) {
// Add page utilities.
pages.forEach(function (page, index) {
page.pagination.first = pages[0]
page.pagination.last = pages[pages.length - 1]
})
Expand Down Expand Up @@ -175,3 +176,27 @@ function interpolate (path, data) {
function groupByPagination (file, index, options) {
return Math.ceil((index + 1) / options.perPage)
}

/**
* Create a "get pages" utility for people to use when rendering.
*
* @param {Array} pages
* @param {number} index
* @return {Function}
*/
function createPagesUtility (pages, index) {
return function getPages (number) {
var offset = Math.floor(number / 2)
var start, end

if (index + offset >= pages.length) {
start = Math.max(0, pages.length - number)
end = pages.length
} else {
start = Math.max(0, index - offset)
end = Math.min(start + number, pages.length)
}

return pages.slice(start, end)
}
}
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ describe('metalsmith collections paginate', function () {
expect(firstPage.pagination.num).to.equal(1)
expect(firstPage.pagination.pages).to.equal(metadata.collections.articles.pages)

expect(pageOne.pagination.getPages(2)).to.deep.equal([firstPage, pageTwo])
expect(firstPage.pagination.getPages(2)).to.deep.equal([firstPage, pageTwo])
expect(pageTwo.pagination.getPages(2)).to.deep.equal([firstPage, pageTwo])
expect(pageThree.pagination.getPages(2)).to.deep.equal([pageTwo, pageThree])

expect(pageOne.pagination.getPages(3)).to.deep.equal([firstPage, pageTwo, pageThree])
expect(pageTwo.pagination.getPages(3)).to.deep.equal([firstPage, pageTwo, pageThree])
expect(pageThree.pagination.getPages(3)).to.deep.equal([firstPage, pageTwo, pageThree])

expect(pageTwo.pagination.getPages(100)).to.deep.equal([firstPage, pageTwo, pageThree])

return done(err)
})
})
Expand Down

0 comments on commit 77d4fe5

Please sign in to comment.