substr/feeds.ts

50 lines
1.5 KiB
TypeScript

import Article from "./models/article.ts";
import Profile from "./models/profile.ts";
import { isoDate } from "./dates.ts";
export function profileAtomFeed(profile: Profile, articles: Article[]) {
const feedId = `tag:${profile.nip05},nostr-p-${profile.pubkey}-k-30023`;
const lastUpdate = articles.sort((a, b) => b.updatedAt - a.updatedAt)[0]
?.updatedAt;
const articlesXml = articles.map((article) => {
const articleId =
`tag:${profile.nip05},nostr-d-${article.identifier}-k-30023`;
return `
<entry>
<id>${articleId}</id>
<title>${article.title}</title>
<link href="${article.url}" />
<updated>${isoDate(article.updatedAt)}</updated>
<published>${isoDate(article.publishedAt)}</published>
<summary>${article.summary}</summary>
<content type="html"><![CDATA[
${cleanContentHtml(article.html)}
]]></content>
</entry>
`;
}).join("\n");
return `
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>${profile.name} on Nostr (Articles)</title>
<id>${feedId}</id>
<updated>${isoDate(lastUpdate)}</updated>
<icon>${profile.picture}</icon>
<author>
<name>${name}</name>
</author>
${articlesXml}
</feed>
`.trim();
}
export function cleanContentHtml(html: string) {
const cleanHtml = html.replace(
/<a class="anchor" aria-hidden="true"[^>]*>.*?<\/a>/gs,
"",
);
return cleanHtml;
}