Skip to content

Commit cb269ec

Browse files
committed
Changes default sort on SSG and CMS pages
1 parent 358b217 commit cb269ec

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

.eleventy.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const lodashGet = require("lodash/get");
12

23
module.exports = function(eleventyConfig) {
34

@@ -104,18 +105,34 @@ module.exports = function(eleventyConfig) {
104105
});
105106

106107
// sort an array of objects by one object key value
107-
eleventyConfig.addFilter('sortKey', (arr, key) => {
108+
// can also get fancy and do a lodash.get selector string too
109+
// see https://lodash.com/docs/4.17.15#get
110+
eleventyConfig.addFilter('sortKey', (arr, selector) => {
108111
return arr.sort((a, b) => {
109-
let aKey = a[key].toLowerCase();
110-
let bKey = b[key].toLowerCase();
112+
let aKey = lodashGet(a, selector).toLowerCase();
113+
let bKey = lodashGet(b, selector).toLowerCase();
111114
if(aKey < bKey) {
112115
return -1;
113116
} else if(aKey > bKey) {
114117
return 1;
115118
}
116119
return 0;
117-
})
118-
})
120+
});
121+
});
122+
123+
eleventyConfig.addFilter('sortTools', (arr, githubData) => {
124+
return arr.sort((a, b) => {
125+
let aKey = githubData[a.data.repo] ? (githubData[a.data.repo].stars || 0) : 0;
126+
let bKey = githubData[b.data.repo] ? (githubData[b.data.repo].stars || 0) : 0;
127+
if(aKey < bKey) {
128+
return 1;
129+
} else if(aKey > bKey) {
130+
return -1;
131+
}
132+
return 0;
133+
});
134+
});
135+
119136

120137
// favicons files
121138
eleventyConfig.addPassthroughCopy("src/site/browserconfig.xml");

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"fast-glob": "^3.2.4",
2929
"gray-matter": "^4.0.2",
3030
"js-yaml": "^3.13.1",
31+
"lodash": "^4.17.20",
3132
"luxon": "^1.23.0",
3233
"markdown-it": "^11.0.1",
3334
"netlify-plugin-minify-html": "^0.2.2",

src/site/generators.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ layout: layouts/base.njk
1414

1515
<section class="cards mt-12">
1616
<div class="grid gap-4 grid-cols-1 md:grid-cols-2 lg:grid-cols-3">
17-
{% for item in collections.generators %}
17+
{% for item in collections.generators | sortTools(github) %}
1818
{{ cards.repo(item, loop.index0, github) }}
1919
{% endfor %}
2020
</div>

src/site/headless-cms.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ layout: layouts/base.njk
1414

1515
<section class="cards mt-12">
1616
<div class="grid gap-4 grid-cols-1 md:grid-cols-2 lg:grid-cols-3">
17-
{% for item in collections.cms %}
17+
{% for item in collections.cms | sortTools(github) %}
1818
{{ cards.repo(item, loop.index0, github) }}
1919
{% endfor %}
2020
</div>

0 commit comments

Comments
 (0)