Skip to content

Commit

Permalink
Merge pull request #19 from rq-abrahamsson/master
Browse files Browse the repository at this point in the history
feat(pagination): added pagination urls to sitemap
  • Loading branch information
nunof07 authored Apr 7, 2021
2 parents da7a4a1 + bc3e131 commit b999f8f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/pagination/.eleventy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

const sitemap = require("../../.eleventy");

module.exports = (eleventyConfig) => {
eleventyConfig.addPlugin(sitemap, {
sitemap: {
hostname: "https://example.com",
},
});
};
27 changes: 27 additions & 0 deletions examples/pagination/_data/posts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"url": "/post_a",
"name": "Post A"
},
{
"url": "/post_b",
"name": "Post B"
},
{
"url": "/post_c",
"name": "Post C"
},
{
"url": "/post_d",
"name": "Post D"
},
{
"url": "/post_sitemap_ignore",
"name": "Post sitemap ignore",
"data": {
"sitemap": {
"ignore": true
}
}
}
]
9 changes: 9 additions & 0 deletions examples/pagination/posts.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
pagination:
data: posts
size: 1
alias: post
permalink: "{{ post.url }}/"
---

<h1>{{ post.name }}</h1>
6 changes: 6 additions & 0 deletions examples/pagination/sitemap.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
permalink: /sitemap.xml
layout: null
eleventyExcludeFromCollections: true
---
{% sitemap collections.all %}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"demo:default": "npx @11ty/eleventy --input=examples/default --output=examples/default/_site --config=examples/default/.eleventy.js",
"demo:multilingual": "npx @11ty/eleventy --input=examples/multilingual --output=examples/multilingual/_site --config=examples/multilingual/.eleventy.js",
"demo:data-file": "npx @11ty/eleventy --input=examples/data-file --output=examples/data-file/_site --config=examples/data-file/.eleventy.js",
"demo:pagination": "npx @11ty/eleventy --input=examples/pagination --output=examples/pagination/_site --config=examples/pagination/.eleventy.js",
"test": "run-p -c test:*",
"test:eslint": "eslint \"./**/*.js\" --color",
"test:prettier": "prettier --check ./**/*.js",
Expand Down
4 changes: 4 additions & 0 deletions src/isPagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";

module.exports = (item) =>
item && item.data && item.data.pagination && item.data.pagination.pages;
14 changes: 14 additions & 0 deletions src/paginationItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

const isPagination = require("./isPagination");

module.exports = (item) => {
if (!isPagination(item)) {
return [item];
}

return item.data.pagination.pages.map((page) => ({
...item,
...page,
}));
};
2 changes: 2 additions & 0 deletions src/sitemapItems.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";

const paginationItems = require("./paginationItems");
const sitemapItem = require("./sitemapItem");
const sitemapProperty = require("./sitemapProperty");

module.exports = (items, options) =>
items
.flatMap(paginationItems)
.filter((item) => !sitemapProperty(item, "ignore") && item.url)
.map((item) => sitemapItem(item, options));

0 comments on commit b999f8f

Please sign in to comment.