Skip to content

Commit

Permalink
improved feed plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Apr 28, 2023
1 parent 9c3d0bc commit 2d9312b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 44 deletions.
54 changes: 31 additions & 23 deletions plugins/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export const defaults: Options = {
generator: true,
},
items: {
title: "title",
description: "description",
date: "date",
content: "children",
lang: "lang",
title: "=title",
description: "=description",
date: "=date",
content: "=children",
lang: "=lang",
},
};

Expand Down Expand Up @@ -81,7 +81,7 @@ export default (userOptions?: DeepPartial<Options>) => {
return (site: Site) => {
const search = new Search(site, true);

site.addEventListener("afterRender", () => {
site.addEventListener("beforeSave", () => {
const output = Array.isArray(options.output)
? options.output
: [options.output];
Expand All @@ -91,30 +91,31 @@ export default (userOptions?: DeepPartial<Options>) => {
options.sort,
options.limit,
) as Data[];
const { info } = options;

const { info, items } = options;
const rootData = site.source.data.get("/") || {};

const feed: FeedData = {
title: info.title,
description: info.description,
date: info.date,
lang: info.lang,
title: getDataValue(rootData, info.title),
description: getDataValue(rootData, info.description),
date: getDataValue(rootData, info.date),
lang: getDataValue(rootData, info.lang),
url: site.url("", true),
generator: info.generator === true
? defaultGenerator
: info.generator || undefined,
items: pages.map((data): FeedItem => {
const content = getDataValue(data, items.content)?.toString();
const pageUrl = site.url(data.url as string, true);
const fixedContent = fixUrls(new URL(pageUrl), content || "");

return {
title: options.items.title &&
getDataValue(data, `=${options.items.title}`),
title: getDataValue(data, items.title),
url: site.url(data.url as string, true),
description: options.items.description &&
getDataValue(data, `=${options.items.description}`),
date: options.items.date &&
getDataValue(data, `=${options.items.date}`),
content: options.items.content &&
getDataValue(data, `=${options.items.content}`)?.toString(),
lang: options.items.lang &&
getDataValue(data, `=${options.items.lang}`),
description: getDataValue(data, items.description),
date: getDataValue(data, items.date),
content: fixedContent,
lang: getDataValue(data, items.lang),
};
}),
};
Expand Down Expand Up @@ -142,6 +143,13 @@ export default (userOptions?: DeepPartial<Options>) => {
};
};

function fixUrls(base: URL, html: string): string {
return html.replaceAll(
/\s(href|src)="([^"]+)"/g,
(_match, attr, value) => ` ${attr}="${new URL(value, base).href}"`,
);
}

function generateRss(data: FeedData, file: string): string {
const feed = {
[$XML]: { cdata: [["rss", "channel", "item", "content:encoded"]] },
Expand All @@ -166,7 +174,7 @@ function generateRss(data: FeedData, file: string): string {
"@type": "application/rss+xml",
},
description: data.description,
lastBuildDate: data.date.toISOString(),
lastBuildDate: data.date.toUTCString(),
language: data.lang,
generator: data.generator,
item: data.items.map((item) => ({
Expand All @@ -178,7 +186,7 @@ function generateRss(data: FeedData, file: string): string {
},
description: item.description,
"content:encoded": item.content,
pubDate: item.date.toISOString(),
pubDate: item.date.toUTCString(),
})),
},
},
Expand Down
32 changes: 21 additions & 11 deletions plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,33 @@ import type { Data } from "../core.ts";
/**
* Get the value of a page data
* For example, if the value is "=title", it will return the value of the page data "title"
* If the value is "$.title", it will return the value of the element with the selector ".title"
*/
export function getDataValue(data: Data, value?: unknown) {
// Get the value from the page data
if (typeof value === "string" && value.startsWith("=")) {
const key = value.slice(1);
if (typeof value === "string") {
if (value.startsWith("=")) {
const key = value.slice(1);

if (!key.includes(".")) {
return data[key];
if (!key.includes(".")) {
return data[key];
}

const keys = key.split(".");
let val = data;
for (const key of keys) {
val = val?.[key];
}
return val;
}

const keys = key.split(".");
let val = data;
for (const key of keys) {
val = val[key];
if (value.startsWith("$")) {
const document = data.page?.document;
const query = value.slice(1);
const element = document?.querySelector(query);
return element?.innerHTML;
}
return val;
} else {
return value;
}

return value;
}
20 changes: 10 additions & 10 deletions tests/__snapshots__/feed.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ snapshot[`RSS plugin 5`] = `
<link>https://example.com/</link>
<atom:link href="https://example.com/feed.rss" rel="self" type="application/rss+xml"/>
<description></description>
<lastBuildDate>2020-01-01T00:00:00.000Z</lastBuildDate>
<lastBuildDate>Wed, 01 Jan 2020 00:00:00 GMT</lastBuildDate>
<language>en</language>
<generator>https://lume.land</generator>
<item>
Expand All @@ -117,7 +117,7 @@ snapshot[`RSS plugin 5`] = `
<guid isPermaLink="false">https://example.com/pages/subpage/page7/</guid>
<description/>
<content:encoded><![CDATA[Content of Page 7]]></content:encoded>
<pubDate>2022-01-02T00:00:00.000Z</pubDate>
<pubDate>Sun, 02 Jan 2022 00:00:00 GMT</pubDate>
</item>
<item>
<title>Page 6</title>
Expand All @@ -128,23 +128,23 @@ snapshot[`RSS plugin 5`] = `
<![CDATA[<p>Content of Page 6</p>
]]>
</content:encoded>
<pubDate>2022-01-01T00:00:00.000Z</pubDate>
<pubDate>Sat, 01 Jan 2022 00:00:00 GMT</pubDate>
</item>
<item>
<title>Page 4</title>
<link>https://example.com/pages/page4/</link>
<guid isPermaLink="false">https://example.com/pages/page4/</guid>
<description/>
<content:encoded><![CDATA[Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.tmpl.ts]]></content:encoded>
<pubDate>2021-01-02T18:32:00.000Z</pubDate>
<pubDate>Sat, 02 Jan 2021 18:32:00 GMT</pubDate>
</item>
<item>
<title>Page 2</title>
<link>https://example.com/overrided-page2/</link>
<guid isPermaLink="false">https://example.com/overrided-page2/</guid>
<description/>
<content:encoded><![CDATA[Content of Page 2]]></content:encoded>
<pubDate>2020-06-21T00:00:00.000Z</pubDate>
<pubDate>Sun, 21 Jun 2020 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>',
Expand All @@ -157,7 +157,7 @@ snapshot[`RSS plugin 5`] = `
<link>https://example.com/</link>
<atom:link href="https://example.com/feed.rss" rel="self" type="application/rss+xml"/>
<description></description>
<lastBuildDate>2020-01-01T00:00:00.000Z</lastBuildDate>
<lastBuildDate>Wed, 01 Jan 2020 00:00:00 GMT</lastBuildDate>
<language>en</language>
<generator>https://lume.land</generator>
<item>
Expand All @@ -166,7 +166,7 @@ snapshot[`RSS plugin 5`] = `
<guid isPermaLink="false">https://example.com/pages/subpage/page7/</guid>
<description/>
<content:encoded><![CDATA[Content of Page 7]]></content:encoded>
<pubDate>2022-01-02T00:00:00.000Z</pubDate>
<pubDate>Sun, 02 Jan 2022 00:00:00 GMT</pubDate>
</item>
<item>
<title>Page 6</title>
Expand All @@ -177,23 +177,23 @@ snapshot[`RSS plugin 5`] = `
<![CDATA[<p>Content of Page 6</p>
]]>
</content:encoded>
<pubDate>2022-01-01T00:00:00.000Z</pubDate>
<pubDate>Sat, 01 Jan 2022 00:00:00 GMT</pubDate>
</item>
<item>
<title>Page 4</title>
<link>https://example.com/pages/page4/</link>
<guid isPermaLink="false">https://example.com/pages/page4/</guid>
<description/>
<content:encoded><![CDATA[Content of Page 4 in Overrided site name, from the file /pages/2021-01-02-18-32_page4.tmpl.ts]]></content:encoded>
<pubDate>2021-01-02T18:32:00.000Z</pubDate>
<pubDate>Sat, 02 Jan 2021 18:32:00 GMT</pubDate>
</item>
<item>
<title>Page 2</title>
<link>https://example.com/overrided-page2/</link>
<guid isPermaLink="false">https://example.com/overrided-page2/</guid>
<description/>
<content:encoded><![CDATA[Content of Page 2]]></content:encoded>
<pubDate>2020-06-21T00:00:00.000Z</pubDate>
<pubDate>Sun, 21 Jun 2020 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>',
Expand Down

0 comments on commit 2d9312b

Please sign in to comment.