Open History to see the last commit
1import * as path from "node:path";
2
3import type {
4 BindingClassDoc,
5 DirectoryCatalog,
6 FileDoc,
7 LinkDoc,
8 ShapeDoc,
9 SourceEntry,
10} from "./types.ts";
11import {
12 docsRoot,
13 escapeMarkdown,
14 labelFromDocRelativePath,
15 relativeDocLink,
16} from "./utils.ts";
17
18function renderLinks(links: LinkDoc[]) {
19 return links.map((link) => `- [${link.label}](${link.href})`).join("\n");
20}
21
22function renderShape(
23 shape: ShapeDoc | undefined,
24 heading: string,
25 level: "##" | "###" = "##",
26) {
27 if (!shape) {
28 return "";
29 }
30
31 if (shape.properties.length === 0) {
32 return [
33 `${level} ${heading}`,
34 shape.description ?? "",
35 shape.signature ? ["```ts", shape.signature, "```"].join("\n") : "",
36 ]
37 .filter(Boolean)
38 .join("\n\n");
39 }
40
41 const rows = shape.properties.map(
42 (property) =>
43 `| \`${escapeMarkdown(property.name)}\` | \`${escapeMarkdown(property.type)}\` | ${
44 property.optional ? "optional" : "required"
45 }${property.readonly ? ", readonly" : ""} | ${escapeMarkdown(
46 property.defaultValue ?? "-",
47 )} | ${escapeMarkdown(property.description ?? "-")} |`,
48 );
49
50 const table = [
51 "| Property | Type | Flags | Default | Description |",
52 "| --- | --- | --- | --- | --- |",
53 ...rows,
54 ].join("\n");
55
56 return [`${level} ${heading}`, shape.description ?? "", table]
57 .filter(Boolean)
58 .join("\n\n");
59}
60
61function renderBindingGroup(heading: string, items: BindingClassDoc[]) {
62 if (items.length === 0) {
63 return "";
64 }
65
66 return [
67 `### ${heading}`,
68 ...items.map((item) =>
69 [
70 `#### \`${item.name}\``,
71 item.identifier ? `Identifier: \`${item.identifier}\`` : "",
72 item.summary ?? "",
73 ]
74 .filter(Boolean)
75 .join("\n\n"),
76 ),
77 ].join("\n\n");
78}
79
80function renderExports(fileDoc: FileDoc) {
81 if (fileDoc.exports.length === 0) {
82 return "";
83 }
84
85 const rows = fileDoc.exports.map(
86 (entry) =>
87 `| \`${escapeMarkdown(entry.name)}\` | ${entry.kind} | \`${escapeMarkdown(
88 entry.signature,
89 )}\` | ${escapeMarkdown(entry.summary ?? "-")} |`,
90 );
91
92 const details = fileDoc.exports
93 .map((entry) =>
94 [
95 `### \`${entry.name}\``,
96 entry.summary ?? "",
97 ["```ts", entry.signature, "```"].join("\n"),
98 entry.shape ? renderShape(entry.shape, entry.shape.title, "###") : "",
99 ]
100 .filter(Boolean)
101 .join("\n\n"),
102 )
103 .join("\n\n");
104
105 return [
106 "## Exports",
107 "| Symbol | Kind | Signature | Description |",
108 "| --- | --- | --- | --- |",
109 ...rows,
110 "",
111 details,
112 ].join("\n");
113}
114
115function renderExamples(fileDoc: FileDoc) {
116 if (fileDoc.examples.length === 0 && !fileDoc.autoExample) {
117 return "";
118 }
119
120 const sections = fileDoc.examples.flatMap((section) => [
121 `### ${section.title}`,
122 ...section.examples.flatMap((example) => [
123 `#### ${example.title}`,
124 example.body,
125 ]),
126 ]);
127
128 if (fileDoc.autoExample) {
129 sections.push(
130 "### Quick Start",
131 `#### ${fileDoc.autoExample.title}`,
132 fileDoc.autoExample.body,
133 );
134 }
135
136 return ["## Examples", ...sections].join("\n\n");
137}
138
139function renderCatalog(catalog: DirectoryCatalog) {
140 const parts = ["## Navigation"];
141 if (catalog.parent) {
142 parts.push(`- Parent: [${catalog.parent.label}](${catalog.parent.href})`);
143 }
144 if (catalog.siblings.length > 0) {
145 parts.push("- Siblings");
146 parts.push(renderLinks(catalog.siblings));
147 }
148 return parts.join("\n");
149}
150
151function isPrimaryReferencePage(fileDoc: FileDoc) {
152 return ["resource", "host", "operation", "event-source"].includes(
153 fileDoc.fileKind,
154 );
155}
156
157export function renderFileDoc(fileDoc: FileDoc) {
158 const sourceHref = relativeDocLink(
159 fileDoc.outputPath,
160 path.join(docsRoot, "..", fileDoc.sourcePath),
161 );
162 const primaryReferencePage = isPrimaryReferencePage(fileDoc);
163
164 return [
165 "<!-- AUTO-GENERATED: DO NOT EDIT. Run `bun run generate:docs`. -->",
166 "",
167 `# ${fileDoc.title}`,
168 "",
169 fileDoc.summary,
170 "",
171 `- Source: [\`${fileDoc.sourcePath}\`](${sourceHref})`,
172 renderExamples(fileDoc),
173 fileDoc.resource && !primaryReferencePage
174 ? [
175 "## Resource Model",
176 `- Resource Type: \`${fileDoc.resource.resourceType}\``,
177 fileDoc.resource.providerName
178 ? `- Provider Export: \`${fileDoc.resource.providerName}\``
179 : "",
180 fileDoc.resource.lifecycleOperations.length > 0
181 ? `- Lifecycle Operations: ${fileDoc.resource.lifecycleOperations
182 .map((name) => `\`${name}\``)
183 .join(", ")}`
184 : "",
185 ]
186 .filter(Boolean)
187 .join("\n")
188 : "",
189 renderShape(fileDoc.resource?.props, "Props"),
190 renderShape(fileDoc.resource?.attributes, "Attributes"),
191 renderShape(fileDoc.resource?.binding, "Binding Contract"),
192 fileDoc.operation
193 ? [
194 "## Reference",
195 renderBindingGroup("Runtime Bindings", fileDoc.operation.services),
196 renderBindingGroup(
197 "Deploy-Time Policies",
198 fileDoc.operation.policies,
199 ),
200 fileDoc.operation.runtimeLayers.length > 0
201 ? [
202 "### Live Layers",
203 fileDoc.operation.runtimeLayers
204 .map((name) => `- \`${name}\``)
205 .join("\n"),
206 ].join("\n\n")
207 : "",
208 fileDoc.operation.supportedHosts.length > 0
209 ? [
210 "### Supported Hosts",
211 fileDoc.operation.supportedHosts
212 .map((name) => `- \`${name}\``)
213 .join("\n"),
214 ].join("\n\n")
215 : "",
216 ...fileDoc.operation.requestShapes.map((shape) =>
217 renderShape(shape, shape.title),
218 ),
219 ]
220 .filter(Boolean)
221 .join("\n\n")
222 : "",
223 fileDoc.provider
224 ? [
225 "## Provider Exports",
226 fileDoc.provider.exportedFactories
227 .map((name) => `- \`${name}\``)
228 .join("\n"),
229 ].join("\n\n")
230 : "",
231 fileDoc.index && fileDoc.index.reExports.length > 0
232 ? [
233 "## Re-Exports",
234 "| Export | Source |",
235 "| --- | --- |",
236 ...fileDoc.index.reExports.map((entry) => {
237 const source = entry.href
238 ? `[\`${entry.sourcePath}\`](${entry.href})`
239 : `\`${entry.sourcePath}\``;
240 return `| \`${escapeMarkdown(entry.exportName)}\` | ${source} |`;
241 }),
242 ].join("\n")
243 : "",
244 !primaryReferencePage ? renderExports(fileDoc) : "",
245 !primaryReferencePage && fileDoc.relatedLinks.length > 0
246 ? ["## Related Files", renderLinks(fileDoc.relatedLinks)].join("\n\n")
247 : "",
248 !primaryReferencePage ? renderCatalog(fileDoc.directoryCatalog) : "",
249 ]
250 .filter((part) => part && part.trim().length > 0)
251 .join("\n\n");
252}
253
254function allDirectories(entries: SourceEntry[]) {
255 const directories = new Set<string>();
256 for (const entry of entries) {
257 let current = path.dirname(entry.relativePath);
258 while (true) {
259 directories.add(current);
260 if (current === ".") {
261 break;
262 }
263 current = path.dirname(current);
264 }
265 }
266 return [...directories].sort();
267}
268
269function hasSourceIndex(directory: string, entries: SourceEntry[]) {
270 return entries.some(
271 (entry) => entry.relativePath === path.join(directory, "index.ts"),
272 );
273}
274
275export function syntheticIndexes(entries: SourceEntry[]) {
276 return allDirectories(entries)
277 .filter((directory) => !hasSourceIndex(directory, entries))
278 .map((directory) => {
279 const outputPath = path.join(
280 docsRoot,
281 directory === "." ? "index.md" : directory,
282 "index.md",
283 );
284 const files = entries
285 .filter(
286 (entry) =>
287 path.dirname(entry.relativePath) === directory &&
288 path.basename(entry.relativePath) !== "index.ts",
289 )
290 .map((entry) => ({
291 label: labelFromDocRelativePath(entry.relativePath),
292 href: relativeDocLink(outputPath, entry.outputPath),
293 }))
294 .sort((left, right) => left.label.localeCompare(right.label));
295 const childDirectories = allDirectories(entries)
296 .filter(
297 (candidate) =>
298 candidate !== "." && path.dirname(candidate) === directory,
299 )
300 .map((candidate) => ({
301 label: path.basename(candidate),
302 href: relativeDocLink(
303 outputPath,
304 path.join(docsRoot, candidate, "index.md"),
305 ),
306 }))
307 .sort((left, right) => left.label.localeCompare(right.label));
308
309 const title =
310 directory === "." ? "API Reference" : path.basename(directory);
311 const content = [
312 "<!-- AUTO-GENERATED: DO NOT EDIT. Run `bun run generate:docs`. -->",
313 "",
314 `# ${title}`,
315 "",
316 directory === "."
317 ? "Static API reference generated from `alchemy/src`."
318 : `Directory index for \`src/${directory}\`.`,
319 "",
320 childDirectories.length > 0
321 ? ["## Directories", renderLinks(childDirectories)].join("\n\n")
322 : "",
323 files.length > 0 ? ["## Files", renderLinks(files)].join("\n\n") : "",
324 ]
325 .filter((part) => part && part.trim().length > 0)
326 .join("\n\n");
327
328 return { outputPath, content };
329 });
330}