-
-
Save Uvacoder/67adec757185af23549543cc7d8b2a95 to your computer and use it in GitHub Desktop.
Generate rss for markdown blog posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://luigicruz.dev/feed.xml | |
| import { readFileSync, readdirSync, writeFileSync } from 'fs' | |
| import { join } from 'path' | |
| import RSS from 'rss' | |
| import matter from 'gray-matter' | |
| async function generate() { | |
| const feed = new RSS({ | |
| title: 'Luigi Cruz', | |
| description: 'Personal blog by Luigi Cruz. Sharing his thoughts about his world.', | |
| site_url: 'https://luigicruz.dev', | |
| feed_url: 'https://luigicruz.dev/feed.xml', | |
| }) | |
| const posts = readdirSync(join(process.cwd(), 'data', 'blog')) | |
| posts.map((name) => { | |
| const content = readFileSync(join(process.cwd(), 'data', 'blog', name)) | |
| const frontmatter = matter(content) | |
| feed.item({ | |
| title: frontmatter.data.title, | |
| url: 'https://luigicruz.dev/blog/' + name.replace(/\.mdx?/, ''), | |
| date: frontmatter.data.publishedAt, | |
| description: frontmatter.data.summary, | |
| author: 'https://twitter.com/luigircruz Luigi Cruz', | |
| }) | |
| }) | |
| writeFileSync('./public/feed.xml', feed.xml({ indent: true })) | |
| } | |
| generate() | |
| // On your package.json file, | |
| // Add this script that executes this file | |
| "scripts": { | |
| ... | |
| "postbuild": "node ./scripts/generate-rss.mjs", | |
| }, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment