blodemd 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/dev-server/app/[[...slug]]/page.tsx +139 -0
  2. package/dev-server/app/blodemd-dev/invalidate/route.ts +12 -0
  3. package/dev-server/app/blodemd-dev/static/[...path]/route.ts +32 -0
  4. package/dev-server/app/blodemd-dev/version/route.ts +14 -0
  5. package/dev-server/app/blodemd-internal/proxy/route.ts +86 -0
  6. package/dev-server/app/error.tsx +24 -0
  7. package/dev-server/app/globals.css +4 -0
  8. package/dev-server/app/layout.tsx +38 -0
  9. package/dev-server/app/not-found.tsx +18 -0
  10. package/dev-server/app/search/route.ts +17 -0
  11. package/dev-server/components/dev-reload-script.tsx +86 -0
  12. package/dev-server/components/providers.tsx +15 -0
  13. package/dev-server/lib/dev-state.ts +8 -0
  14. package/dev-server/lib/local-content-source.ts +103 -0
  15. package/dev-server/lib/local-runtime.tsx +558 -0
  16. package/dev-server/next.config.js +46 -0
  17. package/dev-server/package.json +57 -0
  18. package/dev-server/postcss.config.mjs +7 -0
  19. package/dev-server/public/glide-variable.woff2 +0 -0
  20. package/dev-server/tsconfig.json +49 -0
  21. package/dist/cli.mjs +108 -39
  22. package/dist/cli.mjs.map +1 -1
  23. package/docs/app/globals.css +455 -0
  24. package/docs/components/api/api-playground.tsx +295 -0
  25. package/docs/components/api/api-reference.tsx +121 -0
  26. package/docs/components/content/collection-index.tsx +114 -0
  27. package/docs/components/docs/contextual-menu.tsx +406 -0
  28. package/docs/components/docs/copy-page-menu.tsx +255 -0
  29. package/docs/components/docs/doc-header.tsx +192 -0
  30. package/docs/components/docs/doc-shell.tsx +289 -0
  31. package/docs/components/docs/doc-sidebar.tsx +206 -0
  32. package/docs/components/docs/doc-toc.tsx +45 -0
  33. package/docs/components/docs/mobile-nav.tsx +207 -0
  34. package/docs/components/mdx/accordion.tsx +83 -0
  35. package/docs/components/mdx/badge.tsx +79 -0
  36. package/docs/components/mdx/callout.tsx +88 -0
  37. package/docs/components/mdx/card.tsx +104 -0
  38. package/docs/components/mdx/code-block.tsx +75 -0
  39. package/docs/components/mdx/code-group.tsx +94 -0
  40. package/docs/components/mdx/color.tsx +87 -0
  41. package/docs/components/mdx/columns.tsx +25 -0
  42. package/docs/components/mdx/expandable.tsx +45 -0
  43. package/docs/components/mdx/field-layout.tsx +77 -0
  44. package/docs/components/mdx/frame.tsx +23 -0
  45. package/docs/components/mdx/get-text-content.ts +18 -0
  46. package/docs/components/mdx/icon.tsx +56 -0
  47. package/docs/components/mdx/index.tsx +96 -0
  48. package/docs/components/mdx/installer.tsx +20 -0
  49. package/docs/components/mdx/panel.tsx +11 -0
  50. package/docs/components/mdx/param-field.tsx +56 -0
  51. package/docs/components/mdx/preview.tsx +36 -0
  52. package/docs/components/mdx/prompt.tsx +63 -0
  53. package/docs/components/mdx/request-example.tsx +27 -0
  54. package/docs/components/mdx/response-field.tsx +42 -0
  55. package/docs/components/mdx/steps.tsx +92 -0
  56. package/docs/components/mdx/tabs.tsx +88 -0
  57. package/docs/components/mdx/tile.tsx +43 -0
  58. package/docs/components/mdx/tooltip.tsx +71 -0
  59. package/docs/components/mdx/tree.tsx +120 -0
  60. package/docs/components/mdx/type-table.tsx +71 -0
  61. package/docs/components/mdx/update.tsx +44 -0
  62. package/docs/components/mdx/video.tsx +12 -0
  63. package/docs/components/mdx/view.tsx +66 -0
  64. package/docs/components/providers.tsx +15 -0
  65. package/docs/components/ui/breadcrumb.tsx +92 -0
  66. package/docs/components/ui/button.tsx +90 -0
  67. package/docs/components/ui/card.tsx +92 -0
  68. package/docs/components/ui/command.tsx +139 -0
  69. package/docs/components/ui/dialog.tsx +97 -0
  70. package/docs/components/ui/field.tsx +237 -0
  71. package/docs/components/ui/input.tsx +105 -0
  72. package/docs/components/ui/label.tsx +22 -0
  73. package/docs/components/ui/popover.tsx +72 -0
  74. package/docs/components/ui/search.tsx +380 -0
  75. package/docs/components/ui/separator.tsx +26 -0
  76. package/docs/components/ui/sheet.tsx +104 -0
  77. package/docs/components/ui/sidebar.tsx +433 -0
  78. package/docs/components/ui/theme-toggle.tsx +62 -0
  79. package/docs/components/ui/tooltip.tsx +53 -0
  80. package/docs/lib/contextual-options.ts +193 -0
  81. package/docs/lib/docs-collection.ts +22 -0
  82. package/docs/lib/mdx.ts +90 -0
  83. package/docs/lib/navigation.ts +288 -0
  84. package/docs/lib/openapi.ts +158 -0
  85. package/docs/lib/routes.ts +10 -0
  86. package/docs/lib/server-cache.ts +83 -0
  87. package/docs/lib/shiki.ts +35 -0
  88. package/docs/lib/theme.ts +29 -0
  89. package/docs/lib/toc.ts +2 -0
  90. package/docs/lib/utils.ts +5 -0
  91. package/package.json +33 -4
  92. package/packages/@repo/common/dist/index.d.ts +9 -0
  93. package/packages/@repo/common/dist/index.d.ts.map +1 -0
  94. package/packages/@repo/common/dist/index.js +42 -0
  95. package/packages/@repo/common/package.json +34 -0
  96. package/packages/@repo/common/src/common.unit.test.ts +55 -0
  97. package/packages/@repo/common/src/index.ts +51 -0
  98. package/packages/@repo/contracts/dist/api-key.d.ts +30 -0
  99. package/packages/@repo/contracts/dist/api-key.d.ts.map +1 -0
  100. package/packages/@repo/contracts/dist/api-key.js +20 -0
  101. package/packages/@repo/contracts/dist/dates.d.ts +4 -0
  102. package/packages/@repo/contracts/dist/dates.d.ts.map +1 -0
  103. package/packages/@repo/contracts/dist/dates.js +2 -0
  104. package/packages/@repo/contracts/dist/deployment.d.ts +71 -0
  105. package/packages/@repo/contracts/dist/deployment.d.ts.map +1 -0
  106. package/packages/@repo/contracts/dist/deployment.js +46 -0
  107. package/packages/@repo/contracts/dist/domain.d.ts +94 -0
  108. package/packages/@repo/contracts/dist/domain.d.ts.map +1 -0
  109. package/packages/@repo/contracts/dist/domain.js +36 -0
  110. package/packages/@repo/contracts/dist/ids.d.ts +14 -0
  111. package/packages/@repo/contracts/dist/ids.d.ts.map +1 -0
  112. package/packages/@repo/contracts/dist/ids.js +10 -0
  113. package/packages/@repo/contracts/dist/index.d.ts +10 -0
  114. package/packages/@repo/contracts/dist/index.d.ts.map +1 -0
  115. package/packages/@repo/contracts/dist/index.js +11 -0
  116. package/packages/@repo/contracts/dist/pagination.d.ts +23 -0
  117. package/packages/@repo/contracts/dist/pagination.d.ts.map +1 -0
  118. package/packages/@repo/contracts/dist/pagination.js +15 -0
  119. package/packages/@repo/contracts/dist/project.d.ts +25 -0
  120. package/packages/@repo/contracts/dist/project.d.ts.map +1 -0
  121. package/packages/@repo/contracts/dist/project.js +23 -0
  122. package/packages/@repo/contracts/dist/tenant.d.ts +99 -0
  123. package/packages/@repo/contracts/dist/tenant.d.ts.map +1 -0
  124. package/packages/@repo/contracts/dist/tenant.js +36 -0
  125. package/packages/@repo/contracts/dist/user.d.ts +9 -0
  126. package/packages/@repo/contracts/dist/user.d.ts.map +1 -0
  127. package/packages/@repo/contracts/dist/user.js +9 -0
  128. package/packages/@repo/contracts/package.json +37 -0
  129. package/packages/@repo/contracts/src/api-key.ts +27 -0
  130. package/packages/@repo/contracts/src/dates.ts +4 -0
  131. package/packages/@repo/contracts/src/deployment.ts +73 -0
  132. package/packages/@repo/contracts/src/domain.ts +51 -0
  133. package/packages/@repo/contracts/src/ids.ts +22 -0
  134. package/packages/@repo/contracts/src/index.ts +11 -0
  135. package/packages/@repo/contracts/src/pagination.ts +21 -0
  136. package/packages/@repo/contracts/src/project.ts +30 -0
  137. package/packages/@repo/contracts/src/tenant.ts +54 -0
  138. package/packages/@repo/contracts/src/user.ts +12 -0
  139. package/packages/@repo/models/dist/docs-config.d.ts +985 -0
  140. package/packages/@repo/models/dist/docs-config.d.ts.map +1 -0
  141. package/packages/@repo/models/dist/docs-config.js +548 -0
  142. package/packages/@repo/models/dist/index.d.ts +3 -0
  143. package/packages/@repo/models/dist/index.d.ts.map +1 -0
  144. package/packages/@repo/models/dist/index.js +3 -0
  145. package/packages/@repo/models/dist/tenant.d.ts +25 -0
  146. package/packages/@repo/models/dist/tenant.d.ts.map +1 -0
  147. package/packages/@repo/models/dist/tenant.js +1 -0
  148. package/packages/@repo/models/package.json +37 -0
  149. package/packages/@repo/models/src/docs-config.ts +648 -0
  150. package/packages/@repo/models/src/index.ts +3 -0
  151. package/packages/@repo/models/src/tenant.ts +29 -0
  152. package/packages/@repo/prebuild/dist/index.d.ts +2 -0
  153. package/packages/@repo/prebuild/dist/index.d.ts.map +1 -0
  154. package/packages/@repo/prebuild/dist/index.js +2 -0
  155. package/packages/@repo/prebuild/dist/openapi.d.ts +43 -0
  156. package/packages/@repo/prebuild/dist/openapi.d.ts.map +1 -0
  157. package/packages/@repo/prebuild/dist/openapi.js +58 -0
  158. package/packages/@repo/prebuild/package.json +39 -0
  159. package/packages/@repo/prebuild/src/index.ts +2 -0
  160. package/packages/@repo/prebuild/src/openapi.ts +116 -0
  161. package/packages/@repo/previewing/dist/blob-source.d.ts +16 -0
  162. package/packages/@repo/previewing/dist/blob-source.d.ts.map +1 -0
  163. package/packages/@repo/previewing/dist/blob-source.js +110 -0
  164. package/packages/@repo/previewing/dist/content-source.d.ts +12 -0
  165. package/packages/@repo/previewing/dist/content-source.d.ts.map +1 -0
  166. package/packages/@repo/previewing/dist/content-source.js +1 -0
  167. package/packages/@repo/previewing/dist/fs-source.d.ts +11 -0
  168. package/packages/@repo/previewing/dist/fs-source.d.ts.map +1 -0
  169. package/packages/@repo/previewing/dist/fs-source.js +79 -0
  170. package/packages/@repo/previewing/dist/index.d.ts +120 -0
  171. package/packages/@repo/previewing/dist/index.d.ts.map +1 -0
  172. package/packages/@repo/previewing/dist/index.js +984 -0
  173. package/packages/@repo/previewing/package.json +41 -0
  174. package/packages/@repo/previewing/src/blob-source.ts +167 -0
  175. package/packages/@repo/previewing/src/content-source.ts +12 -0
  176. package/packages/@repo/previewing/src/fs-source.ts +111 -0
  177. package/packages/@repo/previewing/src/index.ts +1490 -0
  178. package/packages/@repo/previewing/src/index.unit.test.ts +290 -0
  179. package/packages/@repo/validation/dist/index.d.ts +12 -0
  180. package/packages/@repo/validation/dist/index.d.ts.map +1 -0
  181. package/packages/@repo/validation/dist/index.js +30 -0
  182. package/packages/@repo/validation/package.json +37 -0
  183. package/packages/@repo/validation/src/index.ts +59 -0
  184. package/packages/@repo/validation/src/mintlify-docs-schema.json +5016 -0
