]> git.otsuka.systems Git - cotsuka.github.io/commitdiff
reduce duplication, make config more readable
authorCameron Otsuka <cameron@otsuka.haus>
Sat, 15 Feb 2025 04:51:52 +0000 (20:51 -0800)
committerCameron Otsuka <cameron@otsuka.haus>
Sat, 15 Feb 2025 04:51:52 +0000 (20:51 -0800)
_data/metadata.json
_includes/components/posse.njk [new file with mode: 0644]
_includes/layouts/article.njk
_includes/layouts/link.njk
_includes/layouts/review.njk
eleventy.config.cjs

index 45ff9e551ef80cd83e11247715fdcd96c04c284e..c231291bace9413562cc31a654f92a6ce8b7b075 100644 (file)
@@ -7,5 +7,22 @@
         "name": "Cameron Otsuka",
         "email": "cameron@otsuka.haus",
         "url": "https://otsuka.haus"
+    },
+    "categories": {
+        "articles": [],
+        "links": [
+            "livestream",
+            "podcast"
+        ],
+        "reviews": [
+            "movie",
+            "music",
+            "show"
+        ]
+    },
+    "calloutLabels": {
+        "info": "ⓘ Info",
+        "warn": "⚠ Warning",
+        "error": "! Error"
     }
 }
\ No newline at end of file
diff --git a/_includes/components/posse.njk b/_includes/components/posse.njk
new file mode 100644 (file)
index 0000000..94e5005
--- /dev/null
@@ -0,0 +1,6 @@
+<li>
+    Also Posted To: 
+    {%- for posse_location, posse_url in posse %}
+        <a href="{{ posse_url }}">{{ posse_location }}</a>
+    {%- endfor %}
+</li>
\ No newline at end of file
index 13d8dc7d6e9fc94047c0b22d6e27a543436ae53f..9c0c02f5aa94d3e0d84b507d28b5588136935928 100644 (file)
@@ -11,12 +11,7 @@ layout: layouts/base.njk
                <li>Description: {{ description }}</li>
                <li>Tags: {{ tags | reject("equalto", "articles") | sort | join(", ") }}</li>
                {%- if posse %}
-                       <li>
-                               Also Posted To: 
-                               {%- for posse_location, posse_url in posse %}
-                                       <a href="{{ posse_url }}">{{ posse_location }}</a>
-                               {%- endfor %}
-                       </li>
+                       {%- include "components/posse.njk" %}
                {%- endif %}
        </ul>
 </details>
index a76c87c55832f45f975228996437297a6a7de1cc..388c0aecbf73858d93530486e8e31f72a339d88c 100644 (file)
@@ -10,14 +10,9 @@ layout: layouts/base.njk
                <li>Published: <time datetime="{{ page.date | htmlDateString }}">{{ page.date | htmlDateString }}</time></li>
                <li>Description: {{ description }}</li>
                <li>Tags: {{ tags | reject("equalto", "links") | sort | join(", ") }}</li>
-               {% if posse %}
-                       <li>
-                               Also Posted To: 
-                               {% for posse_location, posse_url in posse %}
-                                       <a href="{{ posse_url }}">{{ posse_location }}</a>
-                               {% endfor %}
-                       </li>
-               {% endif %}
+               {%- if posse %}
+                       {%- include "components/posse.njk" %}
+               {%- endif %}
        </ul>
 </details>
 
index 85cb4dbd7ec15dca5f4c5410023a626975eac34b..8dac2e6ba01e69e645f3c7d3ec84014967ee47d4 100644 (file)
@@ -10,14 +10,9 @@ layout: layouts/base.njk
                <li>Published: <time datetime="{{ page.date | htmlDateString }}">{{ page.date | htmlDateString }}</time></li>
                <li>Rating: {{ description }}</li>
                <li>Tags: {{ tags | reject("equalto", "articles") | sort | join(", ") }}</li>
-               {% if posse %}
-                       <li>
-                               Also Posted To: 
-                               {% for posse_location, posse_url in posse %}
-                                       <a href="{{ posse_url }}">{{ posse_location }}</a>
-                               {% endfor %}
-                       </li>
-               {% endif %}
+               {%- if posse %}
+                       {%- include "components/posse.njk" %}
+               {%- endif %}
        </ul>
 </details>
 
index ff98001bc10887d55902b4addcf4f7db414b1cca..464ba47a99b720e46beffd825854f5f7d941d877 100644 (file)
@@ -8,16 +8,16 @@ const markdownIt = require("markdown-it");
 const markdownItFootnote = require('markdown-it-footnote');
 const path = require("path");
 
-let markdownOptions = {
+const markdownOptions = {
        html: true,
        breaks: false,
        linkify: true,
        typographer: true,
 };
-let mdLibrary = markdownIt(markdownOptions).use(markdownItFootnote);
+const mdLibrary = markdownIt(markdownOptions).use(markdownItFootnote);
 
 function relativeToInputPath(inputPath, relativeFilePath) {
-       let split = inputPath.split("/");
+       const split = inputPath.split("/");
        split.pop();
        return path.resolve(split.join(path.sep), relativeFilePath);
 }
@@ -31,18 +31,11 @@ function isFullUrl(url) {
        }
 }
 
