diff --git a/.eleventy.js b/.eleventy.js index a09f7569..9827c096 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -4,6 +4,7 @@ const pluginUnfurl = require('eleventy-plugin-unfurl') const pluginFilesMinifier = require('@sherby/eleventy-plugin-files-minifier') const schema = require('@quasibit/eleventy-plugin-schema') const { eleventyImagePlugin } = require('@11ty/eleventy-img') +const pluginRss = require('@11ty/eleventy-plugin-rss') const Image = require('@11ty/eleventy-img') const embedYouTube = require('eleventy-plugin-youtube-embed') const markdownIt = require('markdown-it') @@ -12,6 +13,7 @@ const markdownItFootnote = require('markdown-it-footnote') const filters = require('./config/filters.js') const dateFilters = require('./config/dateFilters.js') const mediaFilters = require('./config/mediaFilters.js') +const feedFilters = require('./config/feedFilters.js') const CleanCSS = require('clean-css') const now = String(Date.now()) const { execSync } = require('child_process') @@ -108,9 +110,18 @@ module.exports = function (eleventyConfig) { eleventyConfig.addLiquidFilter(filterName, mediaFilters[filterName]) }) + // feed filters + Object.keys(feedFilters).forEach((filterName) => { + eleventyConfig.addLiquidFilter(filterName, feedFilters[filterName]) + }) + // css filters eleventyConfig.addFilter('cssmin', (code) => new CleanCSS({}).minify(code).styles) + // rss filters + eleventyConfig.addLiquidFilter('dateToRfc822', pluginRss.dateToRfc822) + eleventyConfig.addLiquidFilter('absoluteUrl', pluginRss.absoluteUrl) + // image shortcode eleventyConfig.addShortcode('image', async function (src, alt, css, sizes, loading) { let metadata = await Image(src, { diff --git a/config/dateFilters.js b/config/dateFilters.js index 172a628f..ba4c44d0 100644 --- a/config/dateFilters.js +++ b/config/dateFilters.js @@ -4,9 +4,6 @@ module.exports = { readableDate: (date) => { return DateTime.fromISO(date).toFormat('LLLL d, yyyy') }, - dateForFeed: (date) => { - return new Date(date).toISOString() - }, toDateTime: (date) => { const formatted = DateTime.fromISO(date) @@ -40,8 +37,8 @@ module.exports = { return [month, day, year].join('.') }, - rssLastUpdatedDate: (collection) => { - if (!collection || !collection.length) return '' - return collection[0]['publishedAt'] + stringToDate: (string) => { + if (!string) return + return new Date(string) }, } diff --git a/config/feedFilters.js b/config/feedFilters.js new file mode 100644 index 00000000..662ad076 --- /dev/null +++ b/config/feedFilters.js @@ -0,0 +1,26 @@ +const { URL } = require('url') +const BASE_URL = 'https://coryd.dev' + +module.exports = { + normalizeEntries: (entries) => { + return entries.map((entry) => { + const dateKey = Object.keys(entry).find((key) => key.includes('date')) + const date = new Date(entry[dateKey]) + let excerpt = '' + + // set the entry excerpt + if (entry.data?.post_excerpt) excerpt = entry.data.post_excerpt + if (entry.description) excerpt = entry.description + + // if there's a valid entry return a normalized object + if (entry) { + return { + title: entry.data?.title || entry.title, + url: entry.url.includes('http') ? entry.url : new URL(entry.url, BASE_URL).toString(), + date, + excerpt, + } + } + }) + }, +} diff --git a/config/filters.js b/config/filters.js index 3961a930..ed558cb1 100644 --- a/config/filters.js +++ b/config/filters.js @@ -1,6 +1,8 @@ const marked = require('marked') const sanitizeHTML = require('sanitize-html') + const utmPattern = /[?&](utm_[^&=]+=[^&#]*)/gi +const BASE_URL = 'https://coryd.dev' module.exports = { trim: (string, limit) => { @@ -23,30 +25,9 @@ module.exports = { if (!string) return return string.replace(utmPattern, '') }, - normalizeEntries: (entries) => { - return entries.map((entry) => { - const dateKey = Object.keys(entry).find((key) => key.includes('date')) - const date = entry[dateKey] - let excerpt = '' - - // set the entry excerpt - if (entry.data?.post_excerpt) excerpt = entry.data.post_excerpt - if (entry.description) excerpt = entry.description - - // if there's a valid entry return a normalized object - if (entry) { - return { - title: entry.data?.title || entry.title, - url: entry.url.includes('http') ? entry.url : `https://coryd.dev${entry.url}`, - date, - excerpt, - } - } - }) - }, getPostImage: (image) => { if (image && image !== '') return image - return 'https://coryd.dev/assets/img/social-card.jpg' + return `${BASE_URL}/assets/img/social-card.jpg` }, getPopularPosts: (posts, analytics) => { return posts @@ -74,7 +55,7 @@ module.exports = { const filtered = webmentions - .filter((entry) => entry['wm-target'].replace(utmPattern, '') === `https://coryd.dev${url}`) + .filter((entry) => entry['wm-target'].replace(utmPattern, '') === `${BASE_URL}${url}`) .filter((entry) => allowedTypes.includes(entry['wm-property'])) || [] filtered.forEach((m) => { diff --git a/package.json b/package.json index 99d2ba3d..a77ec726 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@11ty/eleventy-activity-feed": "^1.0.9", "@11ty/eleventy-fetch": "^4.0.0", "@11ty/eleventy-img": "^3.1.0", + "@11ty/eleventy-plugin-rss": "^1.2.0", "@11ty/eleventy-plugin-syntaxhighlight": "^5.0.0", "@catppuccin/tailwindcss": "^0.1.6", "@commitlint/cli": "^17.7.1", diff --git a/src/_includes/partials/feeds/content.liquid b/src/_includes/partials/feeds/content.liquid index fd53660d..2419137f 100644 --- a/src/_includes/partials/feeds/content.liquid +++ b/src/_includes/partials/feeds/content.liquid @@ -3,10 +3,10 @@ {% assign entries = data | normalizeEntries %} {{ title }} - + - {{ updated | date: "%Y-%m-%dT%H:%M:%S-08:00" }} + {{ updated | stringToDate | dateToRfc822 }} {{ site.url }} {{ site.name }} @@ -20,6 +20,7 @@ {{ entry.date | date: "%m.%d.%Y" }} {{ entry.url | stripUtm | encodeAmp }} + {{ entry.excerpt | escape }} {%- endfor %} diff --git a/src/_includes/partials/feeds/json.liquid b/src/_includes/partials/feeds/json.liquid index 8a95f245..59bb1792 100644 --- a/src/_includes/partials/feeds/json.liquid +++ b/src/_includes/partials/feeds/json.liquid @@ -1,4 +1,4 @@ -{% assign entries = data | normalizeEntries %} +{%- assign entries = data | normalizeEntries -%} { "version": "https://jsonfeed.org/version/1", "title": "{{ title }}", @@ -11,7 +11,7 @@ "title": "{{ entry.title | escape }}", "url": "{{ entry.url }}", "content_text": "{{ entry.title }} {{ entry.url }}", - "date_published": "{{ entry.date | date: "%Y-%m-%dT%H:%M:%S-08:00" }}" + "date_published": "{{ entry.date | stringToDate | dateToRfc822 }}" }{% if not forloop.last %},{% endif %} {%- endfor %} ]