Skip to content

Commit

Permalink
feat(lib): create function to generate atom feed
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 committed Jun 15, 2023
1 parent 1d815c4 commit 5c7dfaf
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/app/(blog)/feed/generate-feed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { getSortedPosts } from '@/shared/lib/get-sorted-posts'
import { host } from '@/shared/lib/webserver-constants'
import { allPosts } from 'contentlayer/generated'
import { Feed } from 'feed'
import { markdownToHtml } from './markdown-to-html'

export function generateFeed() {
const date = new Date()

const posts = getSortedPosts(allPosts).filter(
post => post.status === 'published'
)

const feed = new Feed({
title: "Mateus Felipe's interests",
description:
'This is my "corner of internet", where I take some tests, document my studies and write about some subjects I like...',
id: host,
link: host,
favicon: `${host}/assets/brain.png`,
copyright: `All rights reserved ${date.getFullYear()}, Mateus Felipe.`,
updated: posts.length > 0 ? new Date(posts[0].date) : date,
feedLinks: {
rss2: `${host}/feed`
},
docs: 'https://github.com/mateusfg7/mfg-b',
generator: 'Feed for Node.js',
author: {
name: 'Mateus Felipe Gonçalves',
email: '[email protected]',
link: 'https://mateusf.vercel.app'
}
})

posts.forEach(post => {
const { name, email, url } = post.author_info

feed.addItem({
title: post.title,
id: post.id,
link: `${host}/post/${post.id}`,
description: post.description,
content: markdownToHtml(post.body.raw),
author: [{ name, email, link: url }],
date: new Date(post.date),
category: post.tags.split(',').map(tag => ({ name: tag.trim() }))
})
})

return feed.atom1()
}

0 comments on commit 5c7dfaf

Please sign in to comment.