tailwind-nextjs-blog/scripts/rss.mjs

65 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-08-16 23:55:57 +08:00
import { writeFileSync, mkdirSync } from 'fs';
import path from 'path';
import GithubSlugger from 'github-slugger';
import { escape } from 'pliny/utils/htmlEscaper.js';
import siteMetadata from '../data/siteMetadata.js';
import tagData from '../app/tag-data.json' assert { type: 'json' };
import { allBlogs } from '../.contentlayer/generated/index.mjs';
const generateRssItem = (config, post) => `
<item>
<guid>${config.siteUrl}/blog/${post.slug}</guid>
<title>${escape(post.title)}</title>
<link>${config.siteUrl}/blog/${post.slug}</link>
${post.summary && `<description>${escape(post.summary)}</description>`}
<pubDate>${new Date(post.date).toUTCString()}</pubDate>
<author>${config.email} (${config.author})</author>
${post.tags && post.tags.map((t) => `<category>${t}</category>`).join('')}
</item>
2023-08-16 23:55:57 +08:00
`;
const generateRss = (config, posts, page = 'feed.xml') => `
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${escape(config.title)}</title>
<link>${config.siteUrl}/blog</link>
<description>${escape(config.description)}</description>
<language>${config.language}</language>
<managingEditor>${config.email} (${config.author})</managingEditor>
<webMaster>${config.email} (${config.author})</webMaster>
<lastBuildDate>${new Date(posts[0].date).toUTCString()}</lastBuildDate>
2023-08-16 23:55:57 +08:00
<atom:link href="${
config.siteUrl
}/${page}" rel="self" type="application/rss+xml"/>
${posts.map((post) => generateRssItem(config, post)).join('')}
</channel>
</rss>
2023-08-16 23:55:57 +08:00
`;
async function generateRSS(config, allBlogs, page = 'feed.xml') {
2023-08-16 23:55:57 +08:00
const publishPosts = allBlogs.filter((post) => post.draft !== true);
// RSS for blog post
if (publishPosts.length > 0) {
2023-08-16 23:55:57 +08:00
const rss = generateRss(config, publishPosts);
writeFileSync(`./public/${page}`, rss);
}
if (publishPosts.length > 0) {
for (const tag of Object.keys(tagData)) {
const filteredPosts = allBlogs.filter((post) =>
2023-08-16 23:55:57 +08:00
post.tags.map((t) => GithubSlugger.slug(t)).includes(tag),
);
const rss = generateRss(config, filteredPosts, `tags/${tag}/${page}`);
const rssPath = path.join('public', 'tags', tag);
mkdirSync(rssPath, { recursive: true });
writeFileSync(path.join(rssPath, page), rss);
}
}
}
const rss = () => {
2023-08-16 23:55:57 +08:00
generateRSS(siteMetadata, allBlogs);
console.log('RSS feed generated...');
};
export default rss;