Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: include and exclude filters for pagination #3103

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
38 changes: 32 additions & 6 deletions src/Plugins/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,39 @@ class Pagination {
return false;
}

isFiltered(value) {
isIncluded(value) {
const hasInclude = "include" in this.data.pagination;
const hasExclude = "exclude" in this.data.pagination;
if (hasInclude && hasExclude) {
throw new Error("Pagination cannot have both `include` and `exclude` properties.");
}
if (hasInclude) {
let included = this.data.pagination.include;
if (Array.isArray(included)) {
return included.includes(value);
}
return included === value;
}
if (hasExclude) {
let excluded = this.data.pagination.exclude;
if (Array.isArray(excluded)) {
return !excluded.includes(value);
}
return excluded !== value;
}

// Let's keep this code for backwards compatibility to V2.
Copy link
Contributor

@uncenter uncenter Dec 19, 2023

Choose a reason for hiding this comment

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

We can remove this old filter property logic now I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd be happy to do so, but I think this should be communicated, since it affects the public API of this plugin. Is there a way to log warnings if someone still uses filters?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we add a warning when someone uses filter that says "The filter Pagination property has been deprecated in Eleventy v3. You can replace it with exclude for the same effect".

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, exactly. My question is, if there is a better way than throwing an error or using console.warn for this.

Copy link
Contributor

@uncenter uncenter Dec 19, 2023

Choose a reason for hiding this comment

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

I don't see anywhere else where we are throwing an error or console.warn-ing for a deprecation. Seems like those just get tagged as deprecated with JSDoc but aren't removed or changed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hey @zachleat, how would you want me to handle this here?
Maybe you even have an opinion on doing a breaking change.

// TODO remove in 3.0
if ("filter" in this.data.pagination) {
let filtered = this.data.pagination.filter;
if (Array.isArray(filtered)) {
return filtered.indexOf(value) > -1;
return filtered.indexOf(value) === -1;
}

return filtered === value;
return filtered !== value;
}

return false;
return true;
}

_has(target, key) {
Expand Down Expand Up @@ -162,8 +184,12 @@ class Pagination {
result = result.reverse();
}

if (this.data.pagination.filter) {
result = result.filter((value) => !this.isFiltered(value));
if (
this.data.pagination.filter ||
this.data.pagination.include ||
this.data.pagination.exclude
) {
result = result.filter((value) => this.isIncluded(value));
}

return result;
Expand Down
43 changes: 43 additions & 0 deletions test/PaginationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,49 @@ test("Page over an object (use values)", async (t) => {
);
});

test("Page over an object (excluded, array)", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/paged/pagedobjectexcludearray.njk",
"./test/stubs/",
"./dist"
);

let data = await tmpl.getData();
let pages = await tmpl.getTemplates(data);

t.is(
(await pages[0].template.render(pages[0].data)).trim(),
"<ol><li>item1</li><li>item2</li><li>item3</li><li>item5</li></ol>"
);

t.is(
(await pages[1].template.render(pages[1].data)).trim(),
"<ol><li>item6</li><li>item7</li><li>item8</li><li>item9</li></ol>"
);
});

test("Page over an object (excluded, string)", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/paged/pagedobjectexcludestring.njk",
"./test/stubs/",
"./dist"
);

let data = await tmpl.getData();
let pages = await tmpl.getTemplates(data);
t.is(pages.length, 2);

t.is(
(await pages[0].template.render(pages[0].data)).trim(),
"<ol><li>item1</li><li>item2</li><li>item3</li><li>item5</li></ol>"
);

t.is(
(await pages[1].template.render(pages[1].data)).trim(),
"<ol><li>item6</li><li>item7</li><li>item8</li><li>item9</li></ol>"
);
});

test("Page over an object (filtered, array)", async (t) => {
let tmpl = await getNewTemplate(
"./test/stubs/paged/pagedobjectfilterarray.njk",
Expand Down
18 changes: 18 additions & 0 deletions test/stubs/paged/pagedobjectexcludearray.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
pagination:
data: testdata
size: 4
exclude:
- item4
testdata:
item1: itemvalue1
item2: itemvalue2
item3: itemvalue3
item4: itemvalue4
item5: itemvalue5
item6: itemvalue6
item7: itemvalue7
item8: itemvalue8
item9: itemvalue9
---
<ol>{% for item in pagination.items %}<li>{{ item }}</li>{% endfor %}</ol>
17 changes: 17 additions & 0 deletions test/stubs/paged/pagedobjectexcludestring.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
pagination:
data: testdata
size: 4
exclude: item4
testdata:
item1: itemvalue1
item2: itemvalue2
item3: itemvalue3
item4: itemvalue4
item5: itemvalue5
item6: itemvalue6
item7: itemvalue7
item8: itemvalue8
item9: itemvalue9
---
<ol>{% for item in pagination.items %}<li>{{ item }}</li>{% endfor %}</ol>
19 changes: 19 additions & 0 deletions test/stubs/paged/pagedobjectincludearray.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
pagination:
data: testdata
size: 4
include:
- item3
- item4
testdata:
item1: itemvalue1
item2: itemvalue2
item3: itemvalue3
item4: itemvalue4
item5: itemvalue5
item6: itemvalue6
item7: itemvalue7
item8: itemvalue8
item9: itemvalue9
---
<ol>{% for item in pagination.items %}<li>{{ item }}</li>{% endfor %}</ol>
18 changes: 18 additions & 0 deletions test/stubs/paged/pagedobjectincludeexclude.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
pagination:
data: testdata
size: 4
include: item4
exclude: item5
testdata:
item1: itemvalue1
item2: itemvalue2
item3: itemvalue3
item4: itemvalue4
item5: itemvalue5
item6: itemvalue6
item7: itemvalue7
item8: itemvalue8
item9: itemvalue9
---
<ol>{% for item in pagination.items %}<li>{{ item }}</li>{% endfor %}</ol>
17 changes: 17 additions & 0 deletions test/stubs/paged/pagedobjectincludestring.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
pagination:
data: testdata
size: 4
include: item4
testdata:
item1: itemvalue1
item2: itemvalue2
item3: itemvalue3
item4: itemvalue4
item5: itemvalue5
item6: itemvalue6
item7: itemvalue7
item8: itemvalue8
item9: itemvalue9
---
<ol>{% for item in pagination.items %}<li>{{ item }}</li>{% endfor %}</ol>