chore: feed date improvements

This commit is contained in:
Cory Dransfeldt 2023-09-06 13:46:16 -07:00
parent 46db92b948
commit f47c889725
No known key found for this signature in database
7 changed files with 50 additions and 33 deletions

View file

@ -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, {

View file

@ -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)
},
}

26
config/feedFilters.js Normal file
View file

@ -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,
}
}
})
},
}

View file

@ -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) => {

View file

@ -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",

View file

@ -3,10 +3,10 @@
<feed xmlns="http://www.w3.org/2005/Atom">
{% assign entries = data | normalizeEntries %}
<title>{{ title }}</title>
<link href="{{ site.url }}{{ permalink }}" rel="self" />
<link href="{{ permalink | absoluteUrl: site.url }}" rel="self" />
<link href="{{ site.url }}" />
<link rel="hub" href="https://pubsubhubbub.superfeedr.com/" />
<updated>{{ updated | date: "%Y-%m-%dT%H:%M:%S-08:00" }}</updated>
<updated>{{ updated | stringToDate | dateToRfc822 }}</updated>
<id>{{ site.url }}</id>
<author>
<name>{{ site.name }}</name>
@ -20,6 +20,7 @@
<updated>{{ entry.date | date: "%m.%d.%Y" }}</updated>
<id>{{ entry.url | stripUtm | encodeAmp }}</id>
<content type="html">
{{ entry.excerpt | escape }}
</content>
</entry>
{%- endfor %}

View file

@ -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 %}
]