Improve feed items

This commit is contained in:
Bad Manners 2024-04-03 20:05:25 -03:00
parent e859109ade
commit 568b7709ec
5 changed files with 74 additions and 44 deletions

View file

@ -23,10 +23,10 @@ const getLinkForUser = (user: CollectionEntry<"users">, lang: Lang) => {
const userName = user.data.nameLang[lang] || user.data.name;
if (user.data.preferredLink) {
const link = user.data.links[user.data.preferredLink]!;
return `<a href="${typeof link === "string" ? link : link[0]}">${userName}</a>`
return `<a href="${typeof link === "string" ? link : link[0]}">${userName}</a>`;
}
return userName;
}
};
export const GET: APIRoute = async ({ site }) => {
const stories = (await getCollection("stories", (story) => !story.data.isDraft))
@ -42,37 +42,71 @@ export const GET: APIRoute = async ({ site }) => {
description: "Stories, games, and (possibly) more by Bad Manners",
site: site as URL,
items: [
await Promise.all(stories.map<Promise<FeedItem>>(async ({ data, slug, body }) => ({
title: `New story! "${data.title}"`,
pubDate: toNoonUTCDate(data.pubDate),
link: `/stories/${slug}`,
description:
`Word count: ${data.wordCount}. ${data.contentWarning} ${data.descriptionPlaintext || data.description}`
await Promise.all(
stories.map<Promise<FeedItem>>(async ({ data, slug, body }) => ({
title: `New story! "${data.title}"`,
pubDate: toNoonUTCDate(data.pubDate),
link: `/stories/${slug}`,
description:
`${t(data.lang, "story/warnings", data.wordCount, data.contentWarning.trim())} ${data.descriptionPlaintext || data.description}`
.replaceAll(/[\n ]+/g, " ")
.trim(),
categories: ["story"],
content: sanitizeHtml(
`<h1>${data.title}</h1>` +
`<p>${t(
data.lang,
"story/authors",
[data.authors].flatMap((authorArray) => {
if (!Array.isArray(authorArray)) {
authorArray = [authorArray];
}
return authorArray.map((author) =>
getLinkForUser(users.find((user) => user.id === author.id)!, data.lang),
);
}),
)}</p>` +
(data.requester
? `<p>${t(data.lang, "export_story/request_for", getLinkForUser(users.find((user) => user.id === data.requester!.id)!, data.lang))}</p>`
: "") +
(data.commissioner
? `<p>${t(data.lang, "export_story/commissioned_by", getLinkForUser(users.find((user) => user.id === data.commissioner!.id)!, data.lang))}</p>`
: "") +
`<hr><p><em>${t(data.lang, "story/warnings", data.wordCount, data.contentWarning.trim())}</em></p>` +
`<hr>${tinyDecode(await marked(body))}` +
`<hr>${tinyDecode(await marked(data.description))}`,
),
})),
),
await Promise.all(
games.map<Promise<FeedItem>>(async ({ data, slug, body }) => ({
title: `New game! "${data.title}"`,
pubDate: toNoonUTCDate(data.pubDate),
link: `/games/${slug}`,
description: `${data.contentWarning} ${data.descriptionPlaintext || data.description}`
.replaceAll(/[\n ]+/g, " ")
.trim(),
categories: ["story"],
content: sanitizeHtml(`<h1>${data.title}</h1><p>${t(data.lang, "story/authors", [data.authors].flatMap((authorArray => {
if (!Array.isArray(authorArray)) {
authorArray = [authorArray]
}
return authorArray.map(author => getLinkForUser(users.find(user => user.id === author.id)!, data.lang));
})))}</p>${data.requester ? `<p>${t(data.lang, "export_story/request_for", getLinkForUser(users.find(user => user.id === data.requester!.id)!, data.lang))}</p>` : ""}${data.commissioner ? `<p>${t(data.lang, "export_story/commissioned_by", getLinkForUser(users.find(user => user.id === data.commissioner!.id)!, data.lang))}</p>` : ""}<hr>${tinyDecode(await marked(body))}`),
}))),
await Promise.all(games.map<Promise<FeedItem>>(async ({ data, slug, body }) => ({
title: `New game! "${data.title}"`,
pubDate: toNoonUTCDate(data.pubDate),
link: `/games/${slug}`,
description: `${data.contentWarning} ${data.descriptionPlaintext || data.description}`
.replaceAll(/[\n ]+/g, " ")
.trim(),
categories: ["game"],
content: sanitizeHtml(`<h1>${data.title}</h1><p>${t(data.lang, "story/authors", [data.authors].flatMap((authorArray => {
if (!Array.isArray(authorArray)) {
authorArray = [authorArray]
}
return authorArray.map(author => getLinkForUser(users.find(user => user.id === author.id)!, data.lang));
})))}</p><hr>${tinyDecode(await marked(body))}`),
}))),
categories: ["game"],
content: sanitizeHtml(
`<h1>${data.title}</h1>` +
`<p>${t(
data.lang,
"story/authors",
[data.authors].flatMap((authorArray) => {
if (!Array.isArray(authorArray)) {
authorArray = [authorArray];
}
return authorArray.map((author) =>
getLinkForUser(users.find((user) => user.id === author.id)!, data.lang),
);
}),
)}</p>` +
`<hr><p><em>${data.contentWarning.trim()}</em></p>` +
`<hr>${tinyDecode(await marked(body))}` +
`<hr>${tinyDecode(await marked(data.description))}`,
),
})),
),
]
.flat()
.sort((a, b) => b.pubDate.getTime() - a.pubDate.getTime())