|
| 1 | +import fg from "fast-glob"; |
| 2 | +import * as matter from "gray-matter"; |
| 3 | +import { compileMDX } from "next-mdx-remote/rsc"; |
| 4 | +import { readFile } from "node:fs/promises"; |
| 5 | +import * as React from "react"; |
| 6 | +import rehypeHighlight from "rehype-highlight"; |
| 7 | +import remarkFrontmatter from "remark-frontmatter"; |
| 8 | +import remarkGfm from "remark-gfm"; |
| 9 | +import { z } from "zod"; |
| 10 | + |
| 11 | +const FrontmatterSchema = z.object({ |
| 12 | + title: z.string(), |
| 13 | + description: z.string(), |
| 14 | + slug: z.string(), |
| 15 | + date: z.date(), |
| 16 | +}); |
| 17 | + |
| 18 | +export type ChangelogEntry = { |
| 19 | + filepath: string; |
| 20 | + title: string; |
| 21 | + slug: string; |
| 22 | + description: string; |
| 23 | + date: string; |
| 24 | + source: React.ReactNode; |
| 25 | +}; |
| 26 | + |
| 27 | +function readMatterData(filepath: string) { |
| 28 | + try { |
| 29 | + const { data } = matter.read(filepath); |
| 30 | + return FrontmatterSchema.parse(data); |
| 31 | + } catch (error: unknown) { |
| 32 | + if (error instanceof Error && "code" in error && error.code === "ENOENT") { |
| 33 | + return null; |
| 34 | + } |
| 35 | + throw error; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export async function getDocMdxSource(filepath: string) { |
| 40 | + const source = await readFile(filepath, "utf-8"); |
| 41 | + const result = await compileMDX({ |
| 42 | + source, |
| 43 | + options: { |
| 44 | + mdxOptions: { |
| 45 | + rehypePlugins: [rehypeHighlight], |
| 46 | + remarkPlugins: [remarkGfm, remarkFrontmatter], |
| 47 | + }, |
| 48 | + }, |
| 49 | + }); |
| 50 | + return result.content; |
| 51 | +} |
| 52 | + |
| 53 | +async function getChangelogFromPath( |
| 54 | + filepath: string, |
| 55 | +): Promise<ChangelogEntry | null> { |
| 56 | + const frontmatter = readMatterData(filepath); |
| 57 | + if (!frontmatter) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + const slug = filepath |
| 61 | + .replace(/^.\/changelogs\//, "") |
| 62 | + .replace(/\/index.mdx$/, ""); |
| 63 | + |
| 64 | + const DD_MM_YYYY = frontmatter.date.toISOString().split("T")[0]; |
| 65 | + return { |
| 66 | + filepath, |
| 67 | + title: frontmatter.title, |
| 68 | + description: frontmatter.description, |
| 69 | + slug: `${DD_MM_YYYY}-${slug}`, |
| 70 | + date: frontmatter.date.toISOString(), |
| 71 | + source: await getDocMdxSource(filepath), |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +function validateAllChangelogs( |
| 76 | + changelogs: unknown[], |
| 77 | +): asserts changelogs is ChangelogEntry[] { |
| 78 | + if (changelogs.some((changelog) => changelog === null)) { |
| 79 | + throw new Error("Invalid changelog data"); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +export async function getChangelogs(): Promise<ChangelogEntry[]> { |
| 84 | + const files = await fg("./changelogs/**/*.mdx"); |
| 85 | + const changelogs = await Promise.all(files.map(getChangelogFromPath)); |
| 86 | + validateAllChangelogs(changelogs); |
| 87 | + return changelogs.sort( |
| 88 | + (a, b) => Number(new Date(b.date)) - Number(new Date(a.date)), |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +export async function getChangelogEntryBySlug( |
| 93 | + slug: string, |
| 94 | +): Promise<ChangelogEntry | null> { |
| 95 | + const slugWithoutDate = slug.split("-").slice(3).join("-"); |
| 96 | + const filepath = `./changelogs/${slugWithoutDate}/index.mdx`; |
| 97 | + return getChangelogFromPath(filepath); |
| 98 | +} |
0 commit comments