blume 1.7.0 → 1.7.1

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 (71) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/cli/{chunk-s102bysw.js → chunk-12dzsn9b.js} +140 -23
  3. package/dist/cli/{chunk-s102bysw.js.map → chunk-12dzsn9b.js.map} +6 -5
  4. package/dist/cli/{chunk-agy5rzxy.js → chunk-4ae4f395.js} +78 -48
  5. package/dist/cli/chunk-4ae4f395.js.map +15 -0
  6. package/dist/cli/{chunk-tnskyrej.js → chunk-52cwcqvp.js} +4 -4
  7. package/dist/cli/{chunk-drke6t0h.js → chunk-5gfw0q4j.js} +9 -9
  8. package/dist/cli/{chunk-ckh3a410.js → chunk-6mq7qkve.js} +3 -3
  9. package/dist/cli/{chunk-0qhq7b8q.js → chunk-82atea4k.js} +5 -5
  10. package/dist/cli/{chunk-ye9zdkgv.js → chunk-8p3xe5jv.js} +2 -2
  11. package/dist/cli/{chunk-kwx90v78.js → chunk-90pdhkpm.js} +11 -11
  12. package/dist/cli/{chunk-jk1zwka1.js → chunk-aqjvpd03.js} +4 -4
  13. package/dist/cli/{chunk-cfw6x4rm.js → chunk-h9ekmtz7.js} +5 -5
  14. package/dist/cli/{chunk-jxkxjsc1.js → chunk-he2zfgah.js} +10 -10
  15. package/dist/cli/{chunk-zr3ygrq3.js → chunk-j5f2wrj5.js} +2 -2
  16. package/dist/cli/{chunk-qq9nm3qd.js → chunk-k0v1f8bb.js} +3 -3
  17. package/dist/cli/{chunk-ynacq3ev.js → chunk-ka5k7cz9.js} +6 -19
  18. package/dist/cli/{chunk-ynacq3ev.js.map → chunk-ka5k7cz9.js.map} +3 -4
  19. package/dist/cli/{chunk-v5mm027v.js → chunk-kmx2mydj.js} +2 -2
  20. package/dist/cli/{chunk-9qs6acpw.js → chunk-mfm4sjwx.js} +11 -11
  21. package/dist/cli/{chunk-y3g15rvv.js → chunk-np8dmfb0.js} +6 -6
  22. package/dist/cli/{chunk-18tjv4f7.js → chunk-pdwg3q9g.js} +11 -11
  23. package/dist/cli/{chunk-v2ymm99c.js → chunk-q56730e0.js} +11 -11
  24. package/dist/cli/{chunk-j6pxe0dt.js → chunk-qvvpnwaz.js} +2 -2
  25. package/dist/cli/{chunk-xv91q4nm.js → chunk-r99hynxh.js} +3 -2
  26. package/dist/cli/{chunk-xv91q4nm.js.map → chunk-r99hynxh.js.map} +3 -3
  27. package/dist/cli/{chunk-s5dsk8bj.js → chunk-vyqj481z.js} +8 -7
  28. package/dist/cli/{chunk-s5dsk8bj.js.map → chunk-vyqj481z.js.map} +3 -3
  29. package/dist/cli/{chunk-5d4q7121.js → chunk-x1wvw7a8.js} +21 -11
  30. package/dist/cli/{chunk-5d4q7121.js.map → chunk-x1wvw7a8.js.map} +5 -5
  31. package/dist/cli/{chunk-n0nyat6g.js → chunk-ywn7t0pb.js} +2 -2
  32. package/dist/cli/index.js +13 -13
  33. package/dist/types/components/layout/nav-utils.d.ts +15 -2
  34. package/dist/types/core/config-input.d.ts +7 -0
  35. package/dist/types/core/schema.d.ts +2 -0
  36. package/docs/08-faq.mdx +21 -0
  37. package/docs/configuration/ask-ai.mdx +16 -0
  38. package/docs/content/sources.mdx +2 -2
  39. package/package.json +1 -1
  40. package/src/ai/ask.ts +31 -4
  41. package/src/astro/generate.ts +1 -0
  42. package/src/astro/templates.ts +104 -60
  43. package/src/components/content/YouTube.astro +1 -1
  44. package/src/components/layout/Header.astro +1 -1
  45. package/src/components/layout/Search.astro +11 -0
  46. package/src/components/layout/nav-utils.ts +30 -12
  47. package/src/core/config-input.ts +7 -0
  48. package/src/core/schema.ts +5 -0
  49. package/src/core/sources/assets.ts +162 -26
  50. package/src/core/sources/notion.ts +60 -1
  51. package/src/registry/eject.ts +1 -0
  52. package/src/theme/entry.ts +9 -0
  53. package/dist/cli/chunk-agy5rzxy.js.map +0 -15
  54. /package/dist/cli/{chunk-tnskyrej.js.map → chunk-52cwcqvp.js.map} +0 -0
  55. /package/dist/cli/{chunk-drke6t0h.js.map → chunk-5gfw0q4j.js.map} +0 -0
  56. /package/dist/cli/{chunk-ckh3a410.js.map → chunk-6mq7qkve.js.map} +0 -0
  57. /package/dist/cli/{chunk-0qhq7b8q.js.map → chunk-82atea4k.js.map} +0 -0
  58. /package/dist/cli/{chunk-ye9zdkgv.js.map → chunk-8p3xe5jv.js.map} +0 -0
  59. /package/dist/cli/{chunk-kwx90v78.js.map → chunk-90pdhkpm.js.map} +0 -0
  60. /package/dist/cli/{chunk-jk1zwka1.js.map → chunk-aqjvpd03.js.map} +0 -0
  61. /package/dist/cli/{chunk-cfw6x4rm.js.map → chunk-h9ekmtz7.js.map} +0 -0
  62. /package/dist/cli/{chunk-jxkxjsc1.js.map → chunk-he2zfgah.js.map} +0 -0
  63. /package/dist/cli/{chunk-zr3ygrq3.js.map → chunk-j5f2wrj5.js.map} +0 -0
  64. /package/dist/cli/{chunk-qq9nm3qd.js.map → chunk-k0v1f8bb.js.map} +0 -0
  65. /package/dist/cli/{chunk-v5mm027v.js.map → chunk-kmx2mydj.js.map} +0 -0
  66. /package/dist/cli/{chunk-9qs6acpw.js.map → chunk-mfm4sjwx.js.map} +0 -0
  67. /package/dist/cli/{chunk-y3g15rvv.js.map → chunk-np8dmfb0.js.map} +0 -0
  68. /package/dist/cli/{chunk-18tjv4f7.js.map → chunk-pdwg3q9g.js.map} +0 -0
  69. /package/dist/cli/{chunk-v2ymm99c.js.map → chunk-q56730e0.js.map} +0 -0
  70. /package/dist/cli/{chunk-j6pxe0dt.js.map → chunk-qvvpnwaz.js.map} +0 -0
  71. /package/dist/cli/{chunk-n0nyat6g.js.map → chunk-ywn7t0pb.js.map} +0 -0
