From b58cc0ae247edad7c16d479835174199eb8fc811 Mon Sep 17 00:00:00 2001 From: castaneai Date: Wed, 22 Jan 2025 22:51:43 +0900 Subject: [PATCH] Feed plugin: Add options `info.image`, `info.icon` and `info.color` Add an option to specify images and colors for feed output. This can be used as extended metadata for Feedly, for example. refs: * https://devhd.wordpress.com/2015/07/31/10-ways-to-optimize-your-feed-for-feedly/ * https://justingarrison.com/blog/2022-11-22-hugo-rss-improvements/ * https://github.com/withastro/astro/tree/main/packages/astro-rss#customdata --- CHANGELOG.md | 4 ++++ plugins/feed.ts | 23 +++++++++++++++++++++++ tests/__snapshots__/feed.test.ts.snap | 18 ++++++++++++++---- tests/feed.test.ts | 3 +++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3789cb2..32da9b78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ Go to the `v1` branch to see the changelog of Lume 1. - New `--hostname` argument to `lume --serve` and `lume cms` to change the default `localhost` value to something else. - New `--open, -o` argument to `lume cms` to open automatically in the browser. +- Feed plugin + - New option `info.image` + - New option `info.icon` + - New option `info.color` ### Changed - `inline`: Append classes to existing ones. [#722] diff --git a/plugins/feed.ts b/plugins/feed.ts index f8ba35cf..0948e2ae 100644 --- a/plugins/feed.ts +++ b/plugins/feed.ts @@ -56,6 +56,15 @@ export interface FeedInfoOptions { /** The feed author URL */ authorUrl?: string; + + /** The main image of the site */ + image?: string; + + /** The logotype or icon of the site */ + icon?: string; + + /** The color theme of the site */ + color?: string; } export interface FeedItemOptions { @@ -131,6 +140,9 @@ export interface FeedData { generator?: string; items: FeedItem[]; author?: Author; + image?: string; + icon?: string; + color?: string; } export interface FeedItem { @@ -179,6 +191,9 @@ export function feed(userOptions?: Options) { ? defaultGenerator : info.generator || undefined, author: getAuthor(rootData, info), + image: info.image, + icon: info.icon, + color: info.color, items: pages.map((data): FeedItem => { const content = getDataValue(data, items.content)?.toString(); const pageUrl = site.url(data.url, true); @@ -260,6 +275,7 @@ function generateRss(data: FeedData, file: string): string { "@xmlns:atom": "http://www.w3.org/2005/Atom", "@xmlns:sy": "http://purl.org/rss/1.0/modules/syndication/", "@xmlns:slash": "http://purl.org/rss/1.0/modules/slash/", + "@xmlns:webfeeds": "http://webfeeds.org/rss/1.0", "@version": "2.0", channel: { title: data.title, @@ -277,6 +293,11 @@ function generateRss(data: FeedData, file: string): string { name: data.author?.name, uri: data.author?.url, }, + "webfeeds:cover": { + "image": data.image, + }, + "webfeeds:logo": data.icon, + "webfeeds:accentColor": data.color, item: data.items.map((item) => ({ title: item.title, link: item.url, @@ -311,6 +332,8 @@ function generateJson(data: FeedData, file: string): string { feed_url: file, description: data.description, author: data.author, + icon: data.image, + favicon: data.icon, items: data.items.map((item) => ({ id: item.url, url: item.url, diff --git a/tests/__snapshots__/feed.test.ts.snap b/tests/__snapshots__/feed.test.ts.snap index e2bc107a..f0d9c35c 100644 --- a/tests/__snapshots__/feed.test.ts.snap +++ b/tests/__snapshots__/feed.test.ts.snap @@ -117,10 +117,10 @@ snapshot[`RSS plugin 2`] = `[]`; snapshot[`RSS plugin 3`] = ` [ { - content: '{"version":"https://jsonfeed.org/version/1","title":"My RSS Feed","home_page_url":"https://example.com/","feed_url":"https://example.com/feed.json","author":{"name":"Laura Rubio"},"items":[{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"PAGE 5","author":{"name":"Óscar","url":"https://oscarotero.com"},"content_html":"Content of Page 5","date_published":"Thu, 21 Jun 1979 23:45:00 GMT","date_modified":"Thu, 21 Jun 1979 23:45:00 GMT"}]}', + content: '{"version":"https://jsonfeed.org/version/1","title":"My RSS Feed","home_page_url":"https://example.com/","feed_url":"https://example.com/feed.json","author":{"name":"Laura Rubio"},"icon":"https://example.com/image.png","favicon":"https://example.com/icon.svg","items":[{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"PAGE 5","author":{"name":"Óscar","url":"https://oscarotero.com"},"content_html":"Content of Page 5","date_published":"Thu, 21 Jun 1979 23:45:00 GMT","date_modified":"Thu, 21 Jun 1979 23:45:00 GMT"}]}', data: { basename: "feed", - content: '{"version":"https://jsonfeed.org/version/1","title":"My RSS Feed","home_page_url":"https://example.com/","feed_url":"https://example.com/feed.json","author":{"name":"Laura Rubio"},"items":[{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"PAGE 5","author":{"name":"Óscar","url":"https://oscarotero.com"},"content_html":"Content of Page 5","date_published":"Thu, 21 Jun 1979 23:45:00 GMT","date_modified":"Thu, 21 Jun 1979 23:45:00 GMT"}]}', + content: '{"version":"https://jsonfeed.org/version/1","title":"My RSS Feed","home_page_url":"https://example.com/","feed_url":"https://example.com/feed.json","author":{"name":"Laura Rubio"},"icon":"https://example.com/image.png","favicon":"https://example.com/icon.svg","items":[{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"PAGE 5","author":{"name":"Óscar","url":"https://oscarotero.com"},"content_html":"Content of Page 5","date_published":"Thu, 21 Jun 1979 23:45:00 GMT","date_modified":"Thu, 21 Jun 1979 23:45:00 GMT"}]}', page: [ "src", "data", @@ -137,7 +137,7 @@ snapshot[`RSS plugin 3`] = ` }, { content: ' - + My RSS Feed https://example.com/ @@ -148,6 +148,11 @@ snapshot[`RSS plugin 3`] = ` Laura Rubio + + https://example.com/image.png + + https://example.com/icon.svg + #ff0000 PAGE 5 https://example.com/page5/ @@ -165,7 +170,7 @@ snapshot[`RSS plugin 3`] = ` data: { basename: "feed", content: ' - + My RSS Feed https://example.com/ @@ -176,6 +181,11 @@ snapshot[`RSS plugin 3`] = ` Laura Rubio + + https://example.com/image.png + + https://example.com/icon.svg + #ff0000 PAGE 5 https://example.com/page5/ diff --git a/tests/feed.test.ts b/tests/feed.test.ts index 0d2a1ae5..0b2b2018 100644 --- a/tests/feed.test.ts +++ b/tests/feed.test.ts @@ -14,6 +14,9 @@ Deno.test("RSS plugin", async (t) => { published: new Date("2020-01-01"), generator: "https://lume.land", authorName: "Laura Rubio", + icon: "https://example.com/icon.svg", + image: "https://example.com/image.png", + color: "#ff0000", }, items: { title: (data) => data.title?.toUpperCase(),