@@ -0,0 +1,1490 @@
1
+ import path from "node:path";
2
+
3
+ import { ensureArray, normalizePath, slugify } from "@repo/common";
4
+ import type {
5
+ CollectionConfig,
6
+ ContentType,
7
+ DocsOpenApiSource,
8
+ FrontmatterByType,
9
+ MintlifyDocsConfig,
10
+ PageMode,
11
+ SiteConfig,
12
+ } from "@repo/models";
13
+ import { PageModeSchema } from "@repo/models";
14
+ import {
15
+ extractOpenApiOperations,
16
+ openApiIdentifier,
17
+ openApiSlug,
18
+ parseOpenApiSpec,
19
+ } from "@repo/prebuild";
20
+ import type { OpenApiOperation, OpenApiSpec } from "@repo/prebuild";
21
+ import { validateDocsConfig, validateFrontmatter } from "@repo/validation";
22
+ import YAML from "yaml";
23
+
24
+ import type { ContentSource } from "./content-source.js";
25
+
26
+ export { BlobContentSource, createBlobSource } from "./blob-source.js";
27
+ export { createFsSource, FsContentSource } from "./fs-source.js";
28
+ export type { CompiledMdxResult, ContentSource } from "./content-source.js";
29
+
30
+ export const PREBUILT_INDEX_PATH = "_content-index.json";
31
+ export const PREBUILT_OPENAPI_INDEX_PATH = "_openapi-index.json";
32
+ export const PREBUILT_SEARCH_INDEX_PATH = "_search-index.json";
33
+ export const PREBUILT_TOC_INDEX_PATH = "_toc-index.json";
34
+ export const PREBUILT_UTILITY_INDEX_PATH = "_utility-index.json";
35
+ export const PREBUILT_UTILITY_SITEMAP_PATH = "_utility/sitemap.xml";
36
+ export const PREBUILT_UTILITY_LLMS_PATH = "_utility/llms.txt";
37
+ export const PREBUILT_UTILITY_LLMS_FULL_PATH = "_utility/llms-full.txt";
38
+ export const UTILITY_DOCS_ROOT_TOKEN = "__BLODEMD_DOCS_ROOT__";
39
+
40
+ export type SiteConfigResult =
41
+ | { ok: true; config: SiteConfig; warnings: string[] }
42
+ | { ok: false; errors: string[] };
43
+
44
+ export type ContentEntry =
45
+ | {
46
+ kind: "entry";
47
+ slug: string;
48
+ title: string;
49
+ description?: string;
50
+ hidden?: boolean;
51
+ type: ContentType;
52
+ collectionId: string;
53
+ sourcePath: string;
54
+ relativePath: string;
55
+ frontmatter: FrontmatterByType[ContentType];
56
+ }
57
+ | {
58
+ kind: "index";
59
+ slug: string;
60
+ title: string;
61
+ description?: string;
62
+ type: ContentType;
63
+ collectionId: string;
64
+ };
65
+
66
+ export interface ContentIndex {
67
+ entries: ContentEntry[];
68
+ bySlug: Map<string, ContentEntry>;
69
+ byCollection: Map<string, ContentEntry[]>;
70
+ errors: string[];
71
+ }
72
+
73
+ export interface PageMetadata {
74
+ title?: string;
75
+ sidebarTitle?: string;
76
+ icon?: string;
77
+ iconType?: string;
78
+ tag?: string;
79
+ hidden?: boolean;
80
+ deprecated?: boolean;
81
+ url?: string;
82
+ mode?: PageMode;
83
+ noindex?: boolean;
84
+ hideFooterPagination?: boolean;
85
+ hideApiMarker?: boolean;
86
+ keywords?: string[];
87
+ }
88
+
89
+ export interface SearchIndexItem {
90
+ href?: string;
91
+ title: string;
92
+ path: string;
93
+ }
94
+
95
+ export interface TocItem {
96
+ id: string;
97
+ level: number;
98
+ title: string;
99
+ }
100
+
101
+ export interface UtilityPage {
102
+ content: string;
103
+ description?: string;
104
+ slug: string;
105
+ title: string;
106
+ }
107
+
108
+ export interface UtilityIndex {
109
+ description?: string;
110
+ name: string;
111
+ pages: UtilityPage[];
112
+ }
113
+
114
+ export interface UtilityArtifact {
115
+ content: string;
116
+ contentType: string;
117
+ path: string;
118
+ }
119
+
120
+ export interface PrebuiltOpenApiEntry {
121
+ identifier: string;
122
+ operation: OpenApiOperation;
123
+ slug: string;
124
+ source: DocsOpenApiSource;
125
+ sourceKey: string;
126
+ spec: OpenApiSpec;
127
+ }
128
+
129
+ const validModes = new Set<string>(PageModeSchema.options);
130
+
131
+ export const buildPageMetadataMap = (
132
+ index: ContentIndex
133
+ ): Map<string, PageMetadata> => {
134
+ const map = new Map<string, PageMetadata>();
135
+ for (const entry of index.entries) {
136
+ if (entry.kind !== "entry") {
137
+ continue;
138
+ }
139
+ const fm = entry.frontmatter as Record<string, unknown>;
140
+ const meta: PageMetadata = {
141
+ title: entry.title,
142
+ };
143
+ const hasFields = true;
144
+ if (typeof fm.sidebarTitle === "string") {
145
+ meta.sidebarTitle = fm.sidebarTitle;
146
+ }
147
+ if (typeof fm.icon === "string") {
148
+ meta.icon = fm.icon;
149
+ }
150
+ if (typeof fm.iconType === "string") {
151
+ meta.iconType = fm.iconType;
152
+ }
153
+ if (typeof fm.tag === "string") {
154
+ meta.tag = fm.tag;
155
+ }
156
+ if (typeof fm.hidden === "boolean") {
157
+ meta.hidden = fm.hidden;
158
+ }
159
+ if (typeof fm.deprecated === "boolean") {
160
+ meta.deprecated = fm.deprecated;
161
+ }
162
+ if (typeof fm.url === "string") {
163
+ meta.url = fm.url;
164
+ }
165
+ if (typeof fm.mode === "string" && validModes.has(fm.mode)) {
166
+ meta.mode = fm.mode as PageMode;
167
+ }
168
+ if (typeof fm.noindex === "boolean") {
169
+ meta.noindex = fm.noindex;
170
+ }
171
+ if (typeof fm.hideFooterPagination === "boolean") {
172
+ meta.hideFooterPagination = fm.hideFooterPagination;
173
+ }
174
+ if (typeof fm.hideApiMarker === "boolean") {
175
+ meta.hideApiMarker = fm.hideApiMarker;
176
+ }
177
+ if (Array.isArray(fm.keywords)) {
178
+ meta.keywords = fm.keywords as string[];
179
+ }
180
+ if (hasFields) {
181
+ map.set(entry.slug, meta);
182
+ }
183
+ }
184
+ return map;
185
+ };
186
+
187
+ const DOCS_CONFIG_FILE = "docs.json";
188
+ const DOC_FILE_EXTENSION_REGEX = /\.(mdx|md)$/;
189
+ const FRONTMATTER_REGEX = /^---\s*\n([\s\S]*?)\n---\s*\n?/;
190
+ const INDEX_SUFFIX = "/index";
191
+
192
+ const titleFromSlug = (slug: string) => {
193
+ const clean = slug.replaceAll("-", " ").split("/").pop() ?? slug;
194
+ if (clean === "index") {
195
+ return "Overview";
196
+ }
197
+ return clean.replaceAll(/\b\w/g, (char) => char.toUpperCase());
198
+ };
199
+
200
+ const parseFrontmatter = (source: string) => {
201
+ const match = FRONTMATTER_REGEX.exec(source);
202
+ if (!match) {
203
+ return { body: source, frontmatter: {} };
204
+ }
205
+ const raw = match[1] ?? "";
206
+ const data = YAML.parse(raw) ?? {};
207
+ const body = source.slice(match[0].length);
208
+ return { body, frontmatter: data };
209
+ };
210
+
211
+ const slugFromFile = (relativePath: string) => {
212
+ const clean = normalizePath(relativePath);
213
+ const withoutExt = clean.replace(DOC_FILE_EXTENSION_REGEX, "");
214
+ if (withoutExt.endsWith(INDEX_SUFFIX)) {
215
+ const trimmed = withoutExt.slice(0, -INDEX_SUFFIX.length);
216
+ return trimmed.length ? trimmed : "index";
217
+ }
218
+ return withoutExt.length ? withoutExt : "index";
219
+ };
220
+
221
+ const defaultLinkLabel = (input: {
222
+ href: string;
223
+ label?: string;
224
+ type?: "discord" | "github";
225
+ }) => {
226
+ if (input.label) {
227
+ return input.label;
228
+ }
229
+ if (input.type === "github") {
230
+ return "GitHub";
231
+ }
232
+ if (input.type === "discord") {
233
+ return "Discord";
234
+ }
235
+ try {
236
+ return new URL(input.href).hostname;
237
+ } catch {
238
+ return input.href;
239
+ }
240
+ };
241
+
242
+ const buildGoogleFontsCssUrl = (
243
+ fonts: MintlifyDocsConfig["fonts"]
244
+ ): string | undefined => {
245
+ if (!fonts) {
246
+ return undefined;
247
+ }
248
+
249
+ const fontEntries: { family: string; source?: string }[] = [];
250
+ if (fonts.family) {
251
+ fontEntries.push({ family: fonts.family, source: fonts.source });
252
+ }
253
+ if (fonts.body) {
254
+ fontEntries.push({
255
+ family: fonts.body.family,
256
+ source: fonts.body.source,
257
+ });
258
+ }
259
+ if (fonts.heading) {
260
+ fontEntries.push({
261
+ family: fonts.heading.family,
262
+ source: fonts.heading.source,
263
+ });
264
+ }
265
+
266
+ const googleFamilies = [
267
+ ...new Set(
268
+ fontEntries.filter((entry) => !entry.source).map((entry) => entry.family)
269
+ ),
270
+ ];
271
+ if (!googleFamilies.length) {
272
+ return undefined;
273
+ }
274
+
275
+ const params = googleFamilies.map(
276
+ (family) => `family=${encodeURIComponent(family).replaceAll("%20", "+")}`
277
+ );
278
+ return `https://fonts.googleapis.com/css2?${params.join("&")}&display=swap`;
279
+ };
280
+
281
+ // oxlint-disable-next-line eslint/complexity
282
+ const mapDocsConfig = (docs: MintlifyDocsConfig): SiteConfig => {
283
+ const navigation = {
284
+ global:
285
+ docs.navbar?.links?.length || docs.navigation.global?.anchors?.length
286
+ ? {
287
+ anchors: docs.navigation.global?.anchors?.map((anchor) => ({
288
+ href: anchor.href,
289
+ label: anchor.anchor,
290
+ })),
291
+ links: docs.navbar?.links?.map((link) => ({
292
+ href: link.href,
293
+ label: defaultLinkLabel(link),
294
+ })),
295
+ }
296
+ : undefined,
297
+ groups: docs.navigation.groups?.map((group) => ({
298
+ expanded: group.expanded,
299
+ group: group.group,
300
+ hidden: group.hidden,
301
+ pages: group.root
302
+ ? [
303
+ group.root,
304
+ ...(group.pages ?? []).filter((page) => page !== group.root),
305
+ ]
306
+ : group.pages,
307
+ })),
308
+ languages: docs.navigation.languages?.map((language) => ({
309
+ label: language.language,
310
+ locale: language.language,
311
+ url: language.href,
312
+ })),
313
+ pages: docs.navigation.pages,
314
+ tabs: docs.navigation.tabs?.map((tab) => ({
315
+ groups: tab.groups?.map((group) => ({
316
+ expanded: group.expanded,
317
+ group: group.group,
318
+ hidden: group.hidden,
319
+ pages: group.root
320
+ ? [
321
+ group.root,
322
+ ...(group.pages ?? []).filter((page) => page !== group.root),
323
+ ]
324
+ : group.pages,
325
+ })),
326
+ href: tab.href,
327
+ icon: tab.icon,
328
+ label: tab.tab,
329
+ pages: tab.pages,
330
+ })),
331
+ versions: docs.navigation.versions?.map((version) => ({
332
+ label: version.version,
333
+ url: version.href,
334
+ })),
335
+ } satisfies SiteConfig["navigation"];
336
+
337
+ const baseFontFamily = docs.fonts?.family;
338
+ const fonts =
339
+ docs.fonts && (baseFontFamily || docs.fonts.body || docs.fonts.heading)
340
+ ? {
341
+ body: docs.fonts.body?.family ?? baseFontFamily,
342
+ cssUrl: buildGoogleFontsCssUrl(docs.fonts),
343
+ heading: docs.fonts.heading?.family ?? baseFontFamily,
344
+ provider: "google" as const,
345
+ }
346
+ : undefined;
347
+
348
+ return {
349
+ collections: [
350
+ {
351
+ id: "docs",
352
+ navigation,
353
+ openapi: docs.api?.openapi,
354
+ root: "",
355
+ type: "docs",
356
+ },
357
+ ],
358
+ colors: docs.colors,
359
+ contextual: docs.contextual,
360
+ description: docs.description,
361
+ favicon:
362
+ typeof docs.favicon === "string" ? docs.favicon : docs.favicon?.light,
363
+ features: {
364
+ rightToc: true,
365
+ search: true,
366
+ themeToggle: docs.appearance?.strict !== true,
367
+ toc: true,
368
+ },
369
+ fonts,
370
+ logo: docs.logo
371
+ ? {
372
+ dark: typeof docs.logo === "string" ? docs.logo : docs.logo.dark,
373
+ light: typeof docs.logo === "string" ? docs.logo : docs.logo.light,
374
+ }
375
+ : undefined,
376
+ name: docs.name,
377
+ navigation,
378
+ openapiProxy: {
379
+ enabled:
380
+ docs.api?.playground?.proxy !== false &&
381
+ Boolean(docs.api?.openapi || docs.api?.asyncapi),
382
+ },
383
+ seo: docs.seo,
384
+ theme: docs.theme,
385
+ };
386
+ };
387
+
388
+ const readJsonConfig = async (source: ContentSource, relativePath: string) =>
389
+ JSON.parse(await source.readFile(relativePath)) as unknown;
390
+
391
+ const normalizeRefPath = (baseDirectory: string, reference: string) => {
392
+ if (
393
+ reference.startsWith("/") ||
394
+ reference.startsWith("\\") ||
395
+ reference.startsWith("http://") ||
396
+ reference.startsWith("https://")
397
+ ) {
398
+ throw new Error(
399
+ `Invalid $ref "${reference}". Only relative JSON files are supported.`
400
+ );
401
+ }
402
+
403
+ const normalized = normalizePath(path.posix.join(baseDirectory, reference));
404
+ if (
405
+ !normalized ||
406
+ normalized === "." ||
407
+ normalized.startsWith("../") ||
408
+ normalized.includes("/../")
409
+ ) {
410
+ throw new Error(`Invalid $ref "${reference}".`);
411
+ }
412
+ return normalized;
413
+ };
414
+
415
+ const resolveJsonRefs = async (
416
+ source: ContentSource,
417
+ value: unknown,
418
+ baseDirectory: string,
419
+ seen: Set<string>
420
+ ): Promise<unknown> => {
421
+ if (Array.isArray(value)) {
422
+ return await Promise.all(
423
+ value.map((item) => resolveJsonRefs(source, item, baseDirectory, seen))
424
+ );
425
+ }
426
+
427
+ if (!value || typeof value !== "object") {
428
+ return value;
429
+ }
430
+
431
+ const record = value as Record<string, unknown>;
432
+ const reference = record.$ref;
433
+ if (typeof reference === "string") {
434
+ const resolvedPath = normalizeRefPath(baseDirectory, reference);
435
+ if (seen.has(resolvedPath)) {
436
+ throw new Error(`Circular $ref detected for "${resolvedPath}".`);
437
+ }
438
+
439
+ const nextSeen = new Set(seen);
440
+ // oxlint-disable-next-line eslint-plugin-unicorn/no-immediate-mutation
441
+ nextSeen.add(resolvedPath);
442
+ const referenced = await readJsonConfig(source, resolvedPath);
443
+ const referencedValue = await resolveJsonRefs(
444
+ source,
445
+ referenced,
446
+ path.posix.dirname(resolvedPath) === "."
447
+ ? ""
448
+ : normalizePath(path.posix.dirname(resolvedPath)),
449
+ nextSeen
450
+ );
451
+
452
+ const siblingEntries = Object.entries(record).filter(
453
+ ([key]) => key !== "$ref"
454
+ );
455
+ if (
456
+ !siblingEntries.length ||
457
+ !referencedValue ||
458
+ typeof referencedValue !== "object" ||
459
+ Array.isArray(referencedValue)
460
+ ) {
461
+ return referencedValue;
462
+ }
463
+
464
+ const siblingValue = await resolveJsonRefs(
465
+ source,
466
+ Object.fromEntries(siblingEntries),
467
+ baseDirectory,
468
+ seen
469
+ );
470
+ return {
471
+ ...(referencedValue as Record<string, unknown>),
472
+ ...(siblingValue as Record<string, unknown>),
473
+ };
474
+ }
475
+
476
+ const resolvedEntries = await Promise.all(
477
+ Object.entries(record).map(async ([key, entryValue]) => [
478
+ key,
479
+ await resolveJsonRefs(source, entryValue, baseDirectory, seen),
480
+ ])
481
+ );
482
+ return Object.fromEntries(resolvedEntries);
483
+ };
484
+
485
+ const readResolvedJsonConfig = async (
486
+ source: ContentSource,
487
+ relativePath: string
488
+ ) =>
489
+ await resolveJsonRefs(
490
+ source,
491
+ await readJsonConfig(source, relativePath),
492
+ path.posix.dirname(relativePath) === "."
493
+ ? ""
494
+ : normalizePath(path.posix.dirname(relativePath)),
495
+ new Set([relativePath])
496
+ );
497
+
498
+ const loadDocsConfig = async (
499
+ source: ContentSource
500
+ ): Promise<SiteConfigResult | null> => {
501
+ if (!(await source.exists(DOCS_CONFIG_FILE))) {
502
+ return null;
503
+ }
504
+
505
+ try {
506
+ const parsed = await readResolvedJsonConfig(source, DOCS_CONFIG_FILE);
507
+ const result = validateDocsConfig(parsed);
508
+ if (!result.success) {
509
+ return { errors: result.errors, ok: false };
510
+ }
511
+ return {
512
+ config: mapDocsConfig(result.data),
513
+ ok: true,
514
+ warnings: [],
515
+ };
516
+ } catch (error) {
517
+ return {
518
+ errors: [
519
+ error instanceof Error
520
+ ? error.message
521
+ : `Failed to load ${DOCS_CONFIG_FILE}`,
522
+ ],
523
+ ok: false,
524
+ };
525
+ }
526
+ };
527
+
528
+ export const loadSiteConfig = async (
529
+ source: ContentSource
530
+ ): Promise<SiteConfigResult> => {
531
+ const docsConfig = await loadDocsConfig(source);
532
+ if (docsConfig) {
533
+ return docsConfig;
534
+ }
535
+
536
+ return {
537
+ errors: [`${DOCS_CONFIG_FILE} not found.`],
538
+ ok: false,
539
+ };
540
+ };
541
+
542
+ export const loadContentSource = async (
543
+ source: ContentSource,
544
+ relativePath: string
545
+ ) => await source.readFile(relativePath);
546
+
547
+ const listContentFiles = async (source: ContentSource, directory: string) => {
548
+ const files = await source.listFiles(directory);
549
+ return files.filter((file: string) => DOC_FILE_EXTENSION_REGEX.test(file));
550
+ };
551
+
552
+ const sortDefaults: Record<
553
+ ContentType,
554
+ { field: "date" | "order" | "title" | "price"; direction: "asc" | "desc" }
555
+ > = {
556
+ blog: { direction: "desc", field: "date" },
557
+ courses: { direction: "asc", field: "order" },
558
+ docs: { direction: "asc", field: "title" },
559
+ forms: { direction: "asc", field: "title" },
560
+ notes: { direction: "desc", field: "date" },
561
+ products: { direction: "asc", field: "title" },
562
+ sheets: { direction: "asc", field: "title" },
563
+ site: { direction: "asc", field: "title" },
564
+ slides: { direction: "asc", field: "title" },
565
+ todos: { direction: "desc", field: "date" },
566
+ };
567
+
568
+ const normalizeSortValue = (value: unknown) => {
569
+ if (typeof value === "number") {
570
+ return value;
571
+ }
572
+ if (typeof value === "string") {
573
+ const timestamp = Date.parse(value);
574
+ if (!Number.isNaN(timestamp)) {
575
+ return timestamp;
576
+ }
577
+ return value.toLowerCase();
578
+ }
579
+ return null;
580
+ };
581
+
582
+ const compareValues = (a: unknown, b: unknown, direction: "asc" | "desc") => {
583
+ const left = normalizeSortValue(a);
584
+ const right = normalizeSortValue(b);
585
+ if (left === null && right === null) {
586
+ return 0;
587
+ }
588
+ if (left === null) {
589
+ return 1;
590
+ }
591
+ if (right === null) {
592
+ return -1;
593
+ }
594
+ const multiplier = direction === "desc" ? -1 : 1;
595
+ if (typeof left === "number" && typeof right === "number") {
596
+ return (left - right) * multiplier;
597
+ }
598
+ if (left < right) {
599
+ return -1 * multiplier;
600
+ }
601
+ if (left > right) {
602
+ return 1 * multiplier;
603
+ }
604
+ return 0;
605
+ };
606
+
607
+ const autoIndexTypes = new Set<ContentType>([
608
+ "blog",
609
+ "courses",
610
+ "products",
611
+ "notes",
612
+ "forms",
613
+ "sheets",
614
+ "slides",
615
+ "todos",
616
+ ]);
617
+
618
+ const getCollectionIndex = (
619
+ collection: CollectionConfig,
620
+ slugPrefix: string
621
+ ) => {
622
+ if (collection.index) {
623
+ return collection.index;
624
+ }
625
+ if (autoIndexTypes.has(collection.type)) {
626
+ const slug = slugPrefix || collection.id;
627
+ return {
628
+ slug,
629
+ title: titleFromSlug(slug),
630
+ };
631
+ }
632
+ return null;
633
+ };
634
+
635
+ const addEntry = (
636
+ entry: ContentEntry,
637
+ index: ContentIndex,
638
+ errors: string[]
639
+ ) => {
640
+ if (index.bySlug.has(entry.slug)) {
641
+ errors.push(`slug "${entry.slug}" is defined more than once`);
642
+ return;
643
+ }
644
+ index.entries.push(entry);
645
+ index.bySlug.set(entry.slug, entry);
646
+ };
647
+
648
+ const resolveEntrySlug = (relativeSlug: string, slugPrefix: string) => {
649
+ if (!slugPrefix) {
650
+ return relativeSlug;
651
+ }
652
+
653
+ if (relativeSlug === "index") {
654
+ return slugPrefix;
655
+ }
656
+
657
+ return normalizePath(`${slugPrefix}/${relativeSlug}`);
658
+ };
659
+
660
+ const buildEntryFromFile = async ({
661
+ collection,
662
+ errors,
663
+ file,
664
+ root,
665
+ slugPrefix,
666
+ source,
667
+ }: {
668
+ collection: CollectionConfig;
669
+ errors: string[];
670
+ file: string;
671
+ root: string;
672
+ slugPrefix: string;
673
+ source: ContentSource;
674
+ }): Promise<Extract<ContentEntry, { kind: "entry" }> | null> => {
675
+ const sourcePath = root
676
+ ? normalizePath(path.join(root, file))
677
+ : normalizePath(file);
678
+
679
+ let entrySource = "";
680
+ try {
681
+ entrySource = await source.readFile(sourcePath);
682
+ } catch (error) {
683
+ errors.push(
684
+ error instanceof Error ? error.message : `Failed to read ${sourcePath}`
685
+ );
686
+ return null;
687
+ }
688
+
689
+ const { frontmatter } = parseFrontmatter(entrySource);
690
+ const frontmatterResult = validateFrontmatter(collection.type, frontmatter);
691
+ if (!frontmatterResult.success) {
692
+ for (const issue of frontmatterResult.errors) {
693
+ errors.push(`${sourcePath}: ${issue}`);
694
+ }
695
+ }
696
+
697
+ const resolvedFrontmatter = frontmatterResult.success
698
+ ? frontmatterResult.data
699
+ : (frontmatter as FrontmatterByType[ContentType]);
700
+ const relativeSlug = slugFromFile(file);
701
+ const slug = resolveEntrySlug(relativeSlug, slugPrefix);
702
+ const title =
703
+ typeof resolvedFrontmatter?.title === "string"
704
+ ? resolvedFrontmatter.title
705
+ : titleFromSlug(slug);
706
+ const description =
707
+ typeof resolvedFrontmatter?.description === "string"
708
+ ? resolvedFrontmatter.description
709
+ : undefined;
710
+
711
+ const hidden =
712
+ typeof resolvedFrontmatter?.hidden === "boolean"
713
+ ? resolvedFrontmatter.hidden
714
+ : undefined;
715
+
716
+ return {
717
+ collectionId: collection.id,
718
+ description,
719
+ frontmatter: resolvedFrontmatter,
720
+ hidden: hidden || undefined,
721
+ kind: "entry",
722
+ relativePath: sourcePath,
723
+ slug,
724
+ sourcePath,
725
+ title,
726
+ type: collection.type,
727
+ };
728
+ };
729
+
730
+ export const buildContentIndex = async (
731
+ source: ContentSource,
732
+ config: SiteConfig
733
+ ): Promise<ContentIndex> => {
734
+ const errors: string[] = [];
735
+ const index: ContentIndex = {
736
+ byCollection: new Map<string, ContentEntry[]>(),
737
+ bySlug: new Map<string, ContentEntry>(),
738
+ entries: [],
739
+ errors,
740
+ };
741
+
742
+ for (const collection of config.collections) {
743
+ const root = normalizePath(collection.root ?? "");
744
+ const slugPrefix = normalizePath(collection.slugPrefix ?? "");
745
+ let files: string[] = [];
746
+ try {
747
+ files = await listContentFiles(source, root);
748
+ } catch (error) {
749
+ errors.push(
750
+ error instanceof Error ? error.message : `Failed to read ${root || "."}`
751
+ );
752
+ continue;
753
+ }
754
+
755
+ const collectionEntries: ContentEntry[] = [];
756
+
757
+ const resolvedEntries = await Promise.all(
758
+ files.map(
759
+ async (file) =>
760
+ await buildEntryFromFile({
761
+ collection,
762
+ errors,
763
+ file,
764
+ root,
765
+ slugPrefix,
766
+ source,
767
+ })
768
+ )
769
+ );
770
+
771
+ for (const entry of resolvedEntries) {
772
+ if (!entry) {
773
+ continue;
774
+ }
775
+
776
+ collectionEntries.push(entry);
777
+ addEntry(entry, index, errors);
778
+ }
779
+
780
+ const sortConfig = {
781
+ ...sortDefaults[collection.type],
782
+ ...collection.sort,
783
+ };
784
+ const sortField = sortConfig.field ?? "title";
785
+ const sortDirection = sortConfig.direction ?? "asc";
786
+ collectionEntries.sort((left, right) => {
787
+ const leftValue =
788
+ left.kind === "entry"
789
+ ? (left.frontmatter as Record<string, unknown>)[sortField]
790
+ : undefined;
791
+ const rightValue =
792
+ right.kind === "entry"
793
+ ? (right.frontmatter as Record<string, unknown>)[sortField]
794
+ : undefined;
795
+ return compareValues(leftValue, rightValue, sortDirection);
796
+ });
797
+
798
+ index.byCollection.set(collection.id, collectionEntries);
799
+
800
+ const collectionIndex = getCollectionIndex(collection, slugPrefix);
801
+ if (collectionIndex) {
802
+ const indexEntry: ContentEntry = {
803
+ collectionId: collection.id,
804
+ description: collectionIndex.description,
805
+ kind: "index",
806
+ slug: collectionIndex.slug,
807
+ title: collectionIndex.title ?? titleFromSlug(collectionIndex.slug),
808
+ type: collection.type,
809
+ };
810
+ addEntry(indexEntry, index, errors);
811
+ }
812
+ }
813
+
814
+ return index;
815
+ };
816
+
817
+ interface SerializedContentIndex {
818
+ version: 1;
819
+ entries: ContentEntry[];
820
+ collections: Record<string, ContentEntry[]>;
821
+ }
822
+
823
+ interface SerializedOpenApiIndex {
824
+ entries: PrebuiltOpenApiEntry[];
825
+ version: 1;
826
+ }
827
+
828
+ interface SerializedSearchIndex {
829
+ items: SearchIndexItem[];
830
+ version: 1;
831
+ }
832
+
833
+ interface SerializedTocIndex {
834
+ itemsBySlug: Record<string, TocItem[]>;
835
+ version: 1;
836
+ }
837
+
838
+ interface SerializedUtilityIndex {
839
+ description?: string;
840
+ name: string;
841
+ pages: UtilityPage[];
842
+ version: 1;
843
+ }
844
+
845
+ interface UtilityOpenApiPage extends UtilityPage {
846
+ identifier: string;
847
+ sourceKey: string;
848
+ }
849
+
850
+ const NEWLINE_REGEX = /\r?\n/;
851
+ const HEADING_REGEX = /^(#{2,4})\s+(.*)$/;
852
+ const LEADING_H1_REGEX = /^#\s+([^\r\n]+)(?:\r?\n(?:\r?\n)?)?/;
853
+
854
+ export const extractToc = (source: string): TocItem[] => {
855
+ const withoutCode = source.replaceAll(/```[\s\S]*?```/g, "");
856
+ const lines = withoutCode.split(NEWLINE_REGEX);
857
+ const toc: TocItem[] = [];
858
+
859
+ for (const line of lines) {
860
+ const match = HEADING_REGEX.exec(line.trim());
861
+ if (!match) {
862
+ continue;
863
+ }
864
+
865
+ const [, hashes = "", heading = ""] = match;
866
+ if (!(hashes && heading)) {
867
+ continue;
868
+ }
869
+
870
+ toc.push({
871
+ id: slugify(heading.trim()),
872
+ level: hashes.length,
873
+ title: heading.trim(),
874
+ });
875
+ }
876
+
877
+ return toc;
878
+ };
879
+
880
+ const stripMatchingLeadingH1 = (source: string, title: string) => {
881
+ const trimmed = source.trimStart();
882
+ const match = LEADING_H1_REGEX.exec(trimmed);
883
+ if (!match) {
884
+ return trimmed.trim();
885
+ }
886
+
887
+ const [headingLine = "", headingTitle = ""] = match;
888
+ if (slugify(headingTitle) !== slugify(title)) {
889
+ return trimmed.trim();
890
+ }
891
+
892
+ return trimmed.slice(headingLine.length).trim();
893
+ };
894
+
895
+ export const formatMarkdownPage = (title: string, source: string) => {
896
+ const content = stripMatchingLeadingH1(source, title);
897
+ if (!content) {
898
+ return `# ${title}`;
899
+ }
900
+
901
+ return `# ${title}\n\n${content}`;
902
+ };
903
+
904
+ export const formatMarkdownPageSection = (
905
+ title: string,
906
+ url: string,
907
+ source: string
908
+ ) => {
909
+ const content = stripMatchingLeadingH1(source, title);
910
+ if (!content) {
911
+ return `# ${title} (${url})`;
912
+ }
913
+
914
+ return `# ${title} (${url})\n\n${content}`;
915
+ };
916
+
917
+ const shouldIncludeSearchEntry = (
918
+ entry: ContentEntry,
919
+ pageMetadataMap: Map<string, PageMetadata>,
920
+ config: SiteConfig
921
+ ) => {
922
+ const pageMeta = pageMetadataMap.get(entry.slug);
923
+
924
+ if (pageMeta?.hidden || pageMeta?.noindex) {
925
+ return false;
926
+ }
927
+
928
+ if (
929
+ config.seo?.indexing !== "all" &&
930
+ entry.kind === "entry" &&
931
+ entry.hidden === true
932
+ ) {
933
+ return false;
934
+ }
935
+
936
+ return true;
937
+ };
938
+
939
+ const stripFrontmatter = (source: string) =>
940
+ parseFrontmatter(source).body.trim();
941
+
942
+ const getDocsCollection = (config: SiteConfig) =>
943
+ config.collections.find((collection) => collection.type === "docs");
944
+
945
+ const getDocsNavigation = (config: SiteConfig) =>
946
+ getDocsCollection(config)?.navigation ?? config.navigation;
947
+
948
+ const getDocsCollectionWithNavigation = (
949
+ config: SiteConfig
950
+ ): SiteConfig["collections"][number] | undefined => {
951
+ const docsCollection = getDocsCollection(config);
952
+ const docsNavigation = getDocsNavigation(config);
953
+
954
+ return docsCollection &&
955
+ docsNavigation &&
956
+ docsCollection.navigation !== docsNavigation
957
+ ? { ...docsCollection, navigation: docsNavigation }
958
+ : docsCollection;
959
+ };
960
+
961
+ const getOpenApiSourceKey = (source: DocsOpenApiSource): string =>
962
+ `${source.source}::${source.directory ?? ""}::${(source.include ?? []).join(
963
+ "|"
964
+ )}`;
965
+
966
+ const toOpenApiSourceObject = (
967
+ value: string | DocsOpenApiSource
968
+ ): DocsOpenApiSource => {
969
+ if (typeof value === "string") {
970
+ return { source: value };
971
+ }
972
+ return value;
973
+ };
974
+
975
+ const collectOpenApiSources = (collection?: CollectionConfig) => {
976
+ const sources: DocsOpenApiSource[] = [];
977
+
978
+ for (const item of ensureArray(collection?.openapi)) {
979
+ if (!item) {
980
+ continue;
981
+ }
982
+ sources.push(toOpenApiSourceObject(item));
983
+ }
984
+
985
+ const groups = collection?.navigation?.groups ?? [];
986
+ for (const group of groups) {
987
+ if (!group.openapi) {
988
+ continue;
989
+ }
990
+ sources.push(toOpenApiSourceObject(group.openapi));
991
+ }
992
+
993
+ const seen = new Set<string>();
994
+ return sources.filter((source) => {
995
+ const key = getOpenApiSourceKey(source);
996
+ if (seen.has(key)) {
997
+ return false;
998
+ }
999
+ seen.add(key);
1000
+ return true;
1001
+ });
1002
+ };
1003
+
1004
+ const formatOpenApiPageContent = (operation: OpenApiOperation): string => {
1005
+ const parts = [`Method: ${operation.method}`, `Path: ${operation.path}`];
1006
+
1007
+ if (operation.description) {
1008
+ parts.push(operation.description);
1009
+ }
1010
+ if (operation.tags.length) {
1011
+ parts.push(`Tags: ${operation.tags.join(", ")}`);
1012
+ }
1013
+ if (operation.parameters.length) {
1014
+ parts.push(`Parameters:\n${JSON.stringify(operation.parameters, null, 2)}`);
1015
+ }
1016
+ if (operation.requestBody) {
1017
+ parts.push(
1018
+ `Request Body:\n${JSON.stringify(operation.requestBody, null, 2)}`
1019
+ );
1020
+ }
1021
+ if (operation.responses) {
1022
+ parts.push(`Responses:\n${JSON.stringify(operation.responses, null, 2)}`);
1023
+ }
1024
+
1025
+ return parts.join("\n\n");
1026
+ };
1027
+
1028
+ const getGroupedOpenApiSourceKey = (
1029
+ source: string | DocsOpenApiSource
1030
+ ): string => getOpenApiSourceKey(toOpenApiSourceObject(source));
1031
+
1032
+ const collectUtilityOpenApiPages = (
1033
+ pagesByIdentifier: Map<string, UtilityOpenApiPage>,
1034
+ pagesBySource: Map<string, UtilityOpenApiPage[]>,
1035
+ operations: OpenApiOperation[],
1036
+ directory: string,
1037
+ openApiSource: DocsOpenApiSource,
1038
+ slugPrefix: string
1039
+ ) => {
1040
+ const sourceKey = getOpenApiSourceKey(openApiSource);
1041
+ const includeIdentifiers = openApiSource.include?.length
1042
+ ? new Set(openApiSource.include)
1043
+ : null;
1044
+
1045
+ for (const operation of operations) {
1046
+ const identifier = openApiIdentifier(operation.method, operation.path);
1047
+ if (includeIdentifiers && !includeIdentifiers.has(identifier)) {
1048
+ continue;
1049
+ }
1050
+
1051
+ const baseSlug = normalizePath(
1052
+ openApiSlug(operation.method, operation.path, directory)
1053
+ );
1054
+ const slug = slugPrefix
1055
+ ? normalizePath(`${slugPrefix}/${baseSlug}`)
1056
+ : baseSlug;
1057
+ const page = {
1058
+ content: formatOpenApiPageContent(operation),
1059
+ description: operation.description,
1060
+ identifier,
1061
+ slug,
1062
+ sourceKey,
1063
+ title: operation.summary ?? identifier,
1064
+ } satisfies UtilityOpenApiPage;
1065
+
1066
+ pagesByIdentifier.set(identifier, page);
1067
+ if (!pagesBySource.has(sourceKey)) {
1068
+ pagesBySource.set(sourceKey, []);
1069
+ }
1070
+ pagesBySource.get(sourceKey)?.push(page);
1071
+ }
1072
+ };
1073
+
1074
+ const addUtilityPagesFromSourceKey = (
1075
+ pages: Map<string, UtilityPage>,
1076
+ pagesBySource: Map<string, UtilityOpenApiPage[]>,
1077
+ sourceKey: string
1078
+ ) => {
1079
+ for (const page of pagesBySource.get(sourceKey) ?? []) {
1080
+ pages.set(page.slug, page);
1081
+ }
1082
+ };
1083
+
1084
+ const addReferencedUtilityPages = (
1085
+ pages: Map<string, UtilityPage>,
1086
+ pagesByIdentifier: Map<string, UtilityOpenApiPage>,
1087
+ pageReferences: string[] | undefined,
1088
+ hiddenPages: Set<string>,
1089
+ groupHidden = false
1090
+ ) => {
1091
+ for (const pageReference of pageReferences ?? []) {
1092
+ if (groupHidden || hiddenPages.has(pageReference)) {
1093
+ continue;
1094
+ }
1095
+
1096
+ const page = pagesByIdentifier.get(pageReference);
1097
+ if (page) {
1098
+ pages.set(page.slug, page);
1099
+ }
1100
+ }
1101
+ };
1102
+
1103
+ const buildUtilityOpenApiPages = async (
1104
+ config: SiteConfig,
1105
+ collection: CollectionConfig | undefined,
1106
+ source: ContentSource
1107
+ ) => {
1108
+ if (!collection || collection.type !== "docs") {
1109
+ return [] satisfies UtilityPage[];
1110
+ }
1111
+
1112
+ const docsNavigation = getDocsNavigation(config);
1113
+ const hiddenPages = new Set(docsNavigation?.hidden);
1114
+ const slugPrefix = normalizePath(collection.slugPrefix ?? "");
1115
+ const byIdentifier = new Map<string, UtilityOpenApiPage>();
1116
+ const bySource = new Map<string, UtilityOpenApiPage[]>();
1117
+ const pages = new Map<string, UtilityPage>();
1118
+ const sources = collectOpenApiSources(collection);
1119
+
1120
+ const resolved = await Promise.all(
1121
+ sources.map(async (item) => {
1122
+ const rawSpec = await source.readFile(item.source);
1123
+ const spec = parseOpenApiSpec(rawSpec, item.source);
1124
+ const directory = item.directory ?? "api";
1125
+ const { operations } = extractOpenApiOperations(spec, directory);
1126
+ return { directory, operations, source: item };
1127
+ })
1128
+ );
1129
+
1130
+ for (const { directory, operations, source: openApiSource } of resolved) {
1131
+ collectUtilityOpenApiPages(
1132
+ byIdentifier,
1133
+ bySource,
1134
+ operations,
1135
+ directory,
1136
+ openApiSource,
1137
+ slugPrefix
1138
+ );
1139
+ }
1140
+
1141
+ for (const openApiSource of ensureArray(collection.openapi)) {
1142
+ if (!openApiSource) {
1143
+ continue;
1144
+ }
1145
+ addUtilityPagesFromSourceKey(
1146
+ pages,
1147
+ bySource,
1148
+ getGroupedOpenApiSourceKey(openApiSource)
1149
+ );
1150
+ }
1151
+
1152
+ for (const group of docsNavigation?.groups ?? []) {
1153
+ const groupHidden = group.hidden === true;
1154
+ addReferencedUtilityPages(
1155
+ pages,
1156
+ byIdentifier,
1157
+ group.pages,
1158
+ hiddenPages,
1159
+ groupHidden
1160
+ );
1161
+
1162
+ if (groupHidden || !group.openapi) {
1163
+ continue;
1164
+ }
1165
+ addUtilityPagesFromSourceKey(
1166
+ pages,
1167
+ bySource,
1168
+ getGroupedOpenApiSourceKey(group.openapi)
1169
+ );
1170
+ }
1171
+
1172
+ addReferencedUtilityPages(
1173
+ pages,
1174
+ byIdentifier,
1175
+ docsNavigation?.pages,
1176
+ hiddenPages
1177
+ );
1178
+
1179
+ return [...pages.values()];
1180
+ };
1181
+
1182
+ export const buildSearchIndex = (
1183
+ index: ContentIndex,
1184
+ config: SiteConfig,
1185
+ utilityIndex?: UtilityIndex
1186
+ ): SearchIndexItem[] => {
1187
+ const pageMetadataMap = buildPageMetadataMap(index);
1188
+ const items = new Map<string, SearchIndexItem>();
1189
+
1190
+ for (const page of utilityIndex?.pages ?? []) {
1191
+ items.set(page.slug, {
1192
+ path: page.slug,
1193
+ title: page.title,
1194
+ });
1195
+ }
1196
+
1197
+ for (const entry of index.entries) {
1198
+ if (!shouldIncludeSearchEntry(entry, pageMetadataMap, config)) {
1199
+ continue;
1200
+ }
1201
+
1202
+ const pageMeta = pageMetadataMap.get(entry.slug);
1203
+ items.set(entry.slug, {
1204
+ href: pageMeta?.url,
1205
+ path: entry.slug,
1206
+ title: pageMeta?.sidebarTitle ?? entry.title,
1207
+ });
1208
+ }
1209
+
1210
+ return [...items.values()];
1211
+ };
1212
+
1213
+ export const buildUtilityIndex = async (
1214
+ index: ContentIndex,
1215
+ source: ContentSource,
1216
+ config: SiteConfig
1217
+ ): Promise<UtilityIndex> => {
1218
+ const pageMetadataMap = buildPageMetadataMap(index);
1219
+ const pages = new Map<string, UtilityPage>();
1220
+
1221
+ for (const entry of index.entries) {
1222
+ if (entry.kind !== "entry") {
1223
+ continue;
1224
+ }
1225
+
1226
+ if (!shouldIncludeSearchEntry(entry, pageMetadataMap, config)) {
1227
+ continue;
1228
+ }
1229
+
1230
+ const rawContent = await source.readFile(entry.relativePath);
1231
+ pages.set(entry.slug, {
1232
+ content: stripFrontmatter(rawContent),
1233
+ description: entry.description,
1234
+ slug: entry.slug,
1235
+ title: entry.title,
1236
+ });
1237
+ }
1238
+
1239
+ for (const page of await buildUtilityOpenApiPages(
1240
+ config,
1241
+ getDocsCollectionWithNavigation(config),
1242
+ source
1243
+ )) {
1244
+ pages.set(page.slug, page);
1245
+ }
1246
+
1247
+ const sortedPages = [...pages.values()];
1248
+ // oxlint-disable-next-line eslint-plugin-unicorn/no-array-sort
1249
+ sortedPages.sort((left, right) => left.slug.localeCompare(right.slug));
1250
+
1251
+ return {
1252
+ description: config.description,
1253
+ name: config.name,
1254
+ pages: sortedPages,
1255
+ };
1256
+ };
1257
+
1258
+ const toUtilityDocPath = (value: string) => {
1259
+ const clean = normalizePath(value);
1260
+ if (!clean || clean === "index") {
1261
+ return "/";
1262
+ }
1263
+ return `/${clean}`;
1264
+ };
1265
+
1266
+ const toUtilityTemplatedDocUrl = (value: string) =>
1267
+ `${UTILITY_DOCS_ROOT_TOKEN}${toUtilityDocPath(value)}`;
1268
+
1269
+ export const getPrebuiltUtilityLlmPagePath = (slug: string) => {
1270
+ const normalized = normalizePath(slug);
1271
+ return `_utility/llms-pages/${normalized || "index"}.mdx`;
1272
+ };
1273
+
1274
+ export const buildUtilityArtifacts = (
1275
+ index: UtilityIndex
1276
+ ): UtilityArtifact[] => {
1277
+ const llmsLines = [
1278
+ `# ${index.name}`,
1279
+ index.description ? `> ${index.description}` : null,
1280
+ "",
1281
+ `Sitemap: ${toUtilityTemplatedDocUrl("sitemap.xml")}`,
1282
+ "",
1283
+ "## Docs",
1284
+ ...index.pages.map((page) => {
1285
+ const description = page.description ? `: ${page.description}` : "";
1286
+ return `- [${page.title}](${toUtilityTemplatedDocUrl(page.slug)})${description}`;
1287
+ }),
1288
+ ];
1289
+
1290
+ const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
1291
+ <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
1292
+ ${index.pages
1293
+ .map(
1294
+ (page) => ` <url><loc>${toUtilityTemplatedDocUrl(page.slug)}</loc></url>`
1295
+ )
1296
+ .join("\n")}
1297
+ </urlset>`;
1298
+
1299
+ const llmsFull = index.pages
1300
+ .map((page) =>
1301
+ formatMarkdownPageSection(
1302
+ page.title,
1303
+ toUtilityTemplatedDocUrl(page.slug),
1304
+ page.content
1305
+ )
1306
+ )
1307
+ .join("\n\n");
1308
+
1309
+ return [
1310
+ {
1311
+ content: sitemap,
1312
+ contentType: "application/xml; charset=utf-8",
1313
+ path: PREBUILT_UTILITY_SITEMAP_PATH,
1314
+ },
1315
+ {
1316
+ content: llmsLines.filter((line) => line !== null).join("\n"),
1317
+ contentType: "text/plain; charset=utf-8",
1318
+ path: PREBUILT_UTILITY_LLMS_PATH,
1319
+ },
1320
+ {
1321
+ content: llmsFull,
1322
+ contentType: "text/plain; charset=utf-8",
1323
+ path: PREBUILT_UTILITY_LLMS_FULL_PATH,
1324
+ },
1325
+ ...index.pages.map((page) => ({
1326
+ content: formatMarkdownPage(page.title, page.content),
1327
+ contentType: "text/markdown; charset=utf-8",
1328
+ path: getPrebuiltUtilityLlmPagePath(page.slug),
1329
+ })),
1330
+ ];
1331
+ };
1332
+
1333
+ export const buildTocIndex = async (
1334
+ index: ContentIndex,
1335
+ source: ContentSource
1336
+ ): Promise<Map<string, TocItem[]>> => {
1337
+ const itemsBySlug = new Map<string, TocItem[]>();
1338
+
1339
+ for (const entry of index.entries) {
1340
+ if (entry.kind !== "entry") {
1341
+ continue;
1342
+ }
1343
+
1344
+ const rawContent = await source.readFile(entry.relativePath);
1345
+ itemsBySlug.set(entry.slug, extractToc(rawContent));
1346
+ }
1347
+
1348
+ return itemsBySlug;
1349
+ };
1350
+
1351
+ export const serializeContentIndex = (index: ContentIndex): string =>
1352
+ JSON.stringify({
1353
+ collections: Object.fromEntries(index.byCollection),
1354
+ entries: index.entries,
1355
+ version: 1,
1356
+ } satisfies SerializedContentIndex);
1357
+
1358
+ export const serializeOpenApiIndex = (
1359
+ entries: PrebuiltOpenApiEntry[]
1360
+ ): string =>
1361
+ JSON.stringify({
1362
+ entries,
1363
+ version: 1,
1364
+ } satisfies SerializedOpenApiIndex);
1365
+
1366
+ export const loadPrebuiltContentIndex = async (
1367
+ source: ContentSource
1368
+ ): Promise<ContentIndex | null> => {
1369
+ try {
1370
+ const raw = await source.readFile(PREBUILT_INDEX_PATH);
1371
+ const data = JSON.parse(raw) as SerializedContentIndex;
1372
+ if (data.version !== 1 || !Array.isArray(data.entries)) {
1373
+ return null;
1374
+ }
1375
+
1376
+ const bySlug = new Map<string, ContentEntry>();
1377
+ const byCollection = new Map<string, ContentEntry[]>();
1378
+
1379
+ for (const entry of data.entries) {
1380
+ bySlug.set(entry.slug, entry);
1381
+ }
1382
+
1383
+ for (const [collectionId, entries] of Object.entries(
1384
+ data.collections ?? {}
1385
+ )) {
1386
+ byCollection.set(collectionId, entries);
1387
+ }
1388
+
1389
+ return {
1390
+ byCollection,
1391
+ bySlug,
1392
+ entries: data.entries,
1393
+ errors: [],
1394
+ };
1395
+ } catch {
1396
+ return null;
1397
+ }
1398
+ };
1399
+
1400
+ export const loadPrebuiltOpenApiIndex = async (
1401
+ source: ContentSource
1402
+ ): Promise<PrebuiltOpenApiEntry[] | null> => {
1403
+ try {
1404
+ const raw = await source.readFile(PREBUILT_OPENAPI_INDEX_PATH);
1405
+ const data = JSON.parse(raw) as SerializedOpenApiIndex;
1406
+ if (data.version !== 1 || !Array.isArray(data.entries)) {
1407
+ return null;
1408
+ }
1409
+
1410
+ return data.entries;
1411
+ } catch {
1412
+ return null;
1413
+ }
1414
+ };
1415
+
1416
+ export const serializeSearchIndex = (items: SearchIndexItem[]): string =>
1417
+ JSON.stringify({
1418
+ items,
1419
+ version: 1,
1420
+ } satisfies SerializedSearchIndex);
1421
+
1422
+ export const loadPrebuiltSearchIndex = async (
1423
+ source: ContentSource
1424
+ ): Promise<SearchIndexItem[] | null> => {
1425
+ try {
1426
+ const raw = await source.readFile(PREBUILT_SEARCH_INDEX_PATH);
1427
+ const data = JSON.parse(raw) as SerializedSearchIndex;
1428
+ if (data.version !== 1 || !Array.isArray(data.items)) {
1429
+ return null;
1430
+ }
1431
+
1432
+ return data.items;
1433
+ } catch {
1434
+ return null;
1435
+ }
1436
+ };
1437
+
1438
+ export const serializeTocIndex = (
1439
+ itemsBySlug: Map<string, TocItem[]>
1440
+ ): string =>
1441
+ JSON.stringify({
1442
+ itemsBySlug: Object.fromEntries(itemsBySlug),
1443
+ version: 1,
1444
+ } satisfies SerializedTocIndex);
1445
+
1446
+ export const loadPrebuiltTocIndex = async (
1447
+ source: ContentSource
1448
+ ): Promise<Map<string, TocItem[]> | null> => {
1449
+ try {
1450
+ const raw = await source.readFile(PREBUILT_TOC_INDEX_PATH);
1451
+ const data = JSON.parse(raw) as SerializedTocIndex;
1452
+ if (data.version !== 1 || typeof data.itemsBySlug !== "object") {
1453
+ return null;
1454
+ }
1455
+
1456
+ return new Map(Object.entries(data.itemsBySlug ?? {}));
1457
+ } catch {
1458
+ return null;
1459
+ }
1460
+ };
1461
+
1462
+ export const serializeUtilityIndex = (index: UtilityIndex): string =>
1463
+ JSON.stringify({
1464
+ ...index,
1465
+ version: 1,
1466
+ } satisfies SerializedUtilityIndex);
1467
+
1468
+ export const loadPrebuiltUtilityIndex = async (
1469
+ source: ContentSource
1470
+ ): Promise<UtilityIndex | null> => {
1471
+ try {
1472
+ const raw = await source.readFile(PREBUILT_UTILITY_INDEX_PATH);
1473
+ const data = JSON.parse(raw) as SerializedUtilityIndex;
1474
+ if (
1475
+ data.version !== 1 ||
1476
+ typeof data.name !== "string" ||
1477
+ !Array.isArray(data.pages)
1478
+ ) {
1479
+ return null;
1480
+ }
1481
+
1482
+ return {
1483
+ description: data.description,
1484
+ name: data.name,
1485
+ pages: data.pages,
1486
+ };
1487
+ } catch {
1488
+ return null;
1489
+ }
1490
+ };