From: Cameron Otsuka Date: Fri, 20 Dec 2024 03:53:49 +0000 (-0800) Subject: limit number of articles to show on index, add full articles archive page X-Git-Tag: v1.0.0~75 X-Git-Url: https://git.otsuka.systems/?a=commitdiff_plain;h=f6f31a2b8d7088d8afc1279bcaf2fd1cb08ab09a;p=cotsuka.github.io limit number of articles to show on index, add full articles archive page --- diff --git a/_includes/components/articleslist.njk b/_includes/components/articleslist.njk index b4c30fe..d038bb0 100644 --- a/_includes/components/articleslist.njk +++ b/_includes/components/articleslist.njk @@ -1,6 +1,11 @@
-{% set articleslist = collections.articles %} -{%- for article in articleslist | reverse %} +{% if maxPosts %} + {% set numPosts = collections.articles | length | min(maxPosts) %} + {% set articleslist = collections.articles | reverse | head(numPosts)%} +{% else %} + {% set articleslist = collections.articles | reverse %} +{% endif %} +{%- for article in articleslist %}
{{ article.data.title }}
{{ article.data.description }}
{%- endfor %} diff --git a/_includes/layouts/home.njk b/_includes/layouts/home.njk index 517bb92..7ae154e 100644 --- a/_includes/layouts/home.njk +++ b/_includes/layouts/home.njk @@ -1,5 +1,6 @@ --- layout: layouts/base.njk +maxPosts: 5 ---
@@ -7,7 +8,7 @@ layout: layouts/base.njk
-

Articles

+

Articles

{%- include "components/articleslist.njk" %}
diff --git a/content/articles.njk b/content/articles.njk new file mode 100644 index 0000000..c35f501 --- /dev/null +++ b/content/articles.njk @@ -0,0 +1,7 @@ +--- +layout: layouts/base.njk +--- + +

Articles

+ +{% include "components/articleslist.njk" %} diff --git a/eleventy.config.js b/eleventy.config.js index 85f1b67..a264a30 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -25,6 +25,17 @@ module.exports = function (eleventyConfig) { eleventyConfig.addFilter("cssmin", function (code) { return new cleanCSS({}).minify(code).styles; }); + eleventyConfig.addFilter("head", (array, n) => { + // Get the first `n` elements of a collection. + if(!Array.isArray(array) || array.length === 0) { + return []; + } + if( n < 0 ) { + return array.slice(n); + } + + return array.slice(0, n); + }); eleventyConfig.addFilter("htmlDateString", (dateObj) => { // dateObj input: https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat('yyyy-LL-dd'); @@ -33,6 +44,10 @@ module.exports = function (eleventyConfig) { // Formatting tokens for Luxon: https://moment.github.io/luxon/#/formatting?id=table-of-tokens return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toISO(); }); + eleventyConfig.addFilter("min", (...numbers) => { + // Return the smallest number argument + return Math.min.apply(null, numbers); + }); eleventyConfig.addPassthroughCopy({ "./static/": "/" }); eleventyConfig.addPlugin(feedPlugin, { type: "atom",