From 6168622c8741bfed2951e69dd9fe200d6fe3e747 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Sat, 5 Oct 2024 13:50:18 -0700 Subject: [PATCH] Update date handling and add some h-entry markup --- _includes/layouts/post.njk | 30 ++++++++++++++++--- _includes/style.css | 2 +- archive.njk | 6 ++-- eleventy.config.js | 60 ++++++++++++++++++++++++++++++++++++-- feed.njk | 7 +++-- index.njk | 7 ++++- package-lock.json | 30 +++++++++---------- package.json | 1 - posts/posts.11tydata.js | 8 +++++ posts/posts.json | 4 --- years.njk | 6 ++-- 11 files changed, 127 insertions(+), 34 deletions(-) create mode 100644 posts/posts.11tydata.js delete mode 100644 posts/posts.json diff --git a/_includes/layouts/post.njk b/_includes/layouts/post.njk index 50ab444..2d43379 100644 --- a/_includes/layouts/post.njk +++ b/_includes/layouts/post.njk @@ -1,11 +1,33 @@ --- layout: "layouts/layout.njk" -permalink: "/{{ page.date | date('YYYY') }}/{{ page.fileSlug }}/index.html" +permalink: "/{{ page.date | date('yyyy') }}/{{ page.fileSlug }}/index.html" --- -
+
-

{{ title }}

-Published {{ page.date | date('YYYY-MM-DD') }} +

{{ title }}

