|
| 1 | +import { promises as fs } from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import matter from "gray-matter"; |
| 4 | +import { Feed } from "feed"; |
| 5 | +import { unified } from "unified"; |
| 6 | +import remarkParse from "remark-parse"; |
| 7 | +import remarkRehype from "remark-rehype"; |
| 8 | +import rehypeStringify from "rehype-stringify"; |
| 9 | + |
| 10 | +async function mdToHtml(md: string): Promise<string> { |
| 11 | + const result = await unified() |
| 12 | + .use(remarkParse) |
| 13 | + .use(remarkRehype) |
| 14 | + .use(rehypeStringify) |
| 15 | + .process(md); |
| 16 | + |
| 17 | + return result.toString(); |
| 18 | +} |
| 19 | + |
| 20 | +const BASE_URL = "https://ghostty.org"; |
| 21 | +const RELEASE_NOTES_DIRECTORY = "./docs/install/release-notes/"; |
| 22 | +const FEED_FILENAME = "feed.json"; |
| 23 | + |
| 24 | +export async function generateFeed(): Promise<string> { |
| 25 | + const releaseNotesUrl = new URL(RELEASE_NOTES_DIRECTORY, BASE_URL).href; |
| 26 | + const feedUrl = new URL(FEED_FILENAME, releaseNotesUrl).href; |
| 27 | + const currentYear = new Date().getFullYear(); |
| 28 | + |
| 29 | + const feed = new Feed({ |
| 30 | + title: "Ghostty Release Notes", |
| 31 | + description: "Release notes for Ghostty", |
| 32 | + id: feedUrl, |
| 33 | + link: releaseNotesUrl, |
| 34 | + feedLinks: { |
| 35 | + json: feedUrl, |
| 36 | + }, |
| 37 | + favicon: new URL("favicon.ico", BASE_URL).href, |
| 38 | + copyright: `© ${currentYear} Mitchell Hashimoto`, |
| 39 | + }); |
| 40 | + |
| 41 | + const releaseNotesDir = path.join(process.cwd(), RELEASE_NOTES_DIRECTORY); |
| 42 | + const filenames = (await fs.readdir(releaseNotesDir, { withFileTypes: true })) |
| 43 | + .filter((dirent) => |
| 44 | + dirent.isFile() && dirent.name !== "index.mdx" && |
| 45 | + dirent.name.endsWith(".mdx") |
| 46 | + ).map((dirent) => dirent.name); |
| 47 | + |
| 48 | + for (const filename of filenames) { |
| 49 | + const filePath = path.join(RELEASE_NOTES_DIRECTORY, filename); |
| 50 | + |
| 51 | + const { data, content } = matter.read(filePath); |
| 52 | + const contentHtml = await mdToHtml(content); |
| 53 | + |
| 54 | + const slug = filename.replace(".mdx", ""); |
| 55 | + |
| 56 | + const fileUrl = new URL(slug, releaseNotesUrl).href; |
| 57 | + const dateString = data.description?.match( |
| 58 | + /released on ([A-Za-z]+ \d+, \d{4})/i, |
| 59 | + )?.[1]; |
| 60 | + const date = dateString ? new Date(dateString) : new Date(); |
| 61 | + |
| 62 | + feed.addItem({ |
| 63 | + title: data.title, |
| 64 | + id: fileUrl, |
| 65 | + link: fileUrl, |
| 66 | + description: data.description, |
| 67 | + content: contentHtml, |
| 68 | + date, |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + return feed.json1(); |
| 73 | +} |
0 commit comments