-
Notifications
You must be signed in to change notification settings - Fork 16.1k
Expand file tree
/
Copy pathastro.config.ts
More file actions
298 lines (278 loc) · 9.57 KB
/
Copy pathastro.config.ts
File metadata and controls
298 lines (278 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { readdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig } from "astro/config";
import react from "@astrojs/react";
import tailwindcss from "@tailwindcss/vite";
import icon from "astro-icon";
import skills from "astro-skills";
import nimbus, {
defineConfig as defineNimbusConfig,
} from "@cloudflare/nimbus-docs";
import { hastPlugins } from "./src/plugins/satteri";
import { createSitemapLastmodSerializer } from "./sitemap.serializer";
import { isDisallowedByRobots } from "./src/util/robots";
// One sidebar section per top-level product directory, autogenerated from the
// shared content tree (mirrors CF's `autogenSections` in the Starlight config).
// `scope: "section"` then surfaces only the current product's tree per page;
// without the full set, off-pilot pages fell back to the whole configured list.
async function autogenSections() {
const dirs = await readdir("./src/content/docs/", { withFileTypes: true });
return dirs
.filter((entry) => entry.isDirectory() && entry.name !== "agent-setup")
.map((entry) => ({
label: entry.name,
items: [{ autogenerate: { directory: entry.name, collapsed: true } }],
}));
}
async function getExternalLinkPaths(dir: string): Promise<Set<string>> {
const paths = new Set<string>();
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const full = join(dir, entry.name);
if (entry.isDirectory()) {
for (const path of await getExternalLinkPaths(full)) {
paths.add(path);
}
} else if (entry.name.endsWith(".mdx") || entry.name.endsWith(".md")) {
const content = await readFile(full, "utf-8");
const match = content.match(/^---\n([\s\S]*?)\n---/);
if (match?.[1].includes("external_link:")) {
let rel = full.slice("src/content/docs".length);
rel = rel.replace(/\.(mdx|md)$/, "");
rel = rel.replace(/\/index$/, "/");
if (!rel.endsWith("/")) rel += "/";
paths.add(rel.toLowerCase());
}
}
}
return paths;
}
const sidebarItems = await autogenSections();
const externalLinkPaths = await getExternalLinkPaths("src/content/docs");
const serializeSitemapLastmod = createSitemapLastmodSerializer();
// Mirrors production's sitemap `filter` (root astro.config.ts).
function isExcludedFromSitemap(url: string): boolean {
if (url.includes("/style-guide/")) return true;
if (url.endsWith("/404/")) return true;
const pathname = new URL(url).pathname;
if (externalLinkPaths.has(pathname)) return true;
if (isDisallowedByRobots(pathname)) return true;
return false;
}
// Resolved against this file (project root).
const here = (p: string) =>
fileURLToPath(new URL(p, import.meta.url)).replace(/\/$/, "");
const nimbusConfig = defineNimbusConfig({
site: "https://developers.cloudflare.com",
title: "Cloudflare Docs",
description: "Cloudflare's documentation.",
locale: "en",
github: "https://github.com/cloudflare/cloudflare-docs",
editPattern:
"https://github.com/cloudflare/cloudflare-docs/edit/production/{path}",
socialImage: "/og-docs.png",
socialImageAlt: "Cloudflare Docs",
// "custom" renders the search UI slot but skips the built-in Pagefind index;
// Nimbus mounts Algolia DocSearch instead (see ui/search/DocSearch.astro).
search: { provider: "custom" },
sidebar: {
items: sidebarItems,
overviewLabel: "Overview",
indexDisplay: "overview-leaf",
scope: "section",
defaultCollapsed: true,
},
});
// Maps Starlight icon names used in shared content to the iconify sets we ship,
// without touching content on disk: a pre-stage, MDX-scoped string rewrite.
const iconAlias = {
name: "cf-nimbus:icon-alias",
enforce: "pre" as const,
transform(code: string, id: string) {
const [pathOnly] = id.split("?", 1);
if (!pathOnly?.endsWith(".mdx") && !pathOnly?.endsWith(".md")) return null;
if (!/icon\s*=/.test(code)) return null;
// Starlight ships a native `seti:` file-icon set; Nimbus routes icons
// through astro-icon, which has no `seti` set. Map every `seti:` name the
// shared content uses onto an installed set (vscode-icons / ph /
// simple-icons). The substitute glyphs differ visually from Starlight's
// seti set — a parity item tracked for the parity gate (Epic F), not a
// render blocker.
const SETI: Record<string, string> = {
javascript: "vscode-icons:file-type-js-official",
typescript: "vscode-icons:file-type-typescript-official",
python: "vscode-icons:file-type-python",
shell: "ph:terminal-window",
rust: "vscode-icons:file-type-rust",
video: "ph:play-circle",
db: "ph:database",
php: "vscode-icons:file-type-php",
html: "vscode-icons:file-type-html",
docker: "simple-icons:docker",
svelte: "vscode-icons:file-type-svelte",
powershell: "vscode-icons:file-type-powershell",
notebook: "ph:notebook",
nix: "vscode-icons:file-type-nix",
json: "vscode-icons:file-type-json",
java: "vscode-icons:file-type-java",
go: "vscode-icons:file-type-go",
"c-sharp": "vscode-icons:file-type-csharp",
windows: "simple-icons:windows",
linux: "simple-icons:linux",
redhat: "simple-icons:redhat",
debian: "simple-icons:debian",
lock: "ph:lock",
info: "ph:info",
eye: "ph:eye",
plan: "ph:file-text",
};
// Bare icon names (no set prefix) resolve against Starlight's built-in
// icon set in the default build; under Nimbus they go through astro-icon
// (local `src/icons` + installed iconify sets). Names not present locally
// are mapped here onto an installed set. Glyphs differ visually from
// Starlight's — a parity item for the parity gate (Epic F).
const BARE: Record<string, string> = {
document: "ph:file-text",
"open-book": "ph:book-open",
pen: "ph:pencil-simple",
discord: "simple-icons:discord",
"x.com": "simple-icons:x",
apple: "ph:apple-logo",
astro: "simple-icons:astro",
bluesky: "simple-icons:bluesky",
clock: "ph:clock",
"comment-alt": "ph:chat-circle",
connection: "ph:plugs-connected",
database: "ph:database",
download: "ph:download-simple",
email: "ph:envelope-simple",
error: "ph:warning-circle",
external: "ph:arrow-square-out",
github: "simple-icons:github",
heart: "ph:heart",
information: "ph:info",
key: "ph:key",
laptop: "ph:laptop",
linux: "simple-icons:linux",
"list-format": "ph:list-bullets",
magnifier: "ph:magnifying-glass",
mastodon: "simple-icons:mastodon",
moon: "ph:moon",
puzzle: "ph:puzzle-piece",
rocket: "ph:rocket-launch",
setting: "ph:gear",
star: "ph:star",
terminal: "ph:terminal-window",
};
let out = code;
out = out.replace(/(["'])seti:([a-z0-9_-]+)\1/gi, (match, q, name) => {
const mapped = SETI[name.toLowerCase()];
return mapped ? `${q}${mapped}${q}` : match;
});
out = out.replace(
/\bicon\s*=\s*(["'])([a-z0-9_.-]+)\1/gi,
(match, q, name) => {
if (name.includes(":")) return match;
const mapped = BARE[name.toLowerCase()];
return mapped ? `icon=${q}${mapped}${q}` : match;
},
);
return out === code ? null : { code: out, map: null };
},
};
const markdown = {
syntaxHighlight: {
type: "shiki" as const,
excludeLangs: ["math", "mermaid"],
},
smartypants: false,
};
const integrations = [
icon(),
react(),
// Injects /.well-known/agent-skills/* routes (index.json, SKILL.md, tarballs).
skills(),
nimbus(nimbusConfig, {
mdx: { optimize: true },
markdown: { hastPlugins },
validateMdx: false,
// Sitemap parity (T3): drop excluded URLs, stamp lastmod on the rest.
sitemap: {
serialize: async (item) =>
isExcludedFromSitemap(item.url)
? undefined
: serializeSitemapLastmod(
// nominal-only gap between nimbus-docs and @astrojs/sitemap types
item as Parameters<typeof serializeSitemapLastmod>[0],
),
},
// Partial resolution ( <Render file="..." product="..." /> ) is handled
// entirely by our own Render.astro component via astro:content's
// `getEntry("partials", id)` — no integration-level hook needed.
rules: {
"nimbus/frontmatter-shape": "error",
"nimbus/image-ref": [
"error",
{ aliases: { "~/assets/": "src/assets/" } },
],
"nimbus/internal-link": "error",
},
}),
];
// Hint to the bundler that the components.ts re-export barrel has no module
// side effects, so unused named exports get tree-shaken instead of pulling
// in every component on every page.
const componentsBarrelId = normalizeId(here("./src/components.ts"));
function normalizeId(id: string) {
return id.replace(/\\/g, "/").replace(/\?.*$/, "");
}
const componentsBarrelSideEffects = {
name: "cf:components-barrel-side-effects",
enforce: "pre" as const,
transform: {
filter: { id: /[\\/]components\.ts(?:\?.*)?$/ },
handler(code: string, id: string) {
if (normalizeId(id) !== componentsBarrelId) return;
return { code, moduleSideEffects: false };
},
},
};
const appVite = {
// Force a single React instance in case any dependency ships its own copy
// of react/react-dom; without dedupe, Vite can load two React copies and
// client islands fail to hydrate with "jsxDEV is not a function".
resolve: { dedupe: ["react", "react-dom"] },
plugins: [tailwindcss(), componentsBarrelSideEffects, iconAlias],
};
// https://astro.build/config
export default defineConfig({
site: "https://developers.cloudflare.com",
prefetch: {
prefetchAll: true,
defaultStrategy: "hover",
},
outDir: "./dist",
cacheDir: ".astro-cache",
markdown,
image: {
service: {
entrypoint: "astro/assets/services/sharp",
config: {
limitInputPixels: false,
},
},
},
server: {
port: 1111,
},
integrations,
vite: {
...appVite,
server: {
watch: {
ignored: ["**/dist/**", "**/.astro-cache/**"],
},
},
},
});