Skip to content

Commit

Permalink
Merge pull request #221 from arthur-vanpassel/develop
Browse files Browse the repository at this point in the history
NEWIO_SITE-3626 add author name & avatar to feed.xml
  • Loading branch information
sanderdejong88 committed Dec 20, 2023
2 parents 97bd234 + 87be055 commit 4543b5e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
71 changes: 45 additions & 26 deletions lib/generate-rss.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
import { escape } from '@/lib/utils/htmlEscaper'
import { getAuthors } from '@/lib/authors'

import siteMetadata from '@/data/siteMetadata'

const generateRssItem = (post) => `
<item>
<guid>${siteMetadata.siteUrl}/articles/${post.slug}</guid>
<title>${escape(post.title)}</title>
<link>${siteMetadata.siteUrl}/articles/${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>
`
const generateRssItem = (post, authors) => `
<item>
<guid>${siteMetadata.siteUrl}/articles/${post.slug}</guid>
<title>${escape(post.title)}</title>
<link>${siteMetadata.siteUrl}/articles/${post.slug}</link>
${post.summary && `<description>${escape(post.summary)}</description>`}
<pubDate>${new Date(post.date).toUTCString()}</pubDate>
<author>${siteMetadata.email} (${siteMetadata.author})</author>
${
post.authors
? post.authors
.map(
(a) => `
<io:author>
<name>${authors[a].name}</name>
<avatar>${authors[a].avatar}</avatar>
</io:author>
`
)
.join('')
: ''
}
${post.images ? post.images.map((i) => `<image>${i}</image>`).join('') : ''}
${post.tags ? post.tags.map((t) => `<category>${t}</category>`).join('') : ''}
</item>
`

const generateRss = (posts, page = 'feed.xml') => `
<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>
<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}/${page}" rel="self" type="application/rss+xml"/>
${posts.map(generateRssItem).join('')}
</channel>
</rss>
`
const generateRss = async (posts, page = 'feed.xml') => {
const authors = await getAuthors(posts)
return `
<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>
<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}/${page}" rel="self" type="application/rss+xml"/>
${posts.map((element) => generateRssItem(element, authors)).join('')}
</channel>
</rss>
`
}
export default generateRss
2 changes: 1 addition & 1 deletion pages/articles/[...slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function getStaticProps({ params }) {

// rss
if (allPosts.length > 0) {
const rss = generateRss(allPosts)
const rss = await generateRss(allPosts)
fs.writeFileSync('./public/feed.xml', rss)
}

Expand Down
2 changes: 1 addition & 1 deletion pages/series/[...slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function getStaticProps({ params }) {

// rss
if (allSeries.length > 0) {
const rss = generateRss(allSeries)
const rss = await generateRss(allSeries)
fs.writeFileSync('./public/feed.xml', rss)
}

Expand Down
2 changes: 1 addition & 1 deletion pages/tags/[tag].js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function getStaticProps({ params }) {

// rss
if (filteredPosts.length > 0) {
const rss = generateRss(filteredPosts, `tags/${params.tag}/feed.xml`)
const rss = await generateRss(filteredPosts, `tags/${params.tag}/feed.xml`)
const rssPath = path.join(root, 'public', 'tags', params.tag)
fs.mkdirSync(rssPath, { recursive: true })
fs.writeFileSync(path.join(rssPath, 'feed.xml'), rss)
Expand Down

1 comment on commit 4543b5e

@vercel
Copy link

@vercel vercel bot commented on 4543b5e Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.