-module.exports = function (eleventyConfig) {
+module.exports = async function (eleventyConfig) {
        eleventyConfig.addCollection("activities", function(collectionApi) {
-               let collectionSubset = [
-                       ...collectionApi.getFilteredByTag("articles"),
-                       ...collectionApi.getFilteredByTag("links"),
-                       ...collectionApi.getFilteredByTag("reviews"),
-               ];
-               let sortedSubset = collectionSubset.sort(function(a, b) {
-                       // maintain sort order when working with default collections objects
-                       return a.date - b.date;
-               });
-               return sortedSubset;
+               return collectionApi.getFilteredByTag(
+                       ...Object.keys(globalMetadata['categories'])
+               ).sort((a, b) => a.date - b.date)
        });
        eleventyConfig.addFilter("cssmin", function (code) {
                return new cleanCSS({}).minify(code).styles;
@@ -59,46 +52,33 @@ module.exports = function (eleventyConfig) {
                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');
        });
        eleventyConfig.addFilter("isoDate", (dateObj) => {
-               // 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.addPairedShortcode(
                "callout",
                function (content, level = "", format = "md", customLabel = "") {
+                       const label = customLabel || globalMetadata['calloutLabels'][level]
                        if (format === "md") {
                                content = mdLibrary.renderInline(content);
                        } else if (format === "md-block") {
                                content = mdLibrary.render(content);
-                       } else if (format === "html") {
-                               content = content
-                       }
-                       if (customLabel) {
-                               label = customLabel;
-                       } else if (level === "info") {
-                               label = "ⓘ Info"
-                       } else if (level === "warn") {
-                               label = "⚠ Warning";
-                       } else if (level === "error") {
-                               label = "! Error"
                        }
-                       let labelHtml = label
-                               ? `<div class="callout-label">${customLabel || label}</div>`
+                       const labelHtml = label
+                               ? `<div class="callout-label">${label}</div>`
                                : "";
-                       let contentHtml =
+                       const contentHtml =
                                (content || "").trim().length > 0
                                        ? `<div class="callout-content">${content}</div>`
                                        : "";
-
-                       return `<div class="callout${
-                               level ? ` callout-${level}` : ""
+                       return `<div class="callout${level
+                               ? ` callout-${level}`
+                               : ""
                        }">${labelHtml}${contentHtml}</div>`;
                }
        );
@@ -112,54 +92,23 @@ module.exports = function (eleventyConfig) {
                },
                metadata: globalMetadata,
        });
-       eleventyConfig.addPlugin(feedPlugin, {
-               type: "atom",
-               outputPath: "/feeds/articles.xml",
-               inputPath: "eleventy-plugin-feed-cameron-otsuka-articles-atom.njk",
-               collection: {
-                       name: "articles",
-                       limit: 10,
-               },
-               metadata: {
-                       language: globalMetadata['language'],
-                       title: globalMetadata['title'].concat(" - Articles"),
-                       subtitle: globalMetadata['subtitle'],
-                       base: globalMetadata['base'],
-                       author: globalMetadata['author'],
-               },
-       });
-       eleventyConfig.addPlugin(feedPlugin, {
-               type: "atom",
-               outputPath: "/feeds/links.xml",
-               inputPath: "eleventy-plugin-feed-cameron-otsuka-links-atom.njk",
-               collection: {
-                       name: "links",
-                       limit: 10,
-               },
-               metadata: {
-                       language: globalMetadata['language'],
-                       title: globalMetadata['title'].concat(" - Links"),
-                       subtitle: globalMetadata['subtitle'],
-                       base: globalMetadata['base'],
-                       author: globalMetadata['author'],
-               },
-       });
-       eleventyConfig.addPlugin(feedPlugin, {
-               type: "atom",
-               outputPath: "/feeds/reviews.xml",
-               inputPath: "eleventy-plugin-feed-cameron-otsuka-reviews-atom.njk",
-               collection: {
-                       name: "reviews",
-                       limit: 10,
-               },
-               metadata: {
-                       language: globalMetadata['language'],
-                       title: globalMetadata['title'].concat(" - Reviews"),
-                       subtitle: globalMetadata['subtitle'],
-                       base: globalMetadata['base'],
-                       author: globalMetadata['author'],
-               },
-       });
+       for (category in globalMetadata['categories']) {
+               eleventyConfig.addPlugin(feedPlugin, {
+                       type: "atom",
+                       outputPath: `/feeds/${category}.xml`,
+                       inputPath: `eleventy-plugin-feed-cameron-otsuka-${category}-atom.njk`,
+                       collection: {
+                               name: `${category}`,
+                               limit: 10,
+                       },
+                       metadata: {
+                               ...globalMetadata,
+                               title: globalMetadata['title'].concat(
+                                       ` - ${category.charAt(0).toUpperCase()}`
+                               ),
+                       },
+               });
+       }
        eleventyConfig.addPlugin(eleventySyntaxHighlightPlugin);
        eleventyConfig.addShortcode("image", async function (src, alt) {
                let input;
@@ -168,8 +117,7 @@ module.exports = function (eleventyConfig) {
                } else {
                        input = relativeToInputPath(this.page.inputPath, src);
                }
-
-               let metadata = await eleventyImagePlugin(input, {
+               const metadata = await eleventyImagePlugin(input, {
                        widths: [360, 720],
                        formats: ["svg", "avif", "jpeg", "gif"],
                        sharpOptions: {
@@ -179,16 +127,14 @@ module.exports = function (eleventyConfig) {
                        urlPath: "/img/",
                        outputDir: path.join(eleventyConfig.dir.output, "img"),
                });
-
-               let imageAttributes = {
+               const imageAttributes = {
                        alt,
                        sizes: "360w, 720w",
                        loading: "lazy",
                        decoding: "async",
                        "eleventy:ignore": "",
                };
-
-               let options = {
+               const options = {
                        pictureAttributes: {},
                        whitespaceMode: "inline",
                };