From ce9b571ba699616ff4c5dcda9e72d68e23fbef02 Mon Sep 17 00:00:00 2001 From: Cameron Otsuka Date: Fri, 14 Feb 2025 20:51:52 -0800 Subject: [PATCH] reduce duplication, make config more readable --- _data/metadata.json | 17 +++++ _includes/components/posse.njk | 6 ++ _includes/layouts/article.njk | 7 +- _includes/layouts/link.njk | 11 +-- _includes/layouts/review.njk | 11 +-- eleventy.config.cjs | 122 +++++++++------------------------ 6 files changed, 64 insertions(+), 110 deletions(-) create mode 100644 _includes/components/posse.njk diff --git a/_data/metadata.json b/_data/metadata.json index 45ff9e5..c231291 100644 --- a/_data/metadata.json +++ b/_data/metadata.json @@ -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 index 0000000..94e5005 --- /dev/null +++ b/_includes/components/posse.njk @@ -0,0 +1,6 @@ +
  • + Also Posted To: + {%- for posse_location, posse_url in posse %} + {{ posse_location }} + {%- endfor %} +
  • \ No newline at end of file diff --git a/_includes/layouts/article.njk b/_includes/layouts/article.njk index 13d8dc7..9c0c02f 100644 --- a/_includes/layouts/article.njk +++ b/_includes/layouts/article.njk @@ -11,12 +11,7 @@ layout: layouts/base.njk
  • Description: {{ description }}
  • Tags: {{ tags | reject("equalto", "articles") | sort | join(", ") }}
  • {%- if posse %} -
  • - Also Posted To: - {%- for posse_location, posse_url in posse %} - {{ posse_location }} - {%- endfor %} -
  • + {%- include "components/posse.njk" %} {%- endif %} diff --git a/_includes/layouts/link.njk b/_includes/layouts/link.njk index a76c87c..388c0ae 100644 --- a/_includes/layouts/link.njk +++ b/_includes/layouts/link.njk @@ -10,14 +10,9 @@ layout: layouts/base.njk
  • Published:
  • Description: {{ description }}
  • Tags: {{ tags | reject("equalto", "links") | sort | join(", ") }}
  • - {% if posse %} -
  • - Also Posted To: - {% for posse_location, posse_url in posse %} - {{ posse_location }} - {% endfor %} -
  • - {% endif %} + {%- if posse %} + {%- include "components/posse.njk" %} + {%- endif %} diff --git a/_includes/layouts/review.njk b/_includes/layouts/review.njk index 85cb4db..8dac2e6 100644 --- a/_includes/layouts/review.njk +++ b/_includes/layouts/review.njk @@ -10,14 +10,9 @@ layout: layouts/base.njk
  • Published:
  • Rating: {{ description }}
  • Tags: {{ tags | reject("equalto", "articles") | sort | join(", ") }}
  • - {% if posse %} -
  • - Also Posted To: - {% for posse_location, posse_url in posse %} - {{ posse_location }} - {% endfor %} -
  • - {% endif %} + {%- if posse %} + {%- include "components/posse.njk" %} + {%- endif %} diff --git a/eleventy.config.cjs b/eleventy.config.cjs index ff98001..464ba47 100644 --- a/eleventy.config.cjs +++ b/eleventy.config.cjs @@ -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 - ? `
    ${customLabel || label}
    ` + const labelHtml = label + ? `
    ${label}
    ` : ""; - let contentHtml = + const contentHtml = (content || "").trim().length > 0 ? `
    ${content}
    ` : ""; - - return `
    ${labelHtml}${contentHtml}
    `; } ); @@ -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", }; -- 2.51.0