tailwind-nextjs-blog/lib/generate-rss.ts

38 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-10-17 23:37:01 +08:00
import { escape } from '@/lib/utils/htmlEscaper';
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
import siteMetadata from '@/data/siteMetadata';
import { PostFrontMatter } from 'types/PostFrontMatter';
2022-07-17 21:40:41 +08:00
const generateRssItem = (post: PostFrontMatter) => `
2022-07-17 21:40:41 +08:00
<item>
<guid>${siteMetadata.siteUrl}/blog/${post.slug}</guid>
<title>${escape(post.title)}</title>
<link>${siteMetadata.siteUrl}/blog/${post.slug}</link>
${post.summary && `<description>${escape(post.summary)}</description>`}
<pubDate>${new Date(post.date).toUTCString()}</pubDate>
<author>${siteMetadata.email} (${siteMetadata.author})</author>
${post.tags && post.tags.map((t) => `<category>${t}</category>`).join('')}
</item>
2022-10-17 23:37:01 +08:00
`;
2022-07-17 21:40:41 +08:00
const generateRss = (posts: PostFrontMatter[], page = 'feed.xml') => `
2022-07-17 21:40:41 +08:00
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${escape(siteMetadata.title)}</title>
<link>${siteMetadata.siteUrl}/blog</link>
<description>${escape(siteMetadata.description)}</description>
<language>${siteMetadata.language}</language>
2022-10-17 23:37:01 +08:00
<managingEditor>${siteMetadata.email} (${
siteMetadata.author
})</managingEditor>
2022-07-17 21:40:41 +08:00
<webMaster>${siteMetadata.email} (${siteMetadata.author})</webMaster>
<lastBuildDate>${new Date(posts[0].date).toUTCString()}</lastBuildDate>
2022-10-17 23:37:01 +08:00
<atom:link href="${
siteMetadata.siteUrl
}/${page}" rel="self" type="application/rss+xml"/>
2022-07-17 21:40:41 +08:00
${posts.map(generateRssItem).join('')}
</channel>
</rss>
2022-10-17 23:37:01 +08:00
`;
export default generateRss;