+{% if summary %}{% endif %} + +
+{% if published %} +Published +{%- if updated -%} +, Updated +{% endif %} +
+{# TODO: Add actual author information #} +
+ + + + +
+
{{ content | safe }} +
diff --git a/_includes/style.css b/_includes/style.css index f7087da..0aa207a 100644 --- a/_includes/style.css +++ b/_includes/style.css @@ -9,7 +9,7 @@ footer { margin-top: 1em; } /* Typography */ h1 { margin: 0; } -h1, h2, h3, h4, h5, h6 { margin-bottom: 0; } +h1, h2, h3, h4, h5, h6 { margin-bottom: 0; line-height: 1.2; } code { font-size: 1.2em; word-spacing: -0.2em; } p { /* leaving out because Safari doesn't do hyphens well */ diff --git a/archive.njk b/archive.njk index d1c758e..3d680b1 100644 --- a/archive.njk +++ b/archive.njk @@ -7,12 +7,14 @@ eleventyImport: {% for year, group in collections.posts | reverse | groupby("date.getFullYear()") %}
-

{{ year }}

+

{% for post in group %}
{{ post.data.title }}
- {{ post.date | date('MMM DD') }} + {% if post.data.published %} + + {% endif %}
{% endfor %}
diff --git a/eleventy.config.js b/eleventy.config.js index bf25e32..deb982d 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -1,12 +1,12 @@ import pluginRss from "@11ty/eleventy-plugin-rss"; import pluginSyntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight"; -import dateFilter from "nunjucks-date-filter"; import uslug from "uslug"; import anchor from "markdown-it-anchor"; import markdownIt from "markdown-it"; import CleanCSS from "clean-css"; import htmlMinifier from "html-minifier-terser"; import util from "util"; +import { DateTime } from "luxon"; const md = markdownIt({ html: true, @@ -19,7 +19,7 @@ const md = markdownIt({ }); export default async function (eleventyConfig) { - eleventyConfig.addNunjucksFilter('date', dateFilter); + eleventyConfig.addDateParsing(parseDate); // @TODO: Update to the new virtual template setup? eleventyConfig.addPlugin(pluginRss); eleventyConfig.addPlugin(pluginSyntaxHighlight); @@ -38,11 +38,14 @@ export default async function (eleventyConfig) { eleventyConfig.addPairedShortcode("aside", asideShortcode); eleventyConfig.addPairedShortcode("figure", figureShortcode); + eleventyConfig.addFilter("date", dateFilter); eleventyConfig.addFilter("markdown", value => md.renderInline(value)); eleventyConfig.addFilter("groupby", groupbyFilter); eleventyConfig.addFilter("cssmin", css => new CleanCSS({}).minify(css).styles); eleventyConfig.addFilter("debug", util.inspect); + eleventyConfig.addFilter("toRfc3339", toRfc3339Filter); + eleventyConfig.addFilter("hasTime", hasTimeFilter); eleventyConfig.setDataDeepMerge(true); @@ -57,6 +60,59 @@ export default async function (eleventyConfig) { }; }; +/** + * @param {{string | Date | DateTime}} anyDate + * @returns {DateTime} + */ +function dateTime(anyDate) { + if (anyDate instanceof Date) { + return DateTime.fromJSDate(anyDate); + } + if (DateTime.isDateTime(anyDate)) { + return anyDate; + } + let date = parseDate(anyDate); + if (date == null) { throw Error(`failed to parse date ${anyDate}`); } + return date; +} + +function dateFilter(date, format) { + return dateTime(date).toFormat(format); +} + +function toRfc3339Filter(date) { + return dateTime(date).toISO({ suppressMilliseconds: true }); +} + +function hasTimeFilter(date) { + date = dateTime(date); + return date.hour != 0 + || date.minute != 0 + || date.second != 0 + || date.millisecond != 0; +} + +/** + * @param {string} dateText + * @returns {{DateTime | null}} + */ +function parseDate(dateText) { + try { + if (!dateText) dateText = ""; + const formats = [ + "yyyy-MM-dd HH:mm:ss z", + "yyyy-MM-dd z", + "yyyy-MM-dd", + ]; + for (const format of formats) { + const date = DateTime.fromFormat(dateText, format, { setZone: true }); + if (date.isValid) { return date; } + } + } catch { + return null; + } +} + function access(item, path) { const segments = path.split("."); for (const seg of segments) { diff --git a/feed.njk b/feed.njk index ea01c55..5be0ce0 100644 --- a/feed.njk +++ b/feed.njk @@ -33,12 +33,15 @@ metadata: {{ metadata.author.name }} {{ metadata.author.email }} - {%- for post in collections.posts %} + {%- for post in collections.posts | reverse %} {% set absolutePostUrl %}{{ post.url | url | absoluteUrl(metadata.url) }}{% endset %} {{ post.data.title }} - {{ post.date | dateToRfc3339 }} + {{ post.data.published | toRfc3339 }} + {% if post.data.updated %} + {{ post.data.updated | toRfc3339 }} + {% endif %} {{ absolutePostUrl }} {{ post.templateContent | renderTransforms(post.data.page, metadata.base) }} diff --git a/index.njk b/index.njk index 785ef33..5c37acd 100644 --- a/index.njk +++ b/index.njk @@ -7,7 +7,12 @@ eleventyImport: {% for post in collections.posts | reverse %}
  • {{ post.data.title }}

    - {{ post.date | date("YYYY-MM-DD") }} + {% if post.data.published %} + + {% endif %} {% if post.data.summary %}

    {{ post.data.summary | markdown | safe }}

    {% endif %} diff --git a/package-lock.json b/package-lock.json index 0237bc3..1fa258b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -252,15 +252,6 @@ "js-yaml": "bin/js-yaml.js" } }, - "node_modules/@11ty/eleventy/node_modules/luxon": { - "version": "3.5.0", - "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.5.0.tgz", - "integrity": "sha512-rh+Zjr6DNfUYR3bPwJEnuwDdqMbxZW7LOQfUN4B54+Cl+0o5zaU9RJ6bcidfDtC1cWCZXQ+nvX8bf6bAji37QQ==", - "dev": true, - "engines": { - "node": ">=12" - } - }, "node_modules/@11ty/eleventy/node_modules/posthtml-match-helper": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/posthtml-match-helper/-/posthtml-match-helper-2.0.2.tgz", @@ -1831,6 +1822,15 @@ "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==", "dev": true }, + "node_modules/luxon": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.5.0.tgz", + "integrity": "sha512-rh+Zjr6DNfUYR3bPwJEnuwDdqMbxZW7LOQfUN4B54+Cl+0o5zaU9RJ6bcidfDtC1cWCZXQ+nvX8bf6bAji37QQ==", + "dev": true, + "engines": { + "node": ">=12" + } + }, "node_modules/markdown-it": { "version": "14.1.0", "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-14.1.0.tgz", @@ -3028,12 +3028,6 @@ "argparse": "^2.0.1" } }, - "luxon": { - "version": "3.5.0", - "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.5.0.tgz", - "integrity": "sha512-rh+Zjr6DNfUYR3bPwJEnuwDdqMbxZW7LOQfUN4B54+Cl+0o5zaU9RJ6bcidfDtC1cWCZXQ+nvX8bf6bAji37QQ==", - "dev": true - }, "posthtml-match-helper": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/posthtml-match-helper/-/posthtml-match-helper-2.0.2.tgz", @@ -4291,6 +4285,12 @@ "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==", "dev": true }, + "luxon": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.5.0.tgz", + "integrity": "sha512-rh+Zjr6DNfUYR3bPwJEnuwDdqMbxZW7LOQfUN4B54+Cl+0o5zaU9RJ6bcidfDtC1cWCZXQ+nvX8bf6bAji37QQ==", + "dev": true + }, "markdown-it": { "version": "14.1.0", "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-14.1.0.tgz", diff --git a/package.json b/package.json index 00eb0f3..8cf4f74 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "html-minifier-terser": "^7.2.0", "markdown-it": "^14.1.0", "markdown-it-anchor": "^5.2.5", - "nunjucks-date-filter": "^0.1.1", "prismjs": "github:PrismJS/prism#43efde2ee33310e5a6d8ddf432f0222734ec5928", "uslug": "^1.0.4" }, diff --git a/posts/posts.11tydata.js b/posts/posts.11tydata.js new file mode 100644 index 0000000..2d0a6e7 --- /dev/null +++ b/posts/posts.11tydata.js @@ -0,0 +1,8 @@ +export default { + layout: "layouts/post.njk", + tags: ["posts"], + eleventyComputed: { + published: (data) => data.date, + updated: (data) => data.updated, + } +}; diff --git a/posts/posts.json b/posts/posts.json deleted file mode 100644 index 4927712..0000000 --- a/posts/posts.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "layout": "layouts/post.njk", - "tags": ["posts"] -} diff --git a/years.njk b/years.njk index d576968..508d402 100644 --- a/years.njk +++ b/years.njk @@ -10,13 +10,15 @@ eleventyImport: collections: ["years"] --- -

    {{ year }}

    +

      {% for post in collections.years[year] | reverse %}
    • {{ post.data.title }}

      - {{ post.date | date("YYYY-MM-DD") }} + {% if post.data.published %} + + {% endif %} {% if post.data.summary %}

      {{ post.data.summary | markdown | safe }}

      {% endif %} -- 2.47.0