Skip to content
This repository was archived by the owner on Feb 14, 2022. It is now read-only.

Commit bc55222

Browse files
committed
Setup RSS correctly
1 parent fa86a00 commit bc55222

File tree

8 files changed

+146
-91
lines changed

8 files changed

+146
-91
lines changed

next.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ module.exports = withBundleAnalyzer({
7878
}),
7979
],
8080
},
81+
{
82+
resourceQuery: /rss/,
83+
use: [
84+
...mdx,
85+
createLoader(function (src) {
86+
return this.callback(null, src)
87+
}),
88+
],
89+
},
8190
{
8291
use: [
8392
...mdx,

package-lock.json

Lines changed: 53 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
},
2424
"devDependencies": {
2525
"@next/bundle-analyzer": "^9.4.4",
26+
"feed": "^4.2.1",
2627
"file-loader": "^6.0.0",
27-
"rss": "^1.2.2",
2828
"simple-functional-loader": "^1.2.1"
2929
}
3030
}

scripts/build-rss.js

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,69 @@
11
import fs from 'fs'
2-
import RSS from 'rss'
3-
import getAllPostPreviews from '../src/getAllPostPreviews'
2+
import path from 'path'
3+
import ReactDOMServer from 'react-dom/server'
4+
import { MDXProvider } from '@mdx-js/react'
5+
import { Feed } from 'feed'
46

5-
const feed = new RSS({
7+
import { mdxComponents } from '../src/components/Post'
8+
import { getAllPosts } from '../src/getAllPostPreviews'
9+
10+
const siteUrl = 'https://blog.tailwindcss.com'
11+
12+
const feed = new Feed({
613
title: 'Blog – Tailwind CSS',
7-
site_url: 'https://blog.tailwindcss.com',
8-
feed_url: 'https://blog.tailwindcss.com/feed.xml',
14+
description: 'All the latest Tailwind CSS news, straight from the team.',
15+
id: siteUrl,
16+
link: siteUrl,
17+
language: 'en',
18+
image: `${siteUrl}/favicon-32x32.png`,
19+
favicon: `${siteUrl}/favicon.ico`,
20+
copyright: 'All rights reserved 2020, Tailwind Labs',
21+
feedLinks: {
22+
json: `${siteUrl}/json`,
23+
atom: `${siteUrl}/atom`,
24+
},
25+
author: {
26+
name: 'Adam Wathan',
27+
email: 'adam.wathan@gmail.com',
28+
link: 'https://twitter.com/@adamwathan',
29+
},
930
})
1031

11-
getAllPostPreviews().forEach(({ link, module: { meta } }) => {
12-
feed.item({
32+
getAllPosts().forEach(({ link, module: { meta, default: Content } }) => {
33+
const mdx = (
34+
<MDXProvider components={mdxComponents}>
35+
<Content />
36+
</MDXProvider>
37+
)
38+
const html = ReactDOMServer.renderToStaticMarkup(mdx)
39+
const postText = `<div style="margin-top=55px; font-style: italic;">(The post <a href="${
40+
siteUrl + link
41+
}">${meta.title}</a> appeared first on <a href="${siteUrl}">Tailwind CSS Blog</a>.)</div>`
42+
feed.addItem({
1343
title: meta.title,
14-
guid: link,
15-
url: `https://blog.tailwindcss.com${link}`,
16-
date: meta.date,
44+
id: meta.title,
45+
link,
46+
comments: meta.discussion,
1747
description: meta.description,
18-
custom_elements: [].concat(meta.authors.map((author) => ({ author: [{ name: author.name }] }))),
48+
content: html + postText,
49+
author: meta.authors.map(({ name, twitter }) => ({
50+
name,
51+
link: `https://twitter.com/${twitter}`,
52+
})),
53+
date: new Date(meta.date),
54+
image: siteUrl + meta.image,
55+
extensions: [
56+
{
57+
name: '_comments',
58+
objects: {
59+
about: 'link to discussions forum',
60+
comments: meta.discussion,
61+
},
62+
},
63+
],
1964
})
2065
})
2166

22-
fs.writeFileSync('./out/feed.xml', feed.xml({ indent: true }))
67+
fs.writeFileSync('./out/feed.xml', feed.rss2())
68+
fs.writeFileSync('./out/atom.xml', feed.atom1())
69+
fs.writeFileSync('./out/feed.json', feed.json1())

src/components/Post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Link from 'next/link'
55
import { useRouter } from 'next/router'
66
import { MDXProvider } from '@mdx-js/react'
77

8-
const mdxComponents = {
8+
export const mdxComponents = {
99
pre: ({ className, ...props }) => (
1010
<pre className={`${className} rounded-md bg-gray-800 py-3 px-4 overflow-x-auto`} {...props} />
1111
),

src/getAllPostPreviews.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ export default function getAllPostPreviews() {
1818
dateSortDesc(a.module.meta.date, b.module.meta.date)
1919
)
2020
}
21+
22+
export function getAllPosts() {
23+
return importAll(require.context('./pages/?rss', true, /\.mdx$/)).sort((a, b) =>
24+
dateSortDesc(a.module.meta.date, b.module.meta.date)
25+
)
26+
}

src/pages/_app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export default function App({ Component, pageProps }) {
1414
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
1515
<meta name="msapplication-TileColor" content="#00aba9" />
1616
<meta name="theme-color" content="#ffffff" />
17-
<link rel="alternate" type="application/rss+xml" href="/feed.xml" />
17+
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="/feed.xml" />
18+
<link rel="alternate" type="application/atom+xml" title="Atom 1.0" href="/atom.xml" />
19+
<link rel="alternate" type="application/json" title="JSON Feed" href="/feed.json" />
1820
</Head>
1921
<SectionContainer>
2022
<Header />

0 commit comments

Comments
 (0)