@lexibase/rdx-core 1.4.0 → 2.1.0

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.
@@ -0,0 +1,947 @@
1
+ // src/config/index.ts
2
+ import path5 from "path";
3
+ import { existsSync } from "fs";
4
+
5
+ // src/config/error.ts
6
+ var RdxConfigError = class extends Error {
7
+ constructor(message) {
8
+ super(`Invalid rdx.config.ts: ${message}`);
9
+ this.name = "RdxConfigError";
10
+ }
11
+ };
12
+
13
+ // src/config/metadata.ts
14
+ function validateMetadata(input) {
15
+ function string(value, field) {
16
+ if (typeof value !== "string" || !value.trim())
17
+ throw new RdxConfigError(`${field} must be a non-empty string.`);
18
+ return value;
19
+ }
20
+ function url(value, field, relative = false) {
21
+ const source = string(value, field);
22
+ try {
23
+ if (/[\s\\]/.test(source)) throw new Error();
24
+ if (relative && /^\/(?!\/)/.test(source)) return;
25
+ const parsed = new URL(source);
26
+ if (!["http:", "https:"].includes(parsed.protocol) || parsed.username || parsed.password)
27
+ throw new Error();
28
+ } catch {
29
+ throw new RdxConfigError(
30
+ `${field} must be ${relative ? "a root-relative path or " : ""}an absolute http(s) URL.`
31
+ );
32
+ }
33
+ }
34
+ function object2(value, field, allowed) {
35
+ if (!value || typeof value !== "object" || Array.isArray(value))
36
+ throw new RdxConfigError(`${field} must be an object.`);
37
+ const result = value;
38
+ for (const key of Object.keys(result))
39
+ if (!allowed.includes(key))
40
+ throw new RdxConfigError(`unknown option ${field}.${key}.`);
41
+ return result;
42
+ }
43
+ function array(value, field, validate) {
44
+ if (!Array.isArray(value))
45
+ throw new RdxConfigError(`${field} must be an array.`);
46
+ value.forEach((item, index) => validate(item, `${field}[${index}]`));
47
+ }
48
+ const metadata = object2(input, "metadata", [
49
+ "metadataBase",
50
+ "applicationName",
51
+ "since",
52
+ "authors",
53
+ "keywords",
54
+ "icons",
55
+ "manifest",
56
+ "openGraph",
57
+ "twitter"
58
+ ]);
59
+ if (metadata.metadataBase !== void 0)
60
+ url(metadata.metadataBase, "metadata.metadataBase");
61
+ string(metadata.applicationName, "metadata.applicationName");
62
+ if (metadata.since !== void 0 && (typeof metadata.since !== "number" || !Number.isInteger(metadata.since) || metadata.since < 1 || metadata.since > (/* @__PURE__ */ new Date()).getFullYear()))
63
+ throw new RdxConfigError(
64
+ "metadata.since must be a positive integer no later than the current year."
65
+ );
66
+ if (metadata.manifest !== void 0)
67
+ url(metadata.manifest, "metadata.manifest", true);
68
+ if (metadata.keywords !== void 0)
69
+ array(metadata.keywords, "metadata.keywords", string);
70
+ if (metadata.authors !== void 0)
71
+ array(metadata.authors, "metadata.authors", (value, field) => {
72
+ const author = object2(value, field, ["name", "url"]);
73
+ string(author.name, `${field}.name`);
74
+ if (author.url !== void 0) url(author.url, `${field}.url`);
75
+ });
76
+ if (metadata.icons !== void 0) {
77
+ const icons = object2(metadata.icons, "metadata.icons", [
78
+ "icon",
79
+ "shortcut",
80
+ "apple"
81
+ ]);
82
+ for (const [key, value] of Object.entries(icons)) {
83
+ if (value === void 0) continue;
84
+ const field = `metadata.icons.${key}`;
85
+ if (key === "icon" && Array.isArray(value))
86
+ array(value, field, (item, field2) => {
87
+ const icon = object2(item, field2, ["url", "sizes", "type"]);
88
+ url(icon.url, `${field2}.url`, true);
89
+ for (const key2 of ["sizes", "type"])
90
+ if (icon[key2] !== void 0) string(icon[key2], `${field2}.${key2}`);
91
+ });
92
+ else url(value, field, true);
93
+ }
94
+ }
95
+ for (const key of ["openGraph", "twitter"]) {
96
+ if (metadata[key] === void 0) continue;
97
+ const field = `metadata.${key}`;
98
+ const social = object2(
99
+ metadata[key],
100
+ field,
101
+ key === "openGraph" ? ["title", "description", "siteName", "locale", "type", "images"] : ["title", "description", "card", "site", "creator", "images"]
102
+ );
103
+ for (const [name, value] of Object.entries(social)) {
104
+ if (name === "images" || value === void 0) continue;
105
+ string(value, `${field}.${name}`);
106
+ if (name === "type" && value !== "website" || name === "card" && !["summary", "summary_large_image"].includes(value))
107
+ throw new RdxConfigError(`${field}.${name} has an unsupported value.`);
108
+ }
109
+ if (social.images !== void 0)
110
+ array(social.images, `${field}.images`, (value, field2) => {
111
+ if (key === "twitter") return url(value, field2, true);
112
+ const image = object2(value, field2, ["url", "width", "height", "alt"]);
113
+ url(image.url, `${field2}.url`, true);
114
+ if (image.alt !== void 0) string(image.alt, `${field2}.alt`);
115
+ for (const dimension of ["width", "height"])
116
+ if (image[dimension] !== void 0 && (typeof image[dimension] !== "number" || !Number.isInteger(image[dimension]) || image[dimension] <= 0))
117
+ throw new RdxConfigError(
118
+ `${field2}.${dimension} must be a positive integer.`
119
+ );
120
+ });
121
+ }
122
+ }
123
+
124
+ // src/config/localization.ts
125
+ import fs from "fs";
126
+ import path from "path";
127
+ function normalizeLocalization(input, language) {
128
+ const fail = (message) => {
129
+ throw new RdxConfigError(`localization: ${message}`);
130
+ };
131
+ if (!input || typeof input !== "object" || Array.isArray(input))
132
+ fail("expected an object.");
133
+ const value = input;
134
+ if (Object.keys(value).some(
135
+ (key) => !["locales", "contributionUrl"].includes(key)
136
+ ) || !Array.isArray(value.locales) || value.locales.length < 2)
137
+ fail("locales must declare at least two languages.");
138
+ function code(value2) {
139
+ if (typeof value2 !== "string")
140
+ return fail("locale code must be a language tag.");
141
+ try {
142
+ return Intl.getCanonicalLocales(value2)[0] ?? fail("empty locale code.");
143
+ } catch {
144
+ return fail(`invalid language tag "${value2}".`);
145
+ }
146
+ }
147
+ const primary = code(language);
148
+ const seen = /* @__PURE__ */ new Set();
149
+ const folders = /* @__PURE__ */ new Set();
150
+ const locales = value.locales.map((item) => {
151
+ if (!item || typeof item !== "object" || Array.isArray(item))
152
+ return fail("each locale must be an object.");
153
+ const entry = item;
154
+ if (Object.keys(entry).some(
155
+ (key) => !["code", "label", "folder"].includes(key)
156
+ ))
157
+ fail("unknown locale option.");
158
+ const tag = code(entry.code);
159
+ const url = tag.toLowerCase();
160
+ if (["docs", "rdx-search", "api"].includes(url) || seen.has(url))
161
+ fail(`duplicate or reserved locale "${tag}".`);
162
+ seen.add(url);
163
+ if (typeof entry.label !== "string" || !entry.label.trim())
164
+ return fail(`locale ${tag} requires a label.`);
165
+ if (tag === primary) {
166
+ if (entry.folder !== void 0)
167
+ fail("the primary language always uses docs/.");
168
+ return { code: tag, path: url, label: entry.label };
169
+ }
170
+ const folder = entry.folder ?? url;
171
+ if (typeof folder !== "string" || !/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/.test(folder))
172
+ return fail(`unsafe folder for ${tag}.`);
173
+ if (folders.has(folder.toLowerCase())) fail(`duplicate folder "${folder}".`);
174
+ folders.add(folder.toLowerCase());
175
+ return { code: tag, path: url, label: entry.label, folder };
176
+ });
177
+ if (!seen.has(primary.toLowerCase()))
178
+ fail(`locales must include the primary language ${primary}.`);
179
+ if (value.contributionUrl !== void 0) {
180
+ if (typeof value.contributionUrl !== "string" || !value.contributionUrl.trim())
181
+ fail("contributionUrl must be an absolute HTTP(S) URL.");
182
+ try {
183
+ const url = new URL(value.contributionUrl);
184
+ if (!["https:", "http:"].includes(url.protocol) || url.username || url.password)
185
+ fail(
186
+ "contributionUrl must be an absolute HTTP(S) URL without credentials."
187
+ );
188
+ } catch {
189
+ fail(
190
+ "contributionUrl must be an absolute HTTP(S) URL without credentials."
191
+ );
192
+ }
193
+ }
194
+ return {
195
+ defaultLocale: primary,
196
+ locales,
197
+ ...value.contributionUrl !== void 0 ? { contributionUrl: value.contributionUrl } : {}
198
+ };
199
+ }
200
+ function resolveLocalization(input, language, rootDir, command) {
201
+ const localization = normalizeLocalization(input, language);
202
+ for (const locale of localization.locales) {
203
+ if (!locale.folder) continue;
204
+ const directory = path.resolve(
205
+ /* turbopackIgnore: true */
206
+ rootDir,
207
+ "locale",
208
+ locale.folder,
209
+ "docs"
210
+ );
211
+ if (!fs.existsSync(
212
+ /* turbopackIgnore: true */
213
+ directory
214
+ )) {
215
+ if (command === "runtime" || command === "start") continue;
216
+ throw new RdxConfigError(
217
+ `localization: missing locale/${locale.folder}/docs/. Create it before enabling ${locale.code}.`
218
+ );
219
+ }
220
+ if (!fs.statSync(
221
+ /* turbopackIgnore: true */
222
+ directory
223
+ ).isDirectory())
224
+ throw new RdxConfigError(
225
+ `localization: locale/${locale.folder}/docs/ must be a directory.`
226
+ );
227
+ const relative = path.relative(
228
+ fs.realpathSync(
229
+ /* turbopackIgnore: true */
230
+ rootDir
231
+ ),
232
+ fs.realpathSync(
233
+ /* turbopackIgnore: true */
234
+ directory
235
+ )
236
+ );
237
+ if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
238
+ throw new RdxConfigError(
239
+ `localization: locale ${locale.code} must remain inside the project.`
240
+ );
241
+ }
242
+ return localization;
243
+ }
244
+
245
+ // src/config/paths.ts
246
+ import fs2 from "fs";
247
+ import path2 from "path";
248
+ function resolveProjectPath(rootDir, value, field) {
249
+ if (typeof value !== "string" || !value.trim())
250
+ throw new RdxConfigError(`${field} must be a non-empty string.`);
251
+ const resolved = path2.resolve(
252
+ /* turbopackIgnore: true */
253
+ rootDir,
254
+ value
255
+ );
256
+ const relative = path2.relative(rootDir, resolved);
257
+ if (path2.isAbsolute(value) || !relative || relative === ".." || relative.startsWith(`..${path2.sep}`) || path2.isAbsolute(relative))
258
+ throw new RdxConfigError(
259
+ `${field} must be a relative path inside the project.`
260
+ );
261
+ return resolved;
262
+ }
263
+ function inside(parent, child) {
264
+ const relative = path2.relative(parent, child);
265
+ return !relative || relative !== ".." && !relative.startsWith(`..${path2.sep}`) && !path2.isAbsolute(relative);
266
+ }
267
+ function boundedProjectPath(rootDir, value, field) {
268
+ const target = resolveProjectPath(rootDir, value, field);
269
+ const root = fs2.realpathSync(
270
+ /* turbopackIgnore: true */
271
+ rootDir
272
+ );
273
+ let parent = target;
274
+ while (true) {
275
+ try {
276
+ fs2.lstatSync(
277
+ /* turbopackIgnore: true */
278
+ parent
279
+ );
280
+ break;
281
+ } catch (error) {
282
+ if (error.code !== "ENOENT") throw error;
283
+ parent = path2.dirname(parent);
284
+ }
285
+ }
286
+ const canonical = path2.resolve(
287
+ fs2.realpathSync(
288
+ /* turbopackIgnore: true */
289
+ parent
290
+ ),
291
+ path2.relative(parent, target)
292
+ );
293
+ if (!inside(root, canonical))
294
+ throw new RdxConfigError(
295
+ `${field} must remain inside the project, including filesystem links.`
296
+ );
297
+ return canonical;
298
+ }
299
+
300
+ // src/config/source-localization.ts
301
+ import fs3 from "fs";
302
+ import path3 from "path";
303
+ function readSnapshot(rootDir, source, current, validateContent = true) {
304
+ const relative = path3.join(source.docsDir, ".rdx-locales.json");
305
+ const filename = boundedProjectPath(
306
+ rootDir,
307
+ relative,
308
+ "snapshot locale metadata"
309
+ );
310
+ try {
311
+ if (!fs3.lstatSync(
312
+ /* turbopackIgnore: true */
313
+ filename
314
+ ).isFile())
315
+ throw new Error("Expected a regular metadata file.");
316
+ } catch (error) {
317
+ if (error.code === "ENOENT") return;
318
+ throw error;
319
+ }
320
+ try {
321
+ const value = JSON.parse(
322
+ fs3.readFileSync(
323
+ /* turbopackIgnore: true */
324
+ filename,
325
+ "utf8"
326
+ )
327
+ );
328
+ if (!value || typeof value !== "object" || Array.isArray(value))
329
+ throw new Error("Expected a metadata object.");
330
+ const stored = value;
331
+ if (stored.schemaVersion !== 1 || Object.keys(stored).some(
332
+ (key) => ![
333
+ "schemaVersion",
334
+ "defaultLocale",
335
+ "locales",
336
+ "translations"
337
+ ].includes(key)
338
+ ) || typeof stored.defaultLocale !== "string")
339
+ throw new Error("Unsupported snapshot locale metadata.");
340
+ const frozen = normalizeLocalization(
341
+ { locales: stored.locales },
342
+ stored.defaultLocale
343
+ );
344
+ if (frozen.defaultLocale !== current.defaultLocale)
345
+ throw new Error(
346
+ "The primary language differs from this snapshot. Restore the original lang setting before using its versions."
347
+ );
348
+ if (!stored.translations || typeof stored.translations !== "object" || Array.isArray(stored.translations))
349
+ throw new Error("Expected translations metadata.");
350
+ const translations = stored.translations;
351
+ const expected = frozen.locales.filter((locale) => locale.folder);
352
+ if (Object.keys(translations).some(
353
+ (code) => !expected.some((locale) => locale.code === code)
354
+ ))
355
+ throw new Error("Unknown snapshot translation language.");
356
+ const docs = {
357
+ [frozen.defaultLocale]: source.docsDir
358
+ };
359
+ for (const locale of expected) {
360
+ const entry = translations[locale.code];
361
+ if (!entry || typeof entry !== "object" || Array.isArray(entry))
362
+ throw new Error(`Missing snapshot translation metadata: ${locale.code}`);
363
+ const data = entry;
364
+ const directory = `locale/${locale.folder}/versioned_docs/version-${source.version}`;
365
+ if (data.docsDir !== directory || typeof data.hasDocuments !== "boolean" || Object.keys(data).some(
366
+ (key) => !["docsDir", "hasDocuments"].includes(key)
367
+ ))
368
+ throw new Error(`Invalid snapshot directory for ${locale.code}.`);
369
+ const absolute = boundedProjectPath(
370
+ rootDir,
371
+ directory,
372
+ "snapshot translation"
373
+ );
374
+ if (validateContent && fs3.existsSync(
375
+ /* turbopackIgnore: true */
376
+ absolute
377
+ )) {
378
+ if (!fs3.statSync(
379
+ /* turbopackIgnore: true */
380
+ absolute
381
+ ).isDirectory())
382
+ throw new Error(
383
+ `Snapshot translation must be a directory: ${directory}`
384
+ );
385
+ } else if (validateContent && data.hasDocuments)
386
+ throw new Error(
387
+ `Missing snapshot translation ${directory}. Restore the published snapshot; live translations cannot replace it.`
388
+ );
389
+ docs[locale.code] = directory;
390
+ }
391
+ return { ...frozen, docs };
392
+ } catch (error) {
393
+ throw new RdxConfigError(
394
+ `Invalid ${relative}: ${error instanceof Error ? error.message : String(error)}`
395
+ );
396
+ }
397
+ }
398
+ function hydrateSourceLocalizations(rootDir, sources, current, runtimeFiles) {
399
+ const primary = current.locales.find(
400
+ (locale) => locale.code === current.defaultLocale
401
+ );
402
+ const languages = new Map(
403
+ current.locales.map((locale) => [locale.code, locale])
404
+ );
405
+ for (const [index, source] of sources.entries()) {
406
+ let inventory;
407
+ if (source.id === "current") {
408
+ inventory = {
409
+ ...current,
410
+ docs: Object.fromEntries(
411
+ current.locales.map((locale) => [
412
+ locale.code,
413
+ locale.folder ? `locale/${locale.folder}/docs` : source.docsDir
414
+ ])
415
+ )
416
+ };
417
+ } else {
418
+ const frozen = source.version ? readSnapshot(rootDir, source, current) : void 0;
419
+ inventory = frozen ?? {
420
+ defaultLocale: current.defaultLocale,
421
+ locales: [primary],
422
+ docs: { [primary.code]: source.docsDir }
423
+ };
424
+ if (frozen)
425
+ runtimeFiles.add(path3.join(source.docsDir, ".rdx-locales.json"));
426
+ }
427
+ sources[index] = { ...source, localization: inventory };
428
+ for (const locale of inventory.locales)
429
+ if (!languages.has(locale.code)) languages.set(locale.code, locale);
430
+ if (source.redirectTo !== void 0 && source.previousSnapshot) {
431
+ const retained = readSnapshot(
432
+ rootDir,
433
+ { ...source, ...source.previousSnapshot },
434
+ current,
435
+ false
436
+ );
437
+ if (retained) {
438
+ runtimeFiles.add(
439
+ path3.join(source.previousSnapshot.docsDir, ".rdx-locales.json")
440
+ );
441
+ for (const locale of retained.locales)
442
+ if (!languages.has(locale.code)) languages.set(locale.code, locale);
443
+ }
444
+ }
445
+ }
446
+ return {
447
+ ...current,
448
+ defaultLocale: current.defaultLocale,
449
+ locales: [...languages.values()]
450
+ };
451
+ }
452
+
453
+ // src/config/snapshots.ts
454
+ import fs4 from "fs";
455
+ import path4 from "path";
456
+ function collectSnapshotContributions(context, options) {
457
+ const directories = [
458
+ { sourceDir: options.sourceDir, snapshotDir: options.snapshotDir }
459
+ ];
460
+ const files = [];
461
+ const snapshotContext = {
462
+ ...context,
463
+ version: options.version,
464
+ snapshotDir: options.snapshotDir
465
+ };
466
+ for (const plugin of context.config.plugins) {
467
+ const result = plugin.snapshot?.(snapshotContext);
468
+ if (result === void 0) continue;
469
+ if (!result || typeof result !== "object" || Array.isArray(result) || "then" in result || Object.keys(result).some((key) => !["directories", "files"].includes(key)))
470
+ throw new RdxConfigError(
471
+ `Plugin "${plugin.name}" must declare a synchronous snapshot contribution.`
472
+ );
473
+ if (result.directories !== void 0 && !Array.isArray(result.directories) || result.files !== void 0 && !Array.isArray(result.files))
474
+ throw new RdxConfigError(
475
+ `Plugin "${plugin.name}" snapshot directories and files must be arrays.`
476
+ );
477
+ directories.push(...result.directories ?? []);
478
+ files.push(...result.files ?? []);
479
+ }
480
+ const mapped = directories.map((entry) => {
481
+ if (!entry || typeof entry !== "object" || Object.keys(entry).some(
482
+ (key) => !["sourceDir", "snapshotDir"].includes(key)
483
+ ))
484
+ throw new RdxConfigError("Invalid snapshot directory contribution.");
485
+ const from = boundedProjectPath(
486
+ context.rootDir,
487
+ entry.sourceDir,
488
+ "snapshot sourceDir"
489
+ );
490
+ const to = boundedProjectPath(
491
+ context.rootDir,
492
+ entry.snapshotDir,
493
+ "snapshot snapshotDir"
494
+ );
495
+ if (!fs4.statSync(
496
+ /* turbopackIgnore: true */
497
+ from
498
+ ).isDirectory())
499
+ throw new RdxConfigError(
500
+ `Snapshot source must be a directory: ${entry.sourceDir}`
501
+ );
502
+ try {
503
+ fs4.lstatSync(
504
+ /* turbopackIgnore: true */
505
+ to
506
+ );
507
+ throw new RdxConfigError(
508
+ `Snapshot destination already exists: ${entry.snapshotDir}`
509
+ );
510
+ } catch (error) {
511
+ if (error.code !== "ENOENT") throw error;
512
+ }
513
+ return { from, to };
514
+ });
515
+ for (const [index, entry] of mapped.entries()) {
516
+ for (const other of mapped) {
517
+ if (inside(entry.from, other.to) || inside(other.to, entry.from))
518
+ throw new RdxConfigError(
519
+ "Snapshot destinations and source trees must not overlap."
520
+ );
521
+ }
522
+ for (const other of mapped.slice(index + 1))
523
+ if (inside(entry.to, other.to) || inside(other.to, entry.to))
524
+ throw new RdxConfigError(
525
+ "Snapshot destinations must be distinct, non-overlapping directories."
526
+ );
527
+ }
528
+ const seen = /* @__PURE__ */ new Set();
529
+ const metadata = files.map((entry) => {
530
+ if (!entry || typeof entry !== "object" || typeof entry.content !== "string" || Object.keys(entry).some((key) => !["filename", "content"].includes(key)))
531
+ throw new RdxConfigError("Invalid snapshot file contribution.");
532
+ const filename = boundedProjectPath(
533
+ context.rootDir,
534
+ entry.filename,
535
+ "snapshot filename"
536
+ );
537
+ const directory = mapped.find(
538
+ (entry2) => filename !== entry2.to && inside(entry2.to, filename)
539
+ );
540
+ if (!directory || seen.has(filename))
541
+ throw new RdxConfigError(
542
+ "Snapshot files must have unique paths inside the new snapshot directories."
543
+ );
544
+ seen.add(filename);
545
+ const copiedFile = path4.join(
546
+ directory.from,
547
+ path4.relative(directory.to, filename)
548
+ );
549
+ if (fs4.existsSync(
550
+ /* turbopackIgnore: true */
551
+ copiedFile
552
+ ))
553
+ throw new RdxConfigError(
554
+ `Snapshot metadata conflicts with source content: ${entry.filename}`
555
+ );
556
+ return { filename, content: entry.content };
557
+ });
558
+ return {
559
+ rootDir: fs4.realpathSync(
560
+ /* turbopackIgnore: true */
561
+ context.rootDir
562
+ ),
563
+ directories: mapped,
564
+ files: metadata
565
+ };
566
+ }
567
+
568
+ // src/config/index.ts
569
+ function record(value) {
570
+ return typeof value === "object" && value !== null && !Array.isArray(value);
571
+ }
572
+ function object(value, field) {
573
+ if (!record(value)) throw new RdxConfigError(`${field} must be an object.`);
574
+ return value;
575
+ }
576
+ function text(value, field, allowEmpty = false) {
577
+ if (typeof value !== "string" || !allowEmpty && !value.trim()) {
578
+ throw new RdxConfigError(
579
+ `${field} must be ${allowEmpty ? "a string" : "a non-empty string"}.`
580
+ );
581
+ }
582
+ return value;
583
+ }
584
+ function component(value, field) {
585
+ if (typeof value !== "function" && !(record(value) && [
586
+ /* @__PURE__ */ Symbol.for("react.memo"),
587
+ /* @__PURE__ */ Symbol.for("react.forward_ref"),
588
+ /* @__PURE__ */ Symbol.for("react.lazy")
589
+ ].includes(value.$$typeof)))
590
+ throw new RdxConfigError(
591
+ `${field} must be a React component, not JSX or a file path.`
592
+ );
593
+ }
594
+ function keys(value, allowed, field) {
595
+ for (const key of Object.keys(value)) {
596
+ if (!allowed.includes(key))
597
+ throw new RdxConfigError(`unknown option ${field}${key}.`);
598
+ }
599
+ }
600
+ function resolveConfig(input) {
601
+ const config = object(input, "default export");
602
+ if ("title" in config)
603
+ throw new RdxConfigError(
604
+ "title has been removed. Set the site name in metadata.applicationName instead."
605
+ );
606
+ if ("docsDir" in config)
607
+ throw new RdxConfigError(
608
+ "docsDir is not configurable. Live documentation must be stored in docs/."
609
+ );
610
+ keys(
611
+ config,
612
+ [
613
+ "description",
614
+ "metadata",
615
+ "lang",
616
+ "theme",
617
+ "navbar",
618
+ "plugins",
619
+ "homepage",
620
+ "TOC",
621
+ "search"
622
+ ],
623
+ ""
624
+ );
625
+ if (config.search !== void 0 && typeof config.search !== "boolean")
626
+ throw new RdxConfigError("search must be a boolean.");
627
+ if (config.TOC !== void 0 && typeof config.TOC !== "boolean")
628
+ throw new RdxConfigError("TOC must be a boolean.");
629
+ if (config.description !== void 0)
630
+ text(config.description, "description", true);
631
+ if (config.lang !== void 0) text(config.lang, "lang");
632
+ validateMetadata(config.metadata);
633
+ if (config.homepage !== void 0) {
634
+ const homepage = object(config.homepage, "homepage");
635
+ keys(
636
+ homepage,
637
+ ["enabled", "component", "title", "description", "buttons"],
638
+ "homepage."
639
+ );
640
+ if (homepage.component !== void 0)
641
+ component(homepage.component, "homepage.component");
642
+ if (homepage.enabled !== void 0 && typeof homepage.enabled !== "boolean")
643
+ throw new RdxConfigError("homepage.enabled must be a boolean.");
644
+ if (homepage.title !== void 0) text(homepage.title, "homepage.title");
645
+ if (homepage.description !== void 0)
646
+ text(homepage.description, "homepage.description", true);
647
+ if (homepage.buttons !== void 0) {
648
+ if (!Array.isArray(homepage.buttons))
649
+ throw new RdxConfigError("homepage.buttons must be an array.");
650
+ for (const [index, value] of homepage.buttons.entries()) {
651
+ const field = `homepage.buttons[${index}]`;
652
+ const button = object(value, field);
653
+ keys(button, ["label", "href"], `${field}.`);
654
+ text(button.label, `${field}.label`);
655
+ const href = text(button.href, `${field}.href`);
656
+ if (/[\s\\]/.test(href) || !/^\/(?!\/)/.test(href) && !/^https?:\/\//i.test(href))
657
+ throw new RdxConfigError(
658
+ `${field}.href must be a root-relative path or http(s) URL.`
659
+ );
660
+ try {
661
+ new URL(href, "https://rdx.local");
662
+ } catch {
663
+ throw new RdxConfigError(`${field}.href must be a valid URL.`);
664
+ }
665
+ }
666
+ }
667
+ }
668
+ if (config.theme !== void 0) {
669
+ const theme = object(config.theme, "theme");
670
+ keys(theme, ["defaultTheme"], "theme.");
671
+ if (theme.defaultTheme !== void 0 && (typeof theme.defaultTheme !== "string" || !["system", "dark", "light"].includes(theme.defaultTheme))) {
672
+ throw new RdxConfigError(
673
+ "theme.defaultTheme must be system, dark, or light."
674
+ );
675
+ }
676
+ }
677
+ if (config.navbar !== void 0) {
678
+ const navbar = object(config.navbar, "navbar");
679
+ keys(navbar, ["title", "logo", "actions"], "navbar.");
680
+ if (navbar.logo !== void 0) component(navbar.logo, "navbar.logo");
681
+ if (navbar.title !== void 0) text(navbar.title, "navbar.title", true);
682
+ if (navbar.actions !== void 0) {
683
+ const actions = object(navbar.actions, "navbar.actions");
684
+ keys(actions, ["github", "showToggleMode"], "navbar.actions.");
685
+ if (actions.showToggleMode !== void 0 && typeof actions.showToggleMode !== "boolean")
686
+ throw new RdxConfigError(
687
+ "navbar.actions.showToggleMode must be a boolean."
688
+ );
689
+ if (actions.github !== void 0) {
690
+ const github = object(actions.github, "navbar.actions.github");
691
+ keys(github, ["visible", "url"], "navbar.actions.github.");
692
+ if (typeof github.visible !== "boolean")
693
+ throw new RdxConfigError(
694
+ "navbar.actions.github.visible must be a boolean."
695
+ );
696
+ const url = text(github.url, "navbar.actions.github.url");
697
+ if (url !== "#" && !/^https?:\/\//i.test(url))
698
+ throw new RdxConfigError(
699
+ 'navbar.actions.github.url must use http or https, or "#" as a placeholder.'
700
+ );
701
+ try {
702
+ if (url !== "#") new URL(url);
703
+ } catch {
704
+ throw new RdxConfigError(
705
+ "navbar.actions.github.url must be a valid URL."
706
+ );
707
+ }
708
+ }
709
+ }
710
+ }
711
+ if (config.plugins !== void 0 && !Array.isArray(config.plugins)) {
712
+ throw new RdxConfigError("plugins must be an array of plugin objects.");
713
+ }
714
+ const names = /* @__PURE__ */ new Set();
715
+ for (const [index, value] of (config.plugins ?? []).entries()) {
716
+ const plugin = object(
717
+ value,
718
+ `plugins[${index}] (call the plugin factory, e.g. versioning())`
719
+ );
720
+ const name = text(plugin.name, `plugins[${index}].name`);
721
+ if (names.has(name))
722
+ throw new RdxConfigError(`plugin "${name}" is configured more than once.`);
723
+ names.add(name);
724
+ if (plugin.apiVersion !== 1)
725
+ throw new RdxConfigError(`plugin "${name}" must support apiVersion 1.`);
726
+ if (plugin.setup !== void 0 && typeof plugin.setup !== "function")
727
+ throw new RdxConfigError(`plugin "${name}" setup must be a function.`);
728
+ if (plugin.commands !== void 0 && !Array.isArray(plugin.commands))
729
+ throw new RdxConfigError(`plugin "${name}" commands must be an array.`);
730
+ if (plugin.snapshot !== void 0 && typeof plugin.snapshot !== "function")
731
+ throw new RdxConfigError(`plugin "${name}" snapshot must be a function.`);
732
+ }
733
+ const typed = config;
734
+ return {
735
+ ...typed,
736
+ TOC: typed.TOC ?? false,
737
+ search: typed.search ?? false,
738
+ description: typed.description ?? "",
739
+ lang: typed.lang ?? "en",
740
+ theme: { defaultTheme: typed.theme?.defaultTheme ?? "system" },
741
+ plugins: typed.plugins ?? []
742
+ };
743
+ }
744
+ function defineConfig(config) {
745
+ resolveConfig(config);
746
+ return config;
747
+ }
748
+ async function createRdxProject(input, {
749
+ rootDir = process.cwd(),
750
+ command = "runtime"
751
+ } = {}) {
752
+ rootDir = path5.resolve(
753
+ /* turbopackIgnore: true */
754
+ rootDir
755
+ );
756
+ const config = resolveConfig(input);
757
+ const docsRoot = resolveProjectPath(
758
+ rootDir,
759
+ "docs",
760
+ "documentation directory"
761
+ );
762
+ const sources = [
763
+ {
764
+ id: "current",
765
+ label: "Current",
766
+ docsDir: "docs",
767
+ routePrefix: ""
768
+ }
769
+ ];
770
+ const commands = [];
771
+ const runtimeFiles = /* @__PURE__ */ new Set();
772
+ let localization;
773
+ const commandNames = /* @__PURE__ */ new Set([
774
+ "dev",
775
+ "build",
776
+ "start",
777
+ "prepare",
778
+ "check",
779
+ "help",
780
+ "add"
781
+ ]);
782
+ for (const plugin of config.plugins) {
783
+ for (const value of plugin.commands ?? []) {
784
+ const entry = object(value, `plugin "${plugin.name}" command`);
785
+ const name = text(entry.name, `plugin "${plugin.name}" command.name`);
786
+ if (!/^[a-z][a-z0-9-]*$/.test(name) || commandNames.has(name)) {
787
+ throw new RdxConfigError(
788
+ `plugin "${plugin.name}" command "${name}" is invalid or already registered.`
789
+ );
790
+ }
791
+ text(entry.description, `command ${name}.description`);
792
+ if (typeof entry.run !== "function")
793
+ throw new RdxConfigError(`command ${name}.run must be a function.`);
794
+ commandNames.add(name);
795
+ commands.push(value);
796
+ }
797
+ }
798
+ for (const plugin of config.plugins) {
799
+ let result;
800
+ try {
801
+ result = await plugin.setup?.({ rootDir, command, config });
802
+ } catch (error) {
803
+ throw new Error(
804
+ `[${plugin.name}] ${error instanceof Error ? error.message : String(error)}`
805
+ );
806
+ }
807
+ if (result === void 0) continue;
808
+ const contribution = object(result, `plugin "${plugin.name}" setup result`);
809
+ if (contribution.localization !== void 0) {
810
+ if (localization)
811
+ throw new RdxConfigError("Only one plugin may declare localization.");
812
+ localization = resolveLocalization(
813
+ contribution.localization,
814
+ config.lang,
815
+ rootDir,
816
+ command
817
+ );
818
+ }
819
+ if (contribution.runtimeFiles !== void 0) {
820
+ if (!Array.isArray(contribution.runtimeFiles))
821
+ throw new RdxConfigError(
822
+ `plugin "${plugin.name}" runtimeFiles must be an array.`
823
+ );
824
+ for (const value of contribution.runtimeFiles) {
825
+ const field = `plugin "${plugin.name}" runtimeFiles`;
826
+ const filename = text(value, field);
827
+ const resolved = resolveProjectPath(rootDir, filename, field);
828
+ runtimeFiles.add(path5.relative(rootDir, resolved));
829
+ }
830
+ }
831
+ if (contribution.sources === void 0) continue;
832
+ if (!Array.isArray(contribution.sources))
833
+ throw new RdxConfigError(
834
+ `plugin "${plugin.name}" sources must be an array.`
835
+ );
836
+ for (const value of contribution.sources) {
837
+ const source = object(value, `plugin "${plugin.name}" content source`);
838
+ if (["basePath", "fallbackDocsDir", "fallbackOnly", "localization"].some(
839
+ (key) => key in source
840
+ ))
841
+ throw new RdxConfigError(
842
+ "Content source basePath and fallbackDocsDir are reserved for internal locale views."
843
+ );
844
+ const id = text(source.id, "content source id");
845
+ const prefix = text(
846
+ source.routePrefix,
847
+ `content source ${id}.routePrefix`
848
+ );
849
+ text(source.label, `content source ${id}.label`);
850
+ const docsDir = text(source.docsDir, `content source ${id}.docsDir`);
851
+ resolveProjectPath(rootDir, docsDir, `content source ${id}.docsDir`);
852
+ if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(prefix) || prefix === "." || prefix === "..") {
853
+ throw new RdxConfigError(
854
+ `content source ${id}.routePrefix must be a single safe URL segment.`
855
+ );
856
+ }
857
+ if (sources.some((other) => other.id === id || other.routePrefix === prefix)) {
858
+ throw new RdxConfigError(
859
+ `duplicate content source id or route prefix: ${id}.`
860
+ );
861
+ }
862
+ if (existsSync(
863
+ /* turbopackIgnore: true */
864
+ path5.join(
865
+ /* turbopackIgnore: true */
866
+ docsRoot,
867
+ prefix
868
+ )
869
+ ) || existsSync(
870
+ /* turbopackIgnore: true */
871
+ path5.join(
872
+ /* turbopackIgnore: true */
873
+ docsRoot,
874
+ `${prefix}.mdx`
875
+ )
876
+ )) {
877
+ throw new RdxConfigError(
878
+ `content source ${id} conflicts with live documentation at /docs/${prefix}.`
879
+ );
880
+ }
881
+ if (source.badge !== void 0)
882
+ text(source.badge, `content source ${id}.badge`);
883
+ if (source.redirectTo !== void 0)
884
+ text(source.redirectTo, `content source ${id}.redirectTo`);
885
+ if (source.version !== void 0 && (typeof source.version !== "string" || !/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9][a-zA-Z0-9.-]*)?$/.test(
886
+ source.version
887
+ )))
888
+ throw new RdxConfigError(
889
+ `content source ${id}.version must be a safe snapshot label.`
890
+ );
891
+ if (source.previousSnapshot !== void 0) {
892
+ const previous = object(
893
+ source.previousSnapshot,
894
+ `content source ${id}.previousSnapshot`
895
+ );
896
+ keys(previous, ["version", "docsDir"], "previousSnapshot.");
897
+ if (typeof previous.version !== "string" || !/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9][a-zA-Z0-9.-]*)?$/.test(
898
+ previous.version
899
+ ))
900
+ throw new RdxConfigError(
901
+ "previousSnapshot.version must be a safe snapshot label."
902
+ );
903
+ resolveProjectPath(
904
+ rootDir,
905
+ text(previous.docsDir, "previousSnapshot.docsDir"),
906
+ "previousSnapshot.docsDir"
907
+ );
908
+ }
909
+ sources.push(source);
910
+ }
911
+ }
912
+ for (const source of sources) {
913
+ if (source.redirectTo === void 0) continue;
914
+ const target = sources.find(
915
+ (entry) => entry.routePrefix === source.redirectTo
916
+ );
917
+ if (!target || target === source || target.redirectTo !== void 0 || target.docsDir !== source.docsDir)
918
+ throw new RdxConfigError(
919
+ `content source ${source.id}.redirectTo must reference a canonical source with the same docsDir.`
920
+ );
921
+ }
922
+ if (localization)
923
+ localization = hydrateSourceLocalizations(
924
+ rootDir,
925
+ sources,
926
+ localization,
927
+ runtimeFiles
928
+ );
929
+ return {
930
+ rootDir,
931
+ config,
932
+ sources,
933
+ commands,
934
+ runtimeFiles: [...runtimeFiles],
935
+ ...localization ? { localization } : {}
936
+ };
937
+ }
938
+
939
+ export {
940
+ RdxConfigError,
941
+ normalizeLocalization,
942
+ resolveProjectPath,
943
+ collectSnapshotContributions,
944
+ resolveConfig,
945
+ defineConfig,
946
+ createRdxProject
947
+ };