Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rss #63

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.vercel
auth.json
node_modules
public/rss.xml
68 changes: 68 additions & 0 deletions lib/blog.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs'
import RSS from 'rss'
import { join } from 'path'
import matter from 'gray-matter'
import { remark } from 'remark'
Expand Down Expand Up @@ -52,3 +53,70 @@ export async function convertMarkdownToHtml(markdown) {
.process(markdown)
return result.toString()
}

export async function generateRssFeed() {
const posts = getAllPosts([
'title',
'slug',
'date',
'image',
'description',
'content',
])

const feed = new RSS({
title: 'Zeno Rocha',
description: 'Obsessed with developer experience',
site_url: 'https://zenorocha.com',
generator: 'Next.js',
custom_namespaces: {
media: 'http://search.yahoo.com/mrss/',
webfeeds: 'http://webfeeds.org/rss/1.0',
},
custom_elements: [
{ 'webfeeds:icon': 'https://zenorocha.com/static/images/zeno.png' },
{
'atom:link': {
_attr: {
href: `https://zenorocha.com/rss.xml`,
rel: 'self',
type: 'application/rss+xml',
},
},
},
],
})

for await (const post of posts) {
const { title, slug, date, image, description } = post

const content = await convertMarkdownToHtml(post.content || '')

feed.item({
title,
date,
description,
guid: `https://zenorocha.com/${slug}`,
url: `https://zenorocha.com/${slug}?utm_source=rss&utm_medium=rss`,
author: 'Zeno Rocha',
custom_elements: [
{
'media:content': {
_attr: { url: `https://zenorocha.com${image}`, medium: 'image' },
},
},
{
'webfeeds:featuredImage': {
_attr: { url: `https://zenorocha.com${image}` },
},
},
{ 'content:encoded': content },
],
})
}

await fs.writeFileSync(
`${process.cwd()}/public/rss.xml`,
feed.xml({ indent: true })
)
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"remark-html": "^15.0.0",
"remark-prism": "^1.3.6",
"remixicon": "^2.5.0",
"resend": "^0.9.1"
"resend": "^0.9.1",
"rss": "^1.2.2"
},
"devDependencies": {
"eslint": "^8.9.0",
Expand Down
5 changes: 5 additions & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import Footer from '../components/Footer'
import ShortcutHome from '../components/ShortcutHome'
import { PostMain, PostContent, PostContainer } from '../components/Post'
import { Wrapper } from '../components/Wrapper'
import { generateRssFeed } from '../lib/blog'

export async function getStaticProps() {
if (process.env.NODE_ENV === 'production') {
await generateRssFeed()
}

return {
props: {
title: 'Zeno Rocha',
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,18 @@ [email protected]:
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==

mime-db@~1.25.0:
version "1.25.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.25.0.tgz#c18dbd7c73a5dbf6f44a024dc0d165a1e7b1c392"
integrity sha512-5k547tI4Cy+Lddr/hdjNbBEWBwSl8EBc5aSdKvedav8DReADgWJzcYiktaRIw3GtGC1jjwldXtTzvqJZmtvC7w==

[email protected]:
version "2.1.13"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.13.tgz#e07aaa9c6c6b9a7ca3012c69003ad25a39e92a88"
integrity sha512-ryBDp1Z/6X90UvjUK3RksH0IBPM137T7cmg4OgD5wQBojlAiUwuok0QeELkim/72EtcYuNlmbkrcGuxj3Kl0YQ==
dependencies:
mime-db "~1.25.0"

mime-types@^2.1.12:
version "2.1.35"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
Expand Down Expand Up @@ -2848,6 +2860,14 @@ rimraf@^3.0.2:
dependencies:
glob "^7.1.3"

rss@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/rss/-/rss-1.2.2.tgz#50a1698876138133a74f9a05d2bdc8db8d27a921"
integrity sha512-xUhRTgslHeCBeHAqaWSbOYTydN2f0tAzNXvzh3stjz7QDhQMzdgHf3pfgNIngeytQflrFPfy6axHilTETr6gDg==
dependencies:
mime-types "2.1.13"
xml "1.0.1"

run-parallel@^1.1.9:
version "1.2.0"
resolved "https://registry.yarnpkg.com/run-parallel/-/run-parallel-1.2.0.tgz#66d1368da7bdf921eb9d95bd1a9229e7f21a43ee"
Expand Down Expand Up @@ -3386,6 +3406,11 @@ xml-name-validator@^3.0.0:
resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a"
integrity sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==

[email protected]:
version "1.0.1"
resolved "https://registry.yarnpkg.com/xml/-/xml-1.0.1.tgz#78ba72020029c5bc87b8a81a3cfcd74b4a2fc1e5"
integrity sha512-huCv9IH9Tcf95zuYCsQraZtWnJvBtLVE0QHMOs8bWyZAFZNDcYjsPq1nEx8jKA9y+Beo9v+7OBPRisQTjinQMw==

xmlchars@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/xmlchars/-/xmlchars-2.2.0.tgz#060fe1bcb7f9c76fe2a17db86a9bc3ab894210cb"
Expand Down