|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + */ |
| 4 | +const Feed = require('rss'); |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | +const matter = require('gray-matter'); |
| 8 | + |
| 9 | +const getAllFiles = function (dirPath, arrayOfFiles) { |
| 10 | + const files = fs.readdirSync(dirPath); |
| 11 | + |
| 12 | + arrayOfFiles = arrayOfFiles || []; |
| 13 | + |
| 14 | + files.forEach(function (file) { |
| 15 | + if (fs.statSync(dirPath + '/' + file).isDirectory()) { |
| 16 | + arrayOfFiles = getAllFiles(dirPath + '/' + file, arrayOfFiles); |
| 17 | + } else { |
| 18 | + arrayOfFiles.push(path.join(dirPath, '/', file)); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + return arrayOfFiles; |
| 23 | +}; |
| 24 | + |
| 25 | +exports.generateRssFeed = function () { |
| 26 | + const feed = new Feed({ |
| 27 | + title: 'React Blog', |
| 28 | + description: |
| 29 | + '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.', |
| 30 | + feed_url: 'https://react.dev/rss.xml', |
| 31 | + site_url: 'https://react.dev/', |
| 32 | + language: 'en', |
| 33 | + favicon: 'https://react.dev/favicon.ico', |
| 34 | + pubDate: new Date(), |
| 35 | + generator: 'react.dev rss module', |
| 36 | + }); |
| 37 | + |
| 38 | + const dirPath = path.join(process.cwd(), 'src/content/blog'); |
| 39 | + const filesByOldest = getAllFiles(dirPath); |
| 40 | + const files = filesByOldest.reverse(); |
| 41 | + |
| 42 | + for (const filePath of files) { |
| 43 | + const id = filePath.split('/').slice(-1).join(''); |
| 44 | + if (id !== 'index.md') { |
| 45 | + const content = fs.readFileSync(filePath, 'utf-8'); |
| 46 | + const {data} = matter(content); |
| 47 | + const slug = filePath.split('/').slice(-4).join('/').replace('.md', ''); |
| 48 | + |
| 49 | + if (data.title == null || data.title.trim() === '') { |
| 50 | + throw new Error( |
| 51 | + `${id}: Blog posts must include a title in the metadata, for RSS feeds` |
| 52 | + ); |
| 53 | + } |
| 54 | + if (data.author == null || data.author.trim() === '') { |
| 55 | + throw new Error( |
| 56 | + `${id}: Blog posts must include an author in the metadata, for RSS feeds` |
| 57 | + ); |
| 58 | + } |
| 59 | + if (data.date == null || data.date.trim() === '') { |
| 60 | + throw new Error( |
| 61 | + `${id}: Blog posts must include a date in the metadata, for RSS feeds` |
| 62 | + ); |
| 63 | + } |
| 64 | + if (data.description == null || data.description.trim() === '') { |
| 65 | + throw new Error( |
| 66 | + `${id}: Blog posts must include a description in the metadata, for RSS feeds` |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + feed.item({ |
| 71 | + id, |
| 72 | + title: data.title, |
| 73 | + author: data.author || '', |
| 74 | + date: new Date(data.date), |
| 75 | + url: `https://react.dev/blog/${slug}`, |
| 76 | + description: data.description, |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + fs.writeFileSync('./public/rss.xml', feed.xml({indent: true})); |
| 82 | +}; |
0 commit comments