{"id":18434716,"url":"https://github.com/assemble/assemble-pager","last_synced_at":"2025-04-07T19:31:55.312Z","repository":{"id":57184966,"uuid":"81495615","full_name":"assemble/assemble-pager","owner":"assemble","description":"Assemble plugin that adds prev/next pager information to the context, for a specific collection, specific files or all files in a stream.","archived":false,"fork":false,"pushed_at":"2017-02-21T01:02:08.000Z","size":17,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-01T09:11:19.597Z","etag":null,"topics":["assemble","collections","pager","pages","paginate","pagination","plugin"],"latest_commit_sha":null,"homepage":"https://github.com/assemble/assemble-pager","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/assemble.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/contributing.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-09T21:15:52.000Z","updated_at":"2019-07-15T19:00:48.000Z","dependencies_parsed_at":"2022-09-17T07:20:14.452Z","dependency_job_id":null,"html_url":"https://github.com/assemble/assemble-pager","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/assemble%2Fassemble-pager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/assemble%2Fassemble-pager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/assemble%2Fassemble-pager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/assemble%2Fassemble-pager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/assemble","download_url":"https://codeload.github.com/assemble/assemble-pager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247716356,"owners_count":20984226,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assemble","collections","pager","pages","paginate","pagination","plugin"],"created_at":"2024-11-06T06:05:04.055Z","updated_at":"2025-04-07T19:31:54.915Z","avatar_url":"https://github.com/assemble.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# assemble-pager [![NPM version](https://img.shields.io/npm/v/assemble-pager.svg?style=flat)](https://www.npmjs.com/package/assemble-pager) [![NPM monthly downloads](https://img.shields.io/npm/dm/assemble-pager.svg?style=flat)](https://npmjs.org/package/assemble-pager)  [![NPM total downloads](https://img.shields.io/npm/dt/assemble-pager.svg?style=flat)](https://npmjs.org/package/assemble-pager) [![Linux Build Status](https://img.shields.io/travis/assemble/assemble-pager.svg?style=flat\u0026label=Travis)](https://travis-ci.org/assemble/assemble-pager)\n\n\u003e Assemble plugin that adds prev/next pager information to the context.\n\n## Table of Contents\n\n- [Install](#install)\n- [Usage](#usage)\n- [Specify a collection](#specify-a-collection)\n- [Excluding files](#excluding-files)\n- [Template usage](#template-usage)\n  * [Helpers](#helpers)\n  * [Example usage](#example-usage)\n  * [Troubleshooting](#troubleshooting)\n- [How it works](#how-it-works)\n- [About](#about)\n\n_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save assemble-pager\n```\n\nAdds data to the context to enable creating pagers that work like this:\n\n![image](https://cloud.githubusercontent.com/assets/383994/22801836/730eacdc-eedc-11e6-9dbb-3919875ef9a7.png)\n\n## Usage\n\n```js\nvar assemble = require('assemble');\nvar pager = require('assemble-pager');\nvar app = assemble();\n\n// register the plugin\napp.use(pager());\n\napp.task('site', function() {\n  return app.src('src/*.md')\n    .pipe(app.pager()) // then add \"app.pager()\" before \"app.renderFile()\"\n    .pipe(app.renderFile())\n    .pipe(app.dest('blog'));\n});\n```\n\nWhen `app.pager()` is called, it adds the `pager` variable to the context so that it's available to all views in the collection being rendered.\n\n## Specify a collection\n\n```js\n// only add paging to the \"posts\" collection\n.pipe(app.pager({collection: 'posts'}))\n```\n\n## Excluding files\n\nIf you don't [specify a collection](#specify-a-collection), all files in the stream will be buffered and included. However, you can selectively exclude files by setting `file.data.pager` to `false`, either in another plugin, middleware, or by defining it on yaml front-matter.\n\n**Examples**\n\n```js\n// plugin, defined before `app.pager()`\n.pipe(through.obj(function(file, enc, next) {\n  file.data.pager = false;\n  next(null, file);\n}))\n\n// middleware\napp.onLoad(/\\.foo$/, function(file, next) {\n  file.data.pager = false;\n  next();\n});\n```\n\nOr yaml front-matter\n\n```handlebars\n---\npager: false\n---\n\nThis is a page that should be skipped by `app.pager()`\n```\n\n## Template usage\n\nThe `pager` variable is an object with the following properties:\n\n* `pager.prev` **{Object}** - the previous view that was rendered. This is an actual view, so you can get any data you need from it (like `pager.prev.path` or `pager.prev.data.title`, etc)\n* `pager.next` **{Object}** - the next view to be rendered. This is an actual view, so you can get any data you need from it (like `pager.next.path` or `pager.next.data.title`, etc)\n* `pager.current` **{Object}** - the view that is currently being rendered\n* `pager.first` **{Object}** - Returns the first file in the list\n* `pager.last` **{Object}** - Returns the last file in the list\n* `pager.isFirst` **{Boolean}** - returns true if the \"current\" file is the first to be rendered\n* `pager.isLast` **{Boolean}** - returns true if the \"current\" file is the last to be rendered\n* `pager.index` **{Number}** - the list index of the current file being rendered\n\n### Helpers\n\nSome helpers are included with this plugin. To use them, do the following:\n\n```js\nvar pager = require('assemble-pager');\napp.helpers(pager.helpers);\n```\n\nWhich adds the following helpers:\n\n* `relative`- calculates the relative path from file `A` to file `B` (e.g. \"current\" file to next or previous file)\n* `ifFirst`- returns true if the current file being rendered is the first file\n* `ifLast`- returns true if the current file being rendered is the last file\n* `next`- used in `href=\"\"`, returns either the relative path to the \"next\" file, or `#` if the current file being rendered is the last file\n* `prev`- used in `href=\"\"`, returns either the relative path to the \"previous\" file, or `#` if the current file being rendered is the first file\n* `attr`- stringify the options hash arguments to HTML attributes. For example, you can use this to conditionally add a class for a disabled next/prev link: `{{ifFirst (attr class=\"disabled\")}}`, or `{{ifLast (attr class=\"disabled\")}}` etc.\n\n**Overriding helpers**\n\nHelpers are exposed as an object so that you can choose to register them if you want, and/or override them if necessary:\n\nFor example, this is how the included \"relative\" helper works:\n\n```js\nvar relative = require('relative');\n\napp.helper('relative', function(from, to) {\n  return relative(from.data.path, to.data.path);\n});\n```\n\n### Example usage\n\nSince `pager` is just another variable on the context, you can do anything you normally would with a variable in your handlebars templates.\n\n```handlebars\n\u003c!-- generate relative paths using the relative helper\n   from the \"usage\" section above --\u003e \n\u003ca href=\"{{#if pager.prev}}{{relative pager.current pager.prev}}{{/if}}\"\u003ePrev\u003c/a\u003e\n```\n\nOr you can create a block:\n\n```handlebars\n{{#pager}}\n\u003c!-- generate relative paths using \"link-to\" --\u003e \n\u003ca href=\"{{#if prev}}{{relative current prev}}{{/if}}\"\u003ePrev\u003c/a\u003e\n{{/pager}}\n```\n\n**Partials**\n\nTo simplify paging even more, you can define partials like the following:\n\n```js\napp.partial('prev', '\u003ca href=\"{{prev pager}}\"{{ifFirst pager (attr class=\"disabled\")}}\u003ePrev\u003c/a\u003e');\napp.partial('next', '\\n\u003ca href=\"{{next pager}}\"{{ifLast pager (attr class=\"disabled\")}}\u003eNext\u003c/a\u003e');\napp.partial('pager', '{{\u003e prev }}\\n{{\u003e next }}');\n```\n\n### Troubleshooting\n\nThe easiest way to see how this works is to log out the `pager` variables, to see what's on the context. You can use the built-in `log` helper to do this:\n\n```handlebars\n{{log pager}}\n```\n\n## How it works\n\nThis is really all the plugin does, with the addition of error and options handling (a commented version is below). You can copy this and create your own plugin if you need to:\n\n```js\nfunction pagerPlugin() {\n  var list = new app.List({pager: true});\n  return through.obj(function(file, enc, next) {\n    list.addItem(file);\n    next();\n  }, function(cb) {\n    for (var i = 0; i \u003c list.items.length; i++) {\n      this.push(item);\n    }\n    cb();\n  });\n}\n```\n\nWith comments:\n\n```js\nfunction pagerPlugin() {\n  // create a new assemble `List`. Lists are like collections,\n  // but the items are stored as an array, versus an object\n  var list = new app.List({pager: true});\n\n  return through.obj(function(file, enc, next) {\n    // add files to the list as they come through the stream\n    list.addItem(file);\n\n    // don't pass the file through, we'll do \n    // that in the flush function\n    next();\n  }, function(cb) {\n\n    // all of the files have now been buffered onto the \n    // `list.items` array. We can now loop over that array\n    // and push the items back into the stream (it's important\n    // for all the items to be buffered this way so that all \n    // items are available on the context before anything is\n    // rendered, or items will be missing)\n    for (var i = 0; i \u003c list.items.length; i++) {\n      this.push(item); //\u003c= push the item into the stream\n    }\n    cb();\n  });\n}\n```\n\n## About\n\n### Contributing\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\nPlease read the [contributing guide](.github/contributing.md) for advice on opening issues, pull requests, and coding standards.\n\n### Building docs\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n### Running tests\n\nRunning and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n### Author\n\n**Jon Schlinkert**\n\n* [github/jonschlinkert](https://github.com/jonschlinkert)\n* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)\n\n### License\n\nCopyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).\nMIT\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 09, 2017._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fassemble%2Fassemble-pager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fassemble%2Fassemble-pager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fassemble%2Fassemble-pager/lists"}