1 import { changelogEntries } from "@/generated/changelog/index.mjs"
2
3 /**
4 * A single dated changelog entry, as authored in
5 * `generated/changelog/{YYYY-MM-DD}.mjs`.
6 */
7 export type ChangelogEntry = {
8 /**
9 * The date the changes went live, in `YYYY-MM-DD` format.
10 */
11 date: string
12 /**
13 * A short headline for the entry, written alongside it — the changelog page
14 * uses it as the entry's heading. Optional, and falls back to the entry's
15 * formatted date.
16 */
17 title?: string
18 /**
19 * The entry's changes as Markdown, without the heading. Links to other Cloud
20 * documentation pages are root-relative and omit the base path, such as
21 * `/environments/custom-domains`.
22 */
23 content: string
24 /**
25 * A one-sentence summary of what the entry covers, written alongside the
26 * entry. It isn't rendered on the changelog page, and is only exposed through
27 * the public endpoint.
28 */
29 summary?: string
30 /**
31 * The Cloudinary URL of the entry's banner image, rendered and uploaded by
32 * the changelog pipeline. Like {@link summary}, it isn't rendered on the
33 * changelog page and is only exposed through the public endpoint.
34 */
35 image?: string
36 }
37
38 /**
39 * A changelog entry as it's exposed to the changelog page and the public
40 * `/api/changelog` endpoint.
41 */
42 export type PublicChangelogEntry = Omit<
43 ChangelogEntry,
44 "title" | "summary" | "image"
45 > & {
46 /**
47 * The entry's anchor ID on the changelog page, such as `august-10-2026`.
48 * Derived from the date rather than the title, so rewording a title doesn't
49 * break a permalink to the entry.
50 */
51 id: string
52 /**
53 * The entry's headline, falling back to {@link displayDate} when it was
54 * written without one.
55 */
56 title: string
57 /**
58 * The entry's date, formatted for display, such as `August 10, 2026`.
59 */
60 displayDate: string
61 /**
62 * `null` when the entry was written without a summary.
63 */
64 summary: string | null
65 /**
66 * `null` when the entry has no banner image, which happens when the upload
67 * was skipped or failed.
68 */
69 image: string | null
70 }
71
72 export type ChangelogPage = {
73 entries: PublicChangelogEntry[]
74 /**
75 * The 1-based number of the returned page.
76 */
77 page: number
78 limit: number
79 /**
80 * The total number of changelog entries, across all pages.
81 */
82 count: number
83 has_more: boolean
84 }
85
86 export type GetChangelogPageOptions = {
87 /**
88 * The 1-based page to load. Defaults to `1`.
89 */
90 page?: number
91 /**
92 * How many entries to load. Defaults to {@link CHANGELOG_PAGE_SIZE}.
93 */
94 limit?: number
95 /**
96 * When set, root-relative links in each entry's content are rewritten to
97 * absolute URLs. Pass this for consumers that can't resolve a path against
98 * the documentation site, such as the public endpoint and the Markdown
99 * version of the page.
100 */
101 baseUrl?: string
102 }
103
104 export const CHANGELOG_PAGE_SIZE = 10
105 export const CHANGELOG_MAX_PAGE_SIZE = 50
106
107 const MONTHS = [
108 "January",
109 "February",
110 "March",
111 "April",
112 "May",
113 "June",
114 "July",
115 "August",
116 "September",
117 "October",
118 "November",
119 "December",
120 ]
121
122 /**
123 * Formats a `YYYY-MM-DD` date as `{Month} {Day}, {Year}`. The date is parsed
124 * manually rather than with `Date`, so the result doesn't shift with the
125 * server's time zone.
126 */
127 export function formatChangelogDate(date: string): string {
128 const [year, month, day] = date.split("-")
129 const monthName = MONTHS[parseInt(month, 10) - 1]
130
131 if (!monthName) {
132 return date
133 }
134
135 return `${monthName} ${parseInt(day, 10)}, ${year}`
136 }
137
138 export function getChangelogEntryId(date: string): string {
139 return formatChangelogDate(date)
140 .toLowerCase()
141 .replace(/[^a-z0-9]+/g, "-")
142 .replace(/^-|-$/g, "")
143 }
144
145 export function getChangelogCount(): number {
146 return changelogEntries.length
147 }
148
149 /**
150 * Rewrites root-relative Markdown links, such as `/environments`, to absolute
151 * URLs. Links that are already absolute, anchors, and mail links are untouched.
152 */
153 function absolutizeLinks(content: string, baseUrl: string): string {
154 const prefix = `${baseUrl.replace(/\/$/, "")}${process.env.NEXT_PUBLIC_BASE_PATH || ""}`
155
156 return content.replace(
157 /\]\((\/[^)\s]*)\)/g,
158 (_match, url) => `](${prefix}${url})`
159 )
160 }
161
162 /** Shapes an authored entry into what the page and the endpoints expose. */
163 function toPublicEntry(
164 date: string,
165 entry: ChangelogEntry,
166 baseUrl?: string
167 ): PublicChangelogEntry {
168 const displayDate = formatChangelogDate(date)
169
170 return {
171 id: getChangelogEntryId(date),
172 title: entry.title?.trim() || displayDate,
173 date,
174 displayDate,
175 summary: entry.summary ?? null,
176 image: entry.image ?? null,
177 content: baseUrl ? absolutizeLinks(entry.content, baseUrl) : entry.content,
178 }
179 }
180
181 /**
182 * Loads a single entry by its `YYYY-MM-DD` date, or `null` when the changelog
183 * has no entry for that date. Only that entry's file is imported.
184 */
185 export async function getChangelogEntry(
186 date: string,
187 baseUrl?: string
188 ): Promise<PublicChangelogEntry | null> {
189 const match = changelogEntries.find((entry) => entry.date === date)
190
191 if (!match) {
192 return null
193 }
194
195 const { default: entry } = await match.load()
196
197 return toPublicEntry(match.date, entry, baseUrl)
198 }
199
200 /**
201 * Loads a page of changelog entries, newest first. Only the entries of the
202 * requested page are imported, so the cost of loading a page doesn't grow with
203 * the number of entries in the changelog.
204 */
205 export async function getChangelogPage({
206 page = 1,
207 limit = CHANGELOG_PAGE_SIZE,
208 baseUrl,
209 }: GetChangelogPageOptions = {}): Promise<ChangelogPage> {
210 const normalizedPage = Math.max(1, Math.floor(page))
211 const normalizedLimit = Math.min(
212 CHANGELOG_MAX_PAGE_SIZE,
213 Math.max(1, Math.floor(limit))
214 )
215 const offset = (normalizedPage - 1) * normalizedLimit
216 const slice = changelogEntries.slice(offset, offset + normalizedLimit)
217
218 const entries = await Promise.all(
219 slice.map(async ({ date, load }) => {
220 const { default: entry } = await load()
221
222 return toPublicEntry(date, entry, baseUrl)
223 })
224 )
225
226 return {
227 entries,
228 page: normalizedPage,
229 limit: normalizedLimit,
230 count: changelogEntries.length,
231 has_more: offset + entries.length < changelogEntries.length,
232 }
233 }
234
235 /**
236 * Builds the Markdown version of the full changelog list, used by the
237 * `md-content` route to replace the `<ChangelogList />` component with the
238 * entries it renders.
239 */
240 export async function getChangelogMarkdown(baseUrl?: string): Promise<string> {
241 // Unlike the page and the endpoint, this loads every entry, since the
242 // Markdown version of a page must hold the same content as the page itself.
243 const entries = await Promise.all(
244 changelogEntries.map(async ({ date, load }) => {
245 const { default: entry } = await load()
246
247 return toPublicEntry(date, entry, baseUrl)
248 })
249 )
250
251 return entries
252 .map(
253 (entry) =>
254 `## ${entry.title}\n\n_${entry.displayDate}_\n\n${entry.content}`
255 )
256 .join("\n\n---\n\n")
257 }