Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(front): refactor RSS feed encoding, structure, and include thumbn… #1242

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 50 additions & 42 deletions front/src/lib/_utils/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,55 @@
const feedLink = 'https://www.dgrebb.com';
const feedUpdated = new Date();

export const xml = (posts) => `<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>${feedTitle}</title>
<link href="${feedLink}/RSS.xml" rel="self"/>
<link href="${feedLink}"/>
<id>${feedLink}/</id>
<updated>${feedUpdated.toISOString()}</updated>
<author>
<name>Dan Grebb</name>
</author>
<subtitle>${feedDescription}</subtitle>
<generator>JavaScript</generator>
${posts
.map((post) => {
const { slug, publishedAt, summary } = post.attributes;
export const xml = (posts) => `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel xmlns="http://www.w3.org/2005/Atom">
<title>${feedTitle}</title>
<link href="${feedLink}/RSS.xml" rel="self"/>
<link href="${feedLink}"/>
<id>${feedLink}/</id>
<updated>${feedUpdated.toISOString()}</updated>
<author>
<name>Dan Grebb</name>
</author>
<subtitle>${feedDescription}</subtitle>
<generator>JavaScript</generator>
${posts
.map((post) => {
const { slug, publishedAt, summary } = post.attributes;

const image =
post?.attributes?.hero?.data?.attributes?.formats?.thumbnail?.url ||
false;
const image =
post?.attributes?.hero?.data?.attributes?.formats?.small?.url ||
false,
thumbnail =
post?.attributes?.hero?.data?.attributes?.formats?.thumbnail?.url ||
false,
imageMIME = post?.attributes?.hero?.data?.attributes?.mime || false,
imageAlt =
post?.attributes?.hero?.data?.attributes.alternativeText || false;

const { altText } = post?.attributes?.hero?.data?.attributes || false;

return `<entry>
<title>${post.attributes.title}</title>
<link href="${website}${path}/${slug}/"/>
<id>${website}${path}/${slug}/</id>
<updated>${new Date(publishedAt).toISOString()}</updated>
<published>${new Date(publishedAt).toISOString()}</published>
${
summary ? `<content type="html"><![CDATA[${summary}]]></content>` : ''
}
${
image
? `<image>
<url>${image}</url>
<title>${altText}</title>
<link>${slug}</link>
</image>`
: ''
}
</entry>`;
})
.join('\n')}
</feed>`;
return `<item>
<title>${post.attributes.title.replace('&', '&amp;')}</title>

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This replaces only the first occurrence of '&'.
<link href="${website}${path}/${slug}/"/>
<id>${website}${path}/${slug}/</id>
<updated>${new Date(publishedAt).toISOString()}</updated>
<published>${new Date(publishedAt).toISOString()}</published>
${
summary &&
`
<description>
<![CDATA[
<div style="text-align: center;">
<img src="${image}" alt="${imageAlt}" style="display: inline-block;" />
</div>
<p>${summary}</p>
]]>
</description>`
}
${thumbnail && `<enclosure url="${thumbnail}" type="${imageMIME}" />`}
</item>`;
})
.join('\n')}
</channel>
</rss>
`;
Loading