Skip to content

Commit

Permalink
Merge pull request #21 from quasibit/issue/20/filter-duplicated-items
Browse files Browse the repository at this point in the history
Issue/20/filter duplicated items
  • Loading branch information
nunof07 authored Apr 19, 2021
2 parents b44690c + bdbce09 commit 8d5a0c5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js eol=lf
1 change: 1 addition & 0 deletions examples/pagination/posts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pagination:
data: posts
size: 1
alias: post
addAllPagesToCollections: true
permalink: "{{ post.url }}/"
---

Expand Down
16 changes: 16 additions & 0 deletions src/isDuplicated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

module.exports = (item, index, collection) => {
if (!collection) {
return false;
}

const notFoundIndex = -1;
const firstIndex = collection.findIndex((other) => other.url === item.url);

if (firstIndex === notFoundIndex) {
return false;
}

return index !== firstIndex;
};
5 changes: 5 additions & 0 deletions src/isIgnored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

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

module.exports = (item) => sitemapProperty(item, "ignore") || !item.url;
8 changes: 6 additions & 2 deletions src/sitemapItems.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"use strict";

const isIgnored = require("./isIgnored");
const isDuplicated = require("./isDuplicated");
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)
.filter(
(item, index, collection) =>
!isIgnored(item) && !isDuplicated(item, index, collection)
)
.map((item) => sitemapItem(item, options));

0 comments on commit 8d5a0c5

Please sign in to comment.