|
| 1 | +import Feed from 'rss'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import matter from 'gray-matter'; |
| 5 | + |
| 6 | +const getAllFiles = (dirPath, arrayOfFiles = []) => { |
| 7 | + const files = fs.readdirSync(dirPath); |
| 8 | + |
| 9 | + files.forEach((file) => { |
| 10 | + const filePath = path.join(dirPath, file); |
| 11 | + if (fs.statSync(filePath).isDirectory()) { |
| 12 | + arrayOfFiles = getAllFiles(filePath, arrayOfFiles); |
| 13 | + } else { |
| 14 | + arrayOfFiles.push(filePath); |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | + return arrayOfFiles; |
| 19 | +}; |
| 20 | + |
| 21 | +export async function GET() { |
| 22 | + const feed = new Feed({ |
| 23 | + title: 'React Blog', |
| 24 | + description: |
| 25 | + 'This blog is the official source for the updates from the React team. Anything important, including release notes or deprecation notices, will be posted here first.', |
| 26 | + feed_url: 'https://react.dev/rss.xml', |
| 27 | + site_url: 'https://react.dev/', |
| 28 | + language: 'en', |
| 29 | + favicon: 'https://react.dev/favicon.ico', |
| 30 | + pubDate: new Date(), |
| 31 | + generator: 'react.dev rss module', |
| 32 | + }); |
| 33 | + |
| 34 | + const dirPath = path.join(process.cwd(), 'src/content/blog'); |
| 35 | + const filesByOldest = getAllFiles(dirPath); |
| 36 | + const files = filesByOldest.reverse(); |
| 37 | + |
| 38 | + for (const filePath of files) { |
| 39 | + const id = path.basename(filePath); |
| 40 | + if (id !== 'index.md') { |
| 41 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 42 | + const {data} = matter(content); |
| 43 | + const slug = filePath.split('/').slice(-4).join('/').replace('.md', ''); |
| 44 | + |
| 45 | + if (!data.title || !data.author || !data.date || !data.description) { |
| 46 | + throw new Error( |
| 47 | + `${id}: Blog posts must include title, author, date, and description in metadata.` |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + feed.item({ |
| 52 | + id, |
| 53 | + title: data.title, |
| 54 | + author: data.author, |
| 55 | + date: new Date(data.date), |
| 56 | + url: `https://react.dev/blog/${slug}`, |
| 57 | + description: data.description, |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return new Response(feed.xml({indent: true}), { |
| 63 | + headers: { |
| 64 | + 'Content-Type': 'application/rss+xml', |
| 65 | + }, |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +export const dynamic = 'force-static'; |
0 commit comments