@@ -280,27 +280,45 @@ export interface NavVariant {
280
280
  navigation: Navigation;
281
281
  }
282
282
 
283
+ /**
284
+ * The locale whose code must not appear as a fragment URL segment: the
285
+ * default locale while its URL prefix is hidden. Astro's i18n routing 404s any
286
+ * page URL carrying that locale's code as a segment (it expects the default
287
+ * locale to be unprefixed), so its trees are keyed `default` instead, the same
288
+ * way its page routes drop the prefix. `null` when every locale is prefixed
289
+ * or the site is single-locale.
290
+ */
291
+ export const hiddenDefaultLocale = (
292
+ i18n: { defaultLocale: string; hideDefaultLocalePrefix: boolean } | null
293
+ ): string | null => (i18n?.hideDefaultLocalePrefix ? i18n.defaultLocale : null);
294
+
283
295
  /**
284
296
  * Every navigation tree the runtime data holds — the default, each locale's,
285
297
  * and each archived version's per locale — keyed the way the deferred
286
298
  * sidebar fragments' URLs are (`/blume-nav/<version>/<locale>/…`). An
287
299
  * unlocalized version tree is keyed by `""` in the data; it maps to
288
- * `default` here.
300
+ * `default` here, as does the hidden-prefix default locale's (see
301
+ * `hiddenDefaultLocale`), whose current tree is `data.navigation` already.
289
302
  */
