Skip to content

Commit

Permalink
fix(rss): Encode titles and descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Mar 17, 2024
1 parent c0cca0e commit a75b8c9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 45 deletions.
70 changes: 30 additions & 40 deletions src/middleware.ts
Expand Up @@ -13,51 +13,41 @@ export const onRequest = defineMiddleware(async (ctx, next) => {
const output = await transform(html, [
async (node) => {
await walk(node, (node) => {
// Simplify picture elements to img elements, some feeds are struggling with it
if (node.type === ELEMENT_NODE && node.name === "picture") {
if (node.parent.type === ELEMENT_NODE && node.parent.name === "a") {
const imgChildren = node.children.find(
(child) => child.type === ELEMENT_NODE && child.name === "img",
);
if (node.type === ELEMENT_NODE) {
// Simplify picture elements to img elements, some feeds are struggling with it
if (node.name === "picture") {
if (node.parent.type === ELEMENT_NODE && node.parent.name === "a") {
const imgChildren = node.children.find(
(child) => child.type === ELEMENT_NODE && child.name === "img",
);

node.name = "img";
node.attributes = imgChildren?.attributes;
node.attributes.src = getBaseSiteURL().slice(0, -1) + node.attributes.src;
// biome-ignore lint/performance/noDelete: <explanation>
delete node.attributes.srcset;
// biome-ignore lint/performance/noDelete: <explanation>
delete node.attributes.sizes;
// biome-ignore lint/performance/noDelete: <explanation>
delete node.attributes.onload;
// biome-ignore lint/performance/noDelete: <explanation>
delete node.attributes.style;
const { src, srcset, sizes, onload, style, ...attributes } =
imgChildren?.attributes || {};

node.name = "img";
node.attributes = attributes;
node.attributes.src = getBaseSiteURL().slice(0, -1) + node.attributes.src;
}
}
}

// Make sure links are absolute, some readers are not smart enough to figure it out
if (
node.type === ELEMENT_NODE &&
node.name === "a" &&
node.attributes.src?.startsWith("/")
) {
node.attributes.src = getBaseSiteURL().slice(0, -1) + node.attributes.src;
}
// Make sure links are absolute, some readers are not smart enough to figure it out
if (node.name === "a" && node.attributes.src?.startsWith("/")) {
node.attributes.src = getBaseSiteURL().slice(0, -1) + node.attributes.src;
}

// Remove favicon images, some readers don't know they should be inline and it ends up being a broken image
if (
node.type === ELEMENT_NODE &&
("data-favicon" in node.attributes || "data-favicon-span" in node.attributes)
) {
node = node as unknown as TextNode;
node.type = TEXT_NODE;
node.value = "";
}
// Remove favicon images, some readers don't know they should be inline and it ends up being a broken image
if ("data-favicon" in node.attributes || "data-favicon-span" in node.attributes) {
node = node as unknown as TextNode;
node.type = TEXT_NODE;
node.value = "";
}

// Remove EC buttons
if (node.type === ELEMENT_NODE && node.attributes["data-code"]) {
node = node as unknown as TextNode;
node.type = TEXT_NODE;
node.value = "";
// Remove EC buttons
if (node.attributes["data-code"]) {
node = node as unknown as TextNode;
node.type = TEXT_NODE;
node.value = "";
}
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/pages/rss/blog.astro
Expand Up @@ -14,7 +14,7 @@ function makeIntro() {
return `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>Erika's blog</title>
<title><![CDATA[Erika's blog]]></title>
<link>${getBaseSiteURL()}articles</link>
<description>My personal blog</description>
<language>en</language>
Expand All @@ -39,9 +39,9 @@ function makeOutro() {
{
articles.map(async (article) => {
const start = `\t<item>
<title>${article.data.title}</title>
<title><![CDATA[${article.data.title}]]></title>
<link>${getBaseSiteURL().slice(0, -1)}${getURLFromEntry(article)}</link>
<description>${article.data.tagline}</description>
<description><![CDATA[${article.data.tagline}]]></description>
<pubDate>${new Date(article.data.date).toUTCString()}</pubDate>
<content:encoded><![CDATA[`.trim();
const end = `]]></content:encoded>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/rss/catalogue.astro
Expand Up @@ -46,9 +46,9 @@ function makeOutro() {
{
catalogueContent.map(async (entry) => {
const start = `\t<item>
<title>${entry.data.title}</title>
<title><![CDATA[${entry.data.title}]]></title>
<link>${getBaseSiteURL()}catalogue</link>
<description>Entry for ${entry.data.title}</description>
<description><![CDATA[Entry for ${entry.data.title}]]></description>
<enclosure url="${(await getImage({ src: entry.data.cover, width: 240 })).src}" length="0" type="image/${entry.data.cover.format}" />
<pubDate>${new Date(entry.data.finishedDate).toUTCString()}</pubDate>
<guid isPermaLink="false">${entry.data.title}</guid>
Expand Down

0 comments on commit a75b8c9

Please sign in to comment.