Skip to content

Commit

Permalink
Merge pull request #599 from negrel/598-feed-plugin-image-support
Browse files Browse the repository at this point in the history
598 feed plugin image support
  • Loading branch information
oscarotero committed Apr 19, 2024
2 parents d0d91f3 + 9323a6d commit 70e0398
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/),
and this project try to adheres to [Semantic Versioning](https://semver.org/).
Go to the `v1` branch to see the changelog of Lume 1.

## [X.X.X] - TODO
### Added
- Feed plugin: Add image support.

## [2.1.4] - 2024-04-17
### Added
- Pagefind plugin: Updated the `ui` object with the new options introduced in v1.1.0.
Expand Down
13 changes: 13 additions & 0 deletions plugins/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export interface FeedItemOptions {

/** The item language */
lang?: string | ((data: Data) => string | undefined);

/** The item image */
image?: string | ((data: Data) => string | undefined);
}

export const defaults: Options = {
Expand Down Expand Up @@ -119,6 +122,7 @@ export interface FeedItem {
updated?: Date;
content: string;
lang: string;
image?: string;
}

const defaultGenerator = `Lume ${getCurrentVersion()}`;
Expand Down Expand Up @@ -154,6 +158,10 @@ export default function (userOptions?: Options) {
const content = getDataValue(data, items.content)?.toString();
const pageUrl = site.url(data.url, true);
const fixedContent = fixUrls(new URL(pageUrl), content || "");
const imagePath = getDataValue(data, items.image);
const image = imagePath !== undefined
? site.url(imagePath, true)
: undefined;

return {
title: getDataValue(data, items.title),
Expand All @@ -163,6 +171,7 @@ export default function (userOptions?: Options) {
updated: getDataValue(data, items.updated),
content: fixedContent,
lang: getDataValue(data, items.lang),
image,
};
}),
};
Expand Down Expand Up @@ -240,6 +249,9 @@ function generateRss(data: FeedData, file: string): string {
"content:encoded": item.content,
pubDate: item.published.toUTCString(),
"atom:updated": item.updated?.toISOString(),
meta: item.image
? { "@property": "og:image", "@content": item.image }
: undefined,
})
),
}),
Expand All @@ -264,6 +276,7 @@ function generateJson(data: FeedData, file: string): string {
content_html: item.content,
date_published: item.published.toUTCString(),
date_modified: item.updated?.toUTCString(),
image: item.image,
})
),
});
Expand Down
9 changes: 7 additions & 2 deletions tests/__snapshots__/feed.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ snapshot[`RSS plugin 1`] = `
"/404.md",
"/_data.yml",
"/favicon.png",
"/images",
"/images/avatar.jpg",
"/page5.yaml",
"/pages",
"/pages/2020-06-21_page2.page.json",
Expand Down Expand Up @@ -136,10 +138,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","description":"","items":[{"id":"https://example.com/pages/new-name/page7/","url":"https://example.com/pages/new-name/page7/","content_html":"Content of Page 7","date_published":"Sun, 02 Jan 2022 00:00:00 GMT","date_modified":"Sun, 02 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page6/","url":"https://example.com/pages/page6/","title":"PAGE 6","content_html":"<p>Content of Page 6</p>\\\\n","date_published":"Sat, 01 Jan 2022 00:00:00 GMT","date_modified":"Sat, 01 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page4/","url":"https://example.com/pages/page4/","title":"PAGE 4","content_html":"Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.page.ts","date_published":"Sat, 02 Jan 2021 18:32:00 GMT","date_modified":"Sat, 02 Jan 2021 18:32:00 GMT"},{"id":"https://example.com/overrided-page2/","url":"https://example.com/overrided-page2/","title":"PAGE 2","content_html":"Content of Page 2","date_published":"Sun, 21 Jun 2020 00:00:00 GMT","date_modified":"Sun, 21 Jun 2020 00:00:00 GMT"},{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"PAGE 5","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","description":"","items":[{"id":"https://example.com/pages/new-name/page7/","url":"https://example.com/pages/new-name/page7/","content_html":"Content of Page 7","date_published":"Sun, 02 Jan 2022 00:00:00 GMT","date_modified":"Sun, 02 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page6/","url":"https://example.com/pages/page6/","title":"PAGE 6","content_html":"<p>Content of Page 6</p>\\\\n","date_published":"Sat, 01 Jan 2022 00:00:00 GMT","date_modified":"Sat, 01 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page4/","url":"https://example.com/pages/page4/","title":"PAGE 4","content_html":"Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.page.ts","date_published":"Sat, 02 Jan 2021 18:32:00 GMT","date_modified":"Sat, 02 Jan 2021 18:32:00 GMT"},{"id":"https://example.com/overrided-page2/","url":"https://example.com/overrided-page2/","title":"PAGE 2","content_html":"Content of Page 2","date_published":"Sun, 21 Jun 2020 00:00:00 GMT","date_modified":"Sun, 21 Jun 2020 00:00:00 GMT","image":"https://example.com/images/avatar.jpg"},{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"PAGE 5","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","description":"","items":[{"id":"https://example.com/pages/new-name/page7/","url":"https://example.com/pages/new-name/page7/","content_html":"Content of Page 7","date_published":"Sun, 02 Jan 2022 00:00:00 GMT","date_modified":"Sun, 02 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page6/","url":"https://example.com/pages/page6/","title":"PAGE 6","content_html":"<p>Content of Page 6</p>\\\\n","date_published":"Sat, 01 Jan 2022 00:00:00 GMT","date_modified":"Sat, 01 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page4/","url":"https://example.com/pages/page4/","title":"PAGE 4","content_html":"Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.page.ts","date_published":"Sat, 02 Jan 2021 18:32:00 GMT","date_modified":"Sat, 02 Jan 2021 18:32:00 GMT"},{"id":"https://example.com/overrided-page2/","url":"https://example.com/overrided-page2/","title":"PAGE 2","content_html":"Content of Page 2","date_published":"Sun, 21 Jun 2020 00:00:00 GMT","date_modified":"Sun, 21 Jun 2020 00:00:00 GMT"},{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"PAGE 5","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","description":"","items":[{"id":"https://example.com/pages/new-name/page7/","url":"https://example.com/pages/new-name/page7/","content_html":"Content of Page 7","date_published":"Sun, 02 Jan 2022 00:00:00 GMT","date_modified":"Sun, 02 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page6/","url":"https://example.com/pages/page6/","title":"PAGE 6","content_html":"<p>Content of Page 6</p>\\\\n","date_published":"Sat, 01 Jan 2022 00:00:00 GMT","date_modified":"Sat, 01 Jan 2022 00:00:00 GMT"},{"id":"https://example.com/pages/page4/","url":"https://example.com/pages/page4/","title":"PAGE 4","content_html":"Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.page.ts","date_published":"Sat, 02 Jan 2021 18:32:00 GMT","date_modified":"Sat, 02 Jan 2021 18:32:00 GMT"},{"id":"https://example.com/overrided-page2/","url":"https://example.com/overrided-page2/","title":"PAGE 2","content_html":"Content of Page 2","date_published":"Sun, 21 Jun 2020 00:00:00 GMT","date_modified":"Sun, 21 Jun 2020 00:00:00 GMT","image":"https://example.com/images/avatar.jpg"},{"id":"https://example.com/page5/","url":"https://example.com/page5/","title":"PAGE 5","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",
Expand Down Expand Up @@ -197,6 +199,7 @@ snapshot[`RSS plugin 3`] = `
<content:encoded><![CDATA[Content of Page 2]]></content:encoded>
<pubDate>Sun, 21 Jun 2020 00:00:00 GMT</pubDate>
<atom:updated>2020-06-21T00:00:00.000Z</atom:updated>
<meta property="og:image" content="https://example.com/images/avatar.jpg"/>
</item>
<item>
<title>PAGE 5</title>
Expand Down Expand Up @@ -253,6 +256,7 @@ snapshot[`RSS plugin 3`] = `
<content:encoded><![CDATA[Content of Page 2]]></content:encoded>
<pubDate>Sun, 21 Jun 2020 00:00:00 GMT</pubDate>
<atom:updated>2020-06-21T00:00:00.000Z</atom:updated>
<meta property="og:image" content="https://example.com/images/avatar.jpg"/>
</item>
<item>
<title>PAGE 5</title>
Expand Down Expand Up @@ -374,6 +378,7 @@ Content of Page 2",
metas: [
"title",
"description",
"image",
],
names: "Array(2)",
page: [
Expand Down
3 changes: 3 additions & 0 deletions tests/__snapshots__/loaders.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ snapshot[`Load the pages of a site 1`] = `
"/404.md",
"/_data.yml",
"/favicon.png",
"/images",
"/images/avatar.jpg",
"/page5.yaml",
"/pages",
"/pages/2020-06-21_page2.page.json",
Expand Down Expand Up @@ -285,6 +287,7 @@ Content of Page 2",
metas: [
"title",
"description",
"image",
],
names: "Array(2)",
page: [
Expand Down
12 changes: 12 additions & 0 deletions tests/__snapshots__/robots.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ snapshot[`Robots plugin 1`] = `
"/404.md",
"/_data.yml",
"/favicon.png",
"/images",
"/images/avatar.jpg",
"/page5.yaml",
"/pages",
"/pages/2020-06-21_page2.page.json",
Expand Down Expand Up @@ -250,6 +252,7 @@ Content of Page 2",
metas: [
"title",
"description",
"image",
],
names: "Array(2)",
page: [
Expand Down Expand Up @@ -552,6 +555,8 @@ snapshot[`Robots plugin with allow 1`] = `
"/404.md",
"/_data.yml",
"/favicon.png",
"/images",
"/images/avatar.jpg",
"/page5.yaml",
"/pages",
"/pages/2020-06-21_page2.page.json",
Expand Down Expand Up @@ -698,6 +703,7 @@ Content of Page 2",
metas: [
"title",
"description",
"image",
],
names: "Array(2)",
page: [
Expand Down Expand Up @@ -1000,6 +1006,8 @@ snapshot[`Robots plugin with disallow 1`] = `
"/404.md",
"/_data.yml",
"/favicon.png",
"/images",
"/images/avatar.jpg",
"/page5.yaml",
"/pages",
"/pages/2020-06-21_page2.page.json",
Expand Down Expand Up @@ -1146,6 +1154,7 @@ Content of Page 2",
metas: [
"title",
"description",
"image",
],
names: "Array(2)",
page: [
Expand Down Expand Up @@ -1448,6 +1457,8 @@ snapshot[`Robots plugin with custom rules 1`] = `
"/404.md",
"/_data.yml",
"/favicon.png",
"/images",
"/images/avatar.jpg",
"/page5.yaml",
"/pages",
"/pages/2020-06-21_page2.page.json",
Expand Down Expand Up @@ -1596,6 +1607,7 @@ Content of Page 2",
metas: [
"title",
"description",
"image",
],
names: "Array(2)",
page: [
Expand Down
6 changes: 6 additions & 0 deletions tests/__snapshots__/sitemap.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ snapshot[`Sitemap plugin 1`] = `
"/404.md",
"/_data.yml",
"/favicon.png",
"/images",
"/images/avatar.jpg",
"/page5.yaml",
"/pages",
"/pages/2020-06-21_page2.page.json",
Expand Down Expand Up @@ -314,6 +316,7 @@ Content of Page 2",
metas: [
"title",
"description",
"image",
],
names: "Array(2)",
page: [
Expand Down Expand Up @@ -1604,6 +1607,8 @@ snapshot[`Sitemap plugin with filter_pages plugin 1`] = `
"/404.md",
"/_data.yml",
"/favicon.png",
"/images",
"/images/avatar.jpg",
"/page5.yaml",
"/pages",
"/pages/2020-06-21_page2.page.json",
Expand Down Expand Up @@ -1803,6 +1808,7 @@ Content of Page 2",
metas: [
"title",
"description",
"image",
],
names: "Array(2)",
page: [
Expand Down
Binary file added tests/assets/normal/images/avatar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion tests/assets/normal/pages/2020-06-21_page2.page.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"title": "Page 2",
"url": "/overrided-page2/",
"content": "Content of Page 2"
"content": "Content of Page 2",
"metas": {
"image": "/images/avatar.jpg"
}
}
1 change: 1 addition & 0 deletions tests/feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Deno.test("RSS plugin", async (t) => {
items: {
title: (data) => data.title?.toUpperCase(),
updated: "=date",
image: "=metas.image",
},
}),
);
Expand Down

0 comments on commit 70e0398

Please sign in to comment.