add tag based rss feed

This commit is contained in:
Timothy 2021-01-13 18:35:20 +08:00
parent 89020e4308
commit 1c2f40424b
3 changed files with 33 additions and 2 deletions

View File

@ -7,18 +7,22 @@ const generateRssItem = (post) => `
<link>${siteMetadata.siteUrl}${post.slug}</link>
<description>${post.summary}</description>
<pubDate>${new Date(post.date).toUTCString()}</pubDate>
<author>${siteMetadata.email} (${siteMetadata.author})</author>
${post.tags.map((t) => `<category>${t}</category>`).join('')}
</item>
`
const generateRss = (posts) => `
const generateRss = (posts, page = 'index.xml') => `
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${siteMetadata.title}</title>
<link>${siteMetadata.siteUrl}/blog</link>
<description>${siteMetadata.description}</description>
<language>${siteMetadata.language}</language>
<managingEditor>${siteMetadata.email} (${siteMetadata.author})</managingEditor>
<webMaster>${siteMetadata.email} (${siteMetadata.author})</webMaster>
<lastBuildDate>${new Date(posts[0].date).toUTCString()}</lastBuildDate>
<atom:link href="${siteMetadata.siteUrl}/index.xml" rel="self" type="application/rss+xml"/>
<atom:link href="${siteMetadata.siteUrl}/${page}" rel="self" type="application/rss+xml"/>
${posts.map(generateRssItem).join('')}
</channel>
</rss>

View File

@ -1,9 +1,14 @@
import fs from 'fs'
import path from 'path'
import kebabCase from 'just-kebab-case'
import { getAllFilesFrontMatter } from '@/lib/mdx'
import { getAllTags } from '@/lib/tags'
import siteMetadata from '@/data/siteMetadata'
import ListLayout from '@/layouts/ListLayout'
import { PageSeo } from '@/components/SEO'
import generateRss from '@/lib/generate-rss'
const root = process.cwd()
export async function getStaticPaths() {
const tags = await getAllTags('blog')
@ -24,6 +29,12 @@ export async function getStaticProps({ params }) {
(post) => post.draft !== true && post.tags.map((t) => kebabCase(t)).includes(params.tag)
)
// rss
const rss = generateRss(filteredPosts, `tags/${params.tag}/index.xml`)
const rssPath = path.join(root, 'public', 'tags', params.tag)
fs.mkdirSync(rssPath, { recursive: true })
fs.writeFileSync(path.join(rssPath, 'index.xml'), rss)
return { props: { posts: filteredPosts, tag: params.tag } }
}

View File

@ -5,6 +5,8 @@
<link>https://tailwind-nextjs-starter-blog.vercel.app/blog</link>
<description>A blog created with Next.js and Tailwind.css</description>
<language>en-us</language>
<managingEditor>address@yoursite.com (Tails Azimuth)</managingEditor>
<webMaster>address@yoursite.com (Tails Azimuth)</webMaster>
<lastBuildDate>Tue, 12 Jan 2021 00:00:00 GMT</lastBuildDate>
<atom:link href="https://tailwind-nextjs-starter-blog.vercel.app/index.xml" rel="self" type="application/rss+xml"/>
@ -14,6 +16,8 @@
<link>https://tailwind-nextjs-starter-blog.vercel.appintroducing-tailwind-nextjs-starter-blog</link>
<description>Looking for a performant, out of the box template, with all the best in web technology to support your blogging needs? Checkout the Tailwind Nextjs Starter Blog template.</description>
<pubDate>Tue, 12 Jan 2021 00:00:00 GMT</pubDate>
<author>address@yoursite.com (Tails Azimuth)</author>
<category>next-js</category><category>tailwind</category><category>guide</category>
</item>
<item>
@ -22,6 +26,8 @@
<link>https://tailwind-nextjs-starter-blog.vercel.appguide-to-using-images-in-nextjs</link>
<description>In this article we introduce adding images in the tailwind starter blog and the benefits and limitations of the next/image component.</description>
<pubDate>Wed, 11 Nov 2020 00:00:00 GMT</pubDate>
<author>address@yoursite.com (Tails Azimuth)</author>
<category>next js</category><category>guide</category>
</item>
<item>
@ -30,6 +36,8 @@
<link>https://tailwind-nextjs-starter-blog.vercel.appderiving-ols-estimator</link>
<description>How to derive the OLS Estimator with matrix notation and a tour of math typesetting using markdown with the help of KaTeX.</description>
<pubDate>Sat, 16 Nov 2019 00:00:00 GMT</pubDate>
<author>address@yoursite.com (Tails Azimuth)</author>
<category>next js</category><category>math</category><category>ols</category>
</item>
<item>
@ -38,6 +46,8 @@
<link>https://tailwind-nextjs-starter-blog.vercel.appgithub-markdown-guide</link>
<description>Markdown cheatsheet for all your blogging needs - headers, lists, images, tables and more! An illustrated guide based on Github Flavored Markdown.</description>
<pubDate>Fri, 11 Oct 2019 00:00:00 GMT</pubDate>
<author>address@yoursite.com (Tails Azimuth)</author>
<category>github</category><category>guide</category>
</item>
<item>
@ -46,6 +56,8 @@
<link>https://tailwind-nextjs-starter-blog.vercel.appthe-time-machine</link>
<description>The Time Traveller (for so it will be convenient to speak of him) was expounding a recondite matter to us. His pale grey eyes shone and twinkled, and his usually pale face was flushed and animated...</description>
<pubDate>Wed, 15 Aug 2018 00:00:00 GMT</pubDate>
<author>address@yoursite.com (Tails Azimuth)</author>
<category>writings</category><category>book</category><category>reflection</category>
</item>
<item>
@ -54,6 +66,8 @@
<link>https://tailwind-nextjs-starter-blog.vercel.apppictures-of-canada</link>
<description>The scenic lands of Canada featuring maple leaves, snow-capped mountains, turquoise lakes and Toronto. Take in the sights in this photo gallery exhibition and see how easy it is to replicate with some MDX magic and tailwind classes.</description>
<pubDate>Sat, 15 Jul 2017 00:00:00 GMT</pubDate>
<author>address@yoursite.com (Tails Azimuth)</author>
<category>holiday</category><category>canada</category><category>images</category>
</item>
<item>
@ -62,6 +76,8 @@
<link>https://tailwind-nextjs-starter-blog.vercel.appcode-sample</link>
<description>Example of a markdown file with code blocks and syntax highlighting</description>
<pubDate>Tue, 08 Mar 2016 00:00:00 GMT</pubDate>
<author>address@yoursite.com (Tails Azimuth)</author>
<category>markdown</category><category>code</category><category>features</category>
</item>
</channel>