]> git.otsuka.systems Git - cotsuka.github.io/commitdiff
limit number of articles to show on index, add full articles archive page
authorCameron Otsuka <cameron@otsuka.haus>
Fri, 20 Dec 2024 03:53:49 +0000 (19:53 -0800)
committerCameron Otsuka <cameron@otsuka.haus>
Fri, 20 Dec 2024 03:53:49 +0000 (19:53 -0800)
_includes/components/articleslist.njk
_includes/layouts/home.njk
content/articles.njk [new file with mode: 0644]
eleventy.config.js

index b4c30feb30e3ca5dec8170ac193157f2c1517168..d038bb06a030fb5d7a87edf0cd95f17f40718102 100644 (file)
@@ -1,6 +1,11 @@
 <dl>
-{% 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 %}
     <dt><a href="{{ article.url }}">{{ article.data.title }}</a></dt>
     <dd>{{ article.data.description }}</dd>
 {%- endfor %}
index 517bb92db4572bc61299f948305eadd64b9b2663..7ae154e0e0244e5e902990ebe378a6aa622f96ec 100644 (file)
@@ -1,5 +1,6 @@
 ---
 layout: layouts/base.njk
+maxPosts: 5
 ---
 
 <section>
@@ -7,7 +8,7 @@ layout: layouts/base.njk
 </section>
 
 <section>
-    <h2>Articles</h2>
+    <h2><a href="/articles">Articles</a></h2>
     {%- include "components/articleslist.njk" %}
 </section>
 
diff --git a/content/articles.njk b/content/articles.njk
new file mode 100644 (file)
index 0000000..c35f501
--- /dev/null
@@ -0,0 +1,7 @@
+---
+layout: layouts/base.njk
+---
+
+<h1>Articles</h1>
+
+{% include "components/articleslist.njk" %}
index 85f1b67fb6035966babef95e640dc4617e98252d..a264a30ff77b1f258013f3e33a240666533437f9 100644 (file)
@@ -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",