290
- export const navVariants = (data: {
291
- navigation: Navigation;
292
- navigationByLocale: Record<string, Navigation>;
293
- navigationByVersion: Record<string, Record<string, Navigation>>;
294
- }): NavVariant[] => [
303
+ export const navVariants = (
304
+ data: {
305
+ navigation: Navigation;
306
+ navigationByLocale: Record<string, Navigation>;
307
+ navigationByVersion: Record<string, Record<string, Navigation>>;
308
+ },
309
+ hiddenDefault: string | null = null
310
+ ): NavVariant[] => [
295
311
  { locale: "default", navigation: data.navigation, version: "current" },
296
- ...Object.entries(data.navigationByLocale).map(([locale, navigation]) => ({
297
- locale,
298
- navigation,
299
- version: "current",
300
- })),
312
+ ...Object.entries(data.navigationByLocale)
313
+ .filter(([locale]) => locale !== hiddenDefault)
314
+ .map(([locale, navigation]) => ({
315
+ locale,
316
+ navigation,
317
+ version: "current",
318
+ })),
301
319
  ...Object.entries(data.navigationByVersion).flatMap(([version, byLocale]) =>
302
320
  Object.entries(byLocale).map(([locale, navigation]) => ({
303
- locale: locale || "default",
321
+ locale: locale && locale !== hiddenDefault ? locale : "default",
304
322
  navigation,
305
323
  version,
306
324
  }))
@@ -723,6 +723,13 @@ export interface AskConfig {
723
723
  * limiting, and streaming. Accepts an absolute URL or root-relative path.
724
724
  */
725
725
  endpoint?: string;
726
+ /**
727
+ * Static request headers sent to the provider on every call — a
728
+ * caller-identifying header for a shared backend, for example. Values are
729
+ * written into the generated route as literals, so keep secrets in
730
+ * `apiKeyEnv` rather than here.
731
+ */
732
+ headers?: Record<string, string>;
726
733
  /**
727
734
  * Extra system-prompt text appended to the built-in instructions — use it
728
735
  * for identity, language, or tone. The built-in grounding behavior (answer
@@ -877,6 +877,11 @@ const aiConfigSchema = z.strictObject({
877
877
  // and host Ask AI in an existing backend. Absolute URLs and root-relative
878
878
  // paths are both valid; the built-in request/stream contract is unchanged.
879
879
  endpoint: askEndpointSchema.optional(),
880
+ // Static request headers the generated endpoint sends the provider on
881
+ // every call (a caller-identifying header for a shared backend, say).
882
+ // Values are inlined into the generated route as literals, so the API
883
+ // key stays in `apiKeyEnv`; these are for non-secret metadata.
884
+ headers: z.record(z.string(), z.string()).optional(),
880
885
  // Extra system-prompt text (identity, language, tone) appended to the
881
886
  // built-in instructions, so the grounding contract — answer from the
882
887
  // retrieved excerpts, cite pages as Markdown links — stays intact.
@@ -1,4 +1,12 @@
1
- import { mkdir, writeFile } from "node:fs/promises";
1
+ import { randomUUID } from "node:crypto";
2
+ import {
3
+ access,
4
+ mkdir,
5
+ readdir,
6
+ rename,
7
+ rm,
8
+ writeFile,
9
+ } from "node:fs/promises";
2
10
 
3
11
  import { extname, join } from "pathe";
4
12
 
@@ -6,6 +14,12 @@ import type { Diagnostic } from "../types.ts";
6
14
  import { hashText } from "./cache.ts";
7
15
 
8
16
  const MD_IMAGE = /!\[(?<alt>[^\]]*)\]\((?<url>[^)\s]+)\)/gu;
17
+ // A `<video>` tag's `src`, split so the rewrite can swap the URL and keep the
18
+ // surrounding attributes untouched. Notion's uploaded videos arrive as signed,
19
+ // expiring URLs exactly like its images, so they rot the same way. `[^>]`
20
+ // bounds the attribute run to a single tag.
21
+ const HTML_VIDEO_SRC =
22
+ /(?<open><video\b[^>]*?\ssrc=")(?<url>[^"]+)(?<close>")/gu;
9
23
  const REMOTE = /^https?:\/\//u;
10
24
  const SAFE_EXT = /^\.[a-z0-9]+$/iu;
11
25
  const CODE_FENCE_BLOCK =
@@ -14,32 +28,107 @@ const CODE_FENCE_BLOCK =
14
28
  // oxlint-disable-next-line no-control-regex -- the NUL is the collision guard.
15
29
  const FENCE_TOKEN = /\u0000blume-fence-(?<index>\d+)\u0000/gu;
16
30
 
31
+ // The extension for a URL whose path carries none, keyed by the media type the
32
+ // server reports. A static host serves by extension, so a `.png` holding JPEG
33
+ // bytes is mislabeled and a `.png` holding a video is refused by strict
34
+ // players — the response is the source of truth, not the reference kind.
35
+ const EXT_BY_MIME = new Map([
36
+ ["image/avif", ".avif"],
37
+ ["image/gif", ".gif"],
38
+ ["image/jpeg", ".jpg"],
39
+ ["image/png", ".png"],
40
+ ["image/svg+xml", ".svg"],
41
+ ["image/webp", ".webp"],
42
+ ["video/mp4", ".mp4"],
43
+ ["video/ogg", ".ogv"],
44
+ ["video/quicktime", ".mov"],
45
+ ["video/webm", ".webm"],
46
+ ]);
47
+ const UNKNOWN_EXT = ".bin";
48
+ // Response types that are never a media file. A pasted Vimeo/Loom/Wistia link
49
+ // is a video block in Notion whose URL is a watch page (200 text/html), and an
50
+ // API endpoint answers JSON or XML; `image/svg+xml` is media and stays.
51
+ const NON_MEDIA_TYPE =
52
+ /^(?:text\/|application\/(?:[\w.-]+\+)?(?:json|xml|javascript))/u;
53
+ const PART_SUFFIX = ".part";
54
+ // Generous enough for a multi-hundred-megabyte recording on an ordinary
55
+ // connection; its job is to fail a stalled download rather than hang the build.
56
+ const DEFAULT_TIMEOUT_MS = 120_000;
57
+
17
58
  /** Where to write downloaded assets and how to reference them publicly. */
18
59
  export interface AssetContext {
19
60
  assetsDir: string;
20
61
  assetsBaseUrl: string;
21
62
  /** Injected for tests; defaults to the global `fetch`. */
22
63
  fetchImpl?: typeof fetch;
64
+ /**
65
+ * Gate for concurrent downloads. A source shares one gate across every page
66
+ * it materializes, so a database of video-heavy pages doesn't open every
67
+ * download at once. Defaults to no gate.
68
+ */
69
+ limit?: <T>(task: () => Promise<T>) => Promise<T>;
70
+ /** Abort a download that hasn't completed within this many milliseconds. */
71
+ timeoutMs?: number;
23
72
  }
24
73
 
25
- /** Pick a file extension from a URL, defaulting to `.png`. */
26
- const extFor = (url: string): string => {
74
+ /** The extension in a URL's path, or null when it carries none we'd trust. */
75
+ const extFromUrl = (url: string): string | null => {
27
76
  const clean = url.split("?")[0] ?? url;
28
77
  const ext = extname(clean);
29
- return SAFE_EXT.test(ext) ? ext.toLowerCase() : ".png";
78
+ return SAFE_EXT.test(ext) ? ext.toLowerCase() : null;
79
+ };
80
+
81
+ /** The response's media type, lowercased and stripped of parameters. */
82
+ const mediaType = (res: Response): string =>
83
+ res.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase() ?? "";
84
+
85
+ const exists = async (path: string): Promise<boolean> => {
86
+ try {
87
+ await access(path);
88
+ return true;
89
+ } catch {
90
+ return false;
91
+ }
92
+ };
93
+
94
+ /**
95
+ * The completed file for a stem whose extension came from an earlier response
96
+ * rather than the URL, so an extension-less asset is not fetched on every
97
+ * poll. Only a lone candidate counts: query-less URLs can collide, and picking
98
+ * between two files would be a guess.
99
+ */
100
+ const completedFor = async (
101
+ dir: string,
102
+ stem: string
103
+ ): Promise<string | null> => {
104
+ let names: string[];
105
+ try {
106
+ names = await readdir(dir);
107
+ } catch {
108
+ return null;
109
+ }
110
+ const candidates = names.filter(
111
+ (name) => name.startsWith(`${stem}.`) && !name.endsWith(PART_SUFFIX)
112
+ );
113
+ return candidates.length === 1 ? (candidates[0] ?? null) : null;
30
114
  };
31
115
 
32
116
  /**
33
- * Download remote images referenced in a Markdown body into the asset dir and
34
- * rewrite their `src` to the local public path. Remote CMS URLs (notably
35
- * Notion's signed, expiring links) would otherwise rot a static build. Assets
36
- * are content-addressed by URL hash, so repeated builds are stable and deduped.
117
+ * Download remote media referenced in a Markdown body into the asset dir and
118
+ * rewrite the reference to the local public path. Markdown images and the
119
+ * `src` of a `<video>` tag are both covered. Remote CMS URLs (notably Notion's
120
+ * signed, expiring links) would otherwise rot a static build. Assets are
121
+ * content-addressed by URL hash, so repeated builds are stable and deduped —
122
+ * and a file already on disk is not fetched again, which keeps a dev poll
123
+ * from re-downloading every video on every tick.
37
124
  */
38
125
  export const materializeAssets = async (
39
126
  markdown: string,
40
127
  ctx: AssetContext
41
128
  ): Promise<{ markdown: string; diagnostics: Diagnostic[] }> => {
42
129
  const doFetch = ctx.fetchImpl ?? globalThis.fetch;
130
+ const limit = ctx.limit ?? ((task) => task());
131
+ const timeoutMs = ctx.timeoutMs ?? DEFAULT_TIMEOUT_MS;
43
132
  const diagnostics: Diagnostic[] = [];
44
133
 
45
134
  // Mask fenced code blocks so an image URL inside a code sample is neither
@@ -52,34 +141,77 @@ export const materializeAssets = async (
52
141
  });
53
142
 
54
143
  const urls = new Set<string>();
55
- for (const match of masked.matchAll(MD_IMAGE)) {
56
- const url = match.groups?.url;
57
- if (url && REMOTE.test(url)) {
58
- urls.add(url);
144
+ for (const pattern of [MD_IMAGE, HTML_VIDEO_SRC]) {
145
+ for (const match of masked.matchAll(pattern)) {
146
+ const url = match.groups?.url;
147
+ if (url && REMOTE.test(url)) {
148
+ urls.add(url);
149
+ }
59
150
  }
60
151
  }
61
152
 
153
+ /** Fetch one asset into the asset dir and return its file name. */
154
+ const download = async (url: string): Promise<string> => {
155
+ // Hash the query-less URL: CMS asset URLs are pre-signed, so the query
156
+ // changes on every fetch of the same file — hashing it would mint a new
157
+ // file each refresh and re-dirty the content digest. Two real assets
158
+ // sharing scheme+host+path and differing only in query are rare enough to
159
+ // accept colliding.
160
+ const stem = hashText(url.split("?")[0] ?? url);
161
+ const urlExt = extFromUrl(url);
162
+ // The name is known up front whenever the path has an extension (every
163
+ // Notion upload does), so a file from an earlier run is reused as is;
164
+ // otherwise the extension came from the last response, so look for it.
165
+ if (urlExt && (await exists(join(ctx.assetsDir, `${stem}${urlExt}`)))) {
166
+ return `${stem}${urlExt}`;
167
+ }
168
+ const completed = urlExt ? null : await completedFor(ctx.assetsDir, stem);
169
+ if (completed) {
170
+ return completed;
171
+ }
172
+ const res = await doFetch(url, { signal: AbortSignal.timeout(timeoutMs) });
173
+ if (!res.ok) {
174
+ throw new Error(`${res.status}`);
175
+ }
176
+ // Writing a watch page or an API response out as `.mp4` gives a player
177
+ // that can't play and a green build, so refuse what is never media.
178
+ const type = mediaType(res);
179
+ if (NON_MEDIA_TYPE.test(type)) {
180
+ throw new Error(`responded with ${type}, not a media file`);
181
+ }
182
+ if (!res.body) {
183
+ throw new Error("empty response body");
184
+ }
185
+ const file = `${stem}${urlExt ?? EXT_BY_MIME.get(type) ?? UNKNOWN_EXT}`;
186
+ const target = join(ctx.assetsDir, file);
187
+ await mkdir(ctx.assetsDir, { recursive: true });
188
+ // Stream the body to disk rather than buffering it: a video is hundreds of
189
+ // megabytes where an image was a hundred kilobytes. Write to a temporary
190
+ // name unique to this attempt and rename on completion, so a download that
191
+ // dies midway never leaves a truncated file the next run would trust as
192
+ // complete, and two pages fetching the same asset at once (the gate bounds
193
+ // concurrency, it doesn't dedupe) never write into each other's file —
194
+ // both publish identical bytes and the last rename wins.
195
+ const part = `${target}.${randomUUID()}${PART_SUFFIX}`;
196
+ try {
197
+ await writeFile(part, res.body);
198
+ } catch (error) {
199
+ await rm(part, { force: true });
200
+ throw error;
201
+ }
202
+ await rename(part, target);
203
+ return file;
204
+ };
205
+
62
206
  const rewrites = new Map<string, string>();
63
207
  await Promise.all(
64
208
  [...urls].map(async (url) => {
65
209
  try {
66
- const res = await doFetch(url);
67
- if (!res.ok) {
68
- throw new Error(`${res.status}`);
69
- }
70
- const bytes = new Uint8Array(await res.arrayBuffer());
71
- // Hash the query-less URL (as `extFor` does): CMS asset URLs are
72
- // pre-signed, so the query changes on every fetch of the same image —
73
- // hashing it would mint a new file each refresh and re-dirty the
74
- // content digest. Two real assets sharing scheme+host+path and
75
- // differing only in query are rare enough to accept colliding.
76
- const file = `${hashText(url.split("?")[0] ?? url)}${extFor(url)}`;
77
- await mkdir(ctx.assetsDir, { recursive: true });
78
- await writeFile(join(ctx.assetsDir, file), bytes);
210
+ const file = await limit(() => download(url));
79
211
  rewrites.set(url, `${ctx.assetsBaseUrl}/${file}`);
80
212
  } catch (error) {
81
213
  // SAFETY: everything thrown in this block is an Error — the manual
82
- // `!res.ok` throw above, and fetch/fs failures.
214
+ // throws in `download`, and fetch/fs failures.
83
215
  diagnostics.push({
84
216
  code: "BLUME_ASSET_FETCH_FAILED",
85
217
  message: `Failed to download asset ${url}: ${(error as Error).message}`,
@@ -94,6 +226,10 @@ export const materializeAssets = async (
94
226
  const local = rewrites.get(url);
95
227
  return local ? `![${alt}](${local})` : match;
96
228
  })
229
+ .replaceAll(HTML_VIDEO_SRC, (match, open, url, close) => {
230
+ const local = rewrites.get(url);
231
+ return local ? `${open}${local}${close}` : match;
232
+ })
97
233
  .replaceAll(FENCE_TOKEN, (token, index) => fences[Number(index)] ?? token);
98
234
 
99
235
  return { diagnostics, markdown: rewritten };
@@ -3,6 +3,7 @@ import { setTimeout as sleep } from "node:timers/promises";
3
3
  import pLimit from "p-limit";
4
4
  import { join } from "pathe";
5
5
 
6
+ import { parseYouTubeId } from "../../components/content/youtube.ts";
6
7
  import { BlumeError } from "../diagnostics.ts";
7
8
  import matter from "../frontmatter.ts";
8
9
  import type { Diagnostic } from "../types.ts";
@@ -189,6 +190,7 @@ const MAX_RETRIES = 4;
189
190
  const BASE_DELAY_MS = 500;
190
191
  const SECOND_MS = 1000;
191
192
  const DEFAULT_CONCURRENCY = 3;
193
+ const ASSET_DOWNLOAD_CONCURRENCY = 4;
192
194
 
193
195
  /**
194
196
  * Retry a Notion API call on a `429 rate_limited`, honoring the `Retry-After`
@@ -250,6 +252,55 @@ const LIST_BLOCKS = new Set([
250
252
  const isListItem = (block: NotionBlock | undefined): boolean =>
251
253
  block !== undefined && LIST_BLOCKS.has(block.type);
252
254
 
255
+ const richToPlain = (rich: NotionRichText[] = []): string =>
256
+ rich.map((node) => node.plain_text).join("");
257
+
258
+ /**
259
+ * A string prop in JSX expression form. The quoted form (`title="…"`) keeps
260
+ * `\"` and `\n` as literal characters — MDX decodes no escapes there, so a
261
+ * caption holding a double quote fails to compile the whole page — where the
262
+ * expression form (`title={"…"}`) is a JSON string literal and decodes them.
263
+ */
264
+ const jsxString = (value: string): string => `{${JSON.stringify(value)}}`;
265
+
266
+ const YOUTUBE_HOST = /(?:^|\.)(?:youtube(?:-nocookie)?\.com|youtu\.be)$/u;
267
+
268
+ // `parseYouTubeId` is host-agnostic on purpose (the component accepts bare
269
+ // ids), so gate on the hostname first: `https://cdn.example.com/live/promo.mp4`
270
+ // matches its `/live/<11 chars>` shape but is a media file, not an embed.
271
+ const isYouTubeUrl = (url: string): boolean => {
272
+ const host = URL.parse(url)?.hostname ?? "";
273
+ return YOUTUBE_HOST.test(host) && parseYouTubeId(url) !== null;
274
+ };
275
+
276
+ /**
277
+ * Render a Notion `video` block. Kept out of `renderLeaf`'s switch so that
278
+ * function stays under the complexity limit.
279
+ */
280
+ const renderVideo = (data: NotionBlockPayload): string => {
281
+ const url = data.external?.url ?? data.file?.url;
282
+ if (!url) {
283
+ return "";
284
+ }
285
+ const caption = richToMarkdown(data.caption);
286
+ // A YouTube link pasted into Notion becomes a `video` block holding an
287
+ // external URL. That URL is a watch page, not a media file, so it has to
288
+ // become the embed component — a `<video src>` pointing at it plays nothing,
289
+ // and `materializeAssets` would download the HTML page. The iframe's title
290
+ // is its accessible name, so it gets the caption's plain text; the Markdown
291
+ // rendering goes to the Frame's caption, as it does for an upload.
292
+ const title = caption ? ` title=${jsxString(richToPlain(data.caption))}` : "";
293
+ // Everything else (a Notion upload, or a direct link to a media file) is a
294
+ // real video file: `materializeAssets` rewrites the `src`, which matters most
295
+ // for uploads, whose Notion URLs are signed and expire.
296
+ const media = isYouTubeUrl(url)
297
+ ? `<YouTube${title} url=${JSON.stringify(url)} />`
298
+ : `<video controls src=${JSON.stringify(url)} />`;
299
+ return caption
300
+ ? `<Frame caption=${jsxString(caption)}>\n${media}\n</Frame>`
301
+ : media;
302
+ };
303
+
253
304
  /** Render a leaf (non-container) block to Markdown, or null for containers. */
254
305
  const renderLeaf = (block: NotionBlock): string | null => {
255
306
  const data = payloadOf(block) ?? {};
@@ -289,6 +340,9 @@ const renderLeaf = (block: NotionBlock): string | null => {
289
340
  const url = data.external?.url ?? data.file?.url;
290
341
  return url ? `![${richToMarkdown(data.caption)}](${url})` : "";
291
342
  }
343
+ case "video": {
344
+ return renderVideo(data);
345
+ }
292
346
  default: {
293
347
  return null;
294
348
  }
@@ -311,6 +365,10 @@ export const notionSource = (
311
365
  // container — an unbounded burst guarantees 429s that even the retry loop
312
366
  // can't recover from, so every API call funnels through this limiter.
313
367
  const limit = pLimit(Math.max(1, options.concurrency ?? DEFAULT_CONCURRENCY));
368
+ // Asset downloads get their own gate: they go to Notion's S3, not its rate-
369
+ // limited API, and a slow video must not hold API slots. One gate per source
370
+ // bounds the fan-out across every page, not just within one.
371
+ const downloads = pLimit(ASSET_DOWNLOAD_CONCURRENCY);
314
372
  // Every Notion API call goes through the limiter, inside the retry — so a
315
373
  // call sleeping through a backoff doesn't hold a slot while it waits.
316
374
  const notionCall = <T>(call: () => Promise<T>): Promise<T> =>
@@ -380,7 +438,7 @@ export const notionSource = (
380
438
  return `<Callout>\n${body}\n</Callout>`;
381
439
  }
382
440
  if (block.type === "toggle") {
383
- const title = JSON.stringify(richToMarkdown(blockField(block)));
441
+ const title = jsxString(richToMarkdown(blockField(block)));
384
442
  return `<Accordion>\n<AccordionItem title=${title}>\n${await children(block)}\n</AccordionItem>\n</Accordion>`;
385
443
  }
386
444
  if (block.type === "column_list") {
@@ -510,6 +568,7 @@ export const notionSource = (
510
568
  assetsBaseUrl,
511
569
  assetsDir,
512
570
  fetchImpl: options.fetchImpl,
571
+ limit: downloads,
513
572
  });
514
573
  const raw = matter.stringify(assets.markdown, data);
515
574
  return {
@@ -551,6 +551,7 @@ export const eject = async (
551
551
  ...changelogFiles(project, pages, srcDir, {
552
552
  exportEpub,
553
553
  exportPdf,
554
+ mathEnabled: usesMath,
554
555
  needsReact,
555
556
  staged: hasStaged,
556
557
  })
@@ -194,6 +194,15 @@ ${THEME_MAPPING}
194
194
  scroll-padding-top: 4.5rem;
195
195
  text-rendering: optimizeLegibility;
196
196
  }
197
+ /* Modal surfaces coordinate through independent root attributes. The lock
198
+ remains until every owner releases its attribute, while !important lets it
199
+ temporarily override (and therefore preserve) an authored inline value.
200
+ Classic (non-overlay) scrollbars would otherwise vanish while locked and
201
+ shift the page and the centered dialog sideways, so keep their gutter. */
202
+ html:where([data-blume-nav-open], [data-blume-search-dialog-open]) {
203
+ overflow: hidden !important;
204
+ scrollbar-gutter: stable;
205
+ }
197
206
  /* Headings use the display font (defaults to the body font when unset).
198
207
  The tightened tracking is part of the theme, not the font: display-tuned
199
208
  families bake it into their metrics, but a text family promoted to