@lexibase/rdx-core 1.0.0-beta.1 → 1.0.0-beta.3
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.
- package/dist/chunk-LEDAZNXX.js +252 -0
- package/dist/config/index.d.ts +15 -0
- package/dist/config/index.js +14 -0
- package/dist/index.d.ts +16 -10
- package/dist/index.js +75 -166
- package/package.json +11 -10
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// src/config/index.ts
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { existsSync } from "fs";
|
|
4
|
+
var RdxConfigError = class extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(`Invalid rdx.config.ts: ${message}`);
|
|
7
|
+
this.name = "RdxConfigError";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
function record(value) {
|
|
11
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
12
|
+
}
|
|
13
|
+
function object(value, field) {
|
|
14
|
+
if (!record(value)) throw new RdxConfigError(`${field} must be an object.`);
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
function text(value, field, allowEmpty = false) {
|
|
18
|
+
if (typeof value !== "string" || !allowEmpty && !value.trim()) {
|
|
19
|
+
throw new RdxConfigError(
|
|
20
|
+
`${field} must be ${allowEmpty ? "a string" : "a non-empty string"}.`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
function keys(value, allowed, field) {
|
|
26
|
+
for (const key of Object.keys(value)) {
|
|
27
|
+
if (!allowed.includes(key))
|
|
28
|
+
throw new RdxConfigError(`unknown option ${field}${key}.`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function resolveProjectPath(rootDir, value, field) {
|
|
32
|
+
text(value, field);
|
|
33
|
+
const resolved = path.resolve(rootDir, value);
|
|
34
|
+
const relative = path.relative(rootDir, resolved);
|
|
35
|
+
if (path.isAbsolute(value) || !relative || relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
|
36
|
+
throw new RdxConfigError(
|
|
37
|
+
`${field} must be a relative path inside the project.`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return resolved;
|
|
41
|
+
}
|
|
42
|
+
function resolveConfig(input) {
|
|
43
|
+
const config = object(input, "default export");
|
|
44
|
+
keys(
|
|
45
|
+
config,
|
|
46
|
+
[
|
|
47
|
+
"title",
|
|
48
|
+
"description",
|
|
49
|
+
"lang",
|
|
50
|
+
"docsDir",
|
|
51
|
+
"theme",
|
|
52
|
+
"navbar",
|
|
53
|
+
"plugins",
|
|
54
|
+
"homepage"
|
|
55
|
+
],
|
|
56
|
+
""
|
|
57
|
+
);
|
|
58
|
+
text(config.title, "title");
|
|
59
|
+
if (config.description !== void 0)
|
|
60
|
+
text(config.description, "description", true);
|
|
61
|
+
if (config.lang !== void 0) text(config.lang, "lang");
|
|
62
|
+
if (config.docsDir !== void 0) text(config.docsDir, "docsDir");
|
|
63
|
+
if (config.homepage !== void 0) {
|
|
64
|
+
const homepage = object(config.homepage, "homepage");
|
|
65
|
+
keys(homepage, ["enabled", "title", "description", "buttons"], "homepage.");
|
|
66
|
+
if (homepage.enabled !== void 0 && typeof homepage.enabled !== "boolean")
|
|
67
|
+
throw new RdxConfigError("homepage.enabled must be a boolean.");
|
|
68
|
+
if (homepage.title !== void 0) text(homepage.title, "homepage.title");
|
|
69
|
+
if (homepage.description !== void 0)
|
|
70
|
+
text(homepage.description, "homepage.description", true);
|
|
71
|
+
if (homepage.buttons !== void 0) {
|
|
72
|
+
if (!Array.isArray(homepage.buttons))
|
|
73
|
+
throw new RdxConfigError("homepage.buttons must be an array.");
|
|
74
|
+
for (const [index, value] of homepage.buttons.entries()) {
|
|
75
|
+
const field = `homepage.buttons[${index}]`;
|
|
76
|
+
const button = object(value, field);
|
|
77
|
+
keys(button, ["label", "href"], `${field}.`);
|
|
78
|
+
text(button.label, `${field}.label`);
|
|
79
|
+
const href = text(button.href, `${field}.href`);
|
|
80
|
+
if (/[\s\\]/.test(href) || !/^\/(?!\/)/.test(href) && !/^https?:\/\//i.test(href))
|
|
81
|
+
throw new RdxConfigError(
|
|
82
|
+
`${field}.href must be a root-relative path or http(s) URL.`
|
|
83
|
+
);
|
|
84
|
+
try {
|
|
85
|
+
new URL(href, "https://rdx.local");
|
|
86
|
+
} catch {
|
|
87
|
+
throw new RdxConfigError(`${field}.href must be a valid URL.`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (config.theme !== void 0) {
|
|
93
|
+
const theme = object(config.theme, "theme");
|
|
94
|
+
keys(theme, ["defaultTheme"], "theme.");
|
|
95
|
+
if (theme.defaultTheme !== void 0 && (typeof theme.defaultTheme !== "string" || !["system", "dark", "light"].includes(theme.defaultTheme))) {
|
|
96
|
+
throw new RdxConfigError(
|
|
97
|
+
"theme.defaultTheme must be system, dark, or light."
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (config.navbar !== void 0) {
|
|
102
|
+
const navbar = object(config.navbar, "navbar");
|
|
103
|
+
keys(navbar, ["title", "actions"], "navbar.");
|
|
104
|
+
if (navbar.title !== void 0) text(navbar.title, "navbar.title", true);
|
|
105
|
+
if (navbar.actions !== void 0) {
|
|
106
|
+
const actions = object(navbar.actions, "navbar.actions");
|
|
107
|
+
keys(actions, ["github"], "navbar.actions.");
|
|
108
|
+
if (actions.github !== void 0) {
|
|
109
|
+
const github = object(actions.github, "navbar.actions.github");
|
|
110
|
+
keys(github, ["visible", "url"], "navbar.actions.github.");
|
|
111
|
+
if (typeof github.visible !== "boolean")
|
|
112
|
+
throw new RdxConfigError(
|
|
113
|
+
"navbar.actions.github.visible must be a boolean."
|
|
114
|
+
);
|
|
115
|
+
const url = text(github.url, "navbar.actions.github.url");
|
|
116
|
+
if (!/^https?:\/\//i.test(url))
|
|
117
|
+
throw new RdxConfigError(
|
|
118
|
+
"navbar.actions.github.url must use http or https."
|
|
119
|
+
);
|
|
120
|
+
try {
|
|
121
|
+
new URL(url);
|
|
122
|
+
} catch {
|
|
123
|
+
throw new RdxConfigError(
|
|
124
|
+
"navbar.actions.github.url must be a valid URL."
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (config.plugins !== void 0 && !Array.isArray(config.plugins)) {
|
|
131
|
+
throw new RdxConfigError("plugins must be an array of plugin objects.");
|
|
132
|
+
}
|
|
133
|
+
const names = /* @__PURE__ */ new Set();
|
|
134
|
+
for (const [index, value] of (config.plugins ?? []).entries()) {
|
|
135
|
+
const plugin = object(
|
|
136
|
+
value,
|
|
137
|
+
`plugins[${index}] (call the plugin factory, e.g. versioning())`
|
|
138
|
+
);
|
|
139
|
+
const name = text(plugin.name, `plugins[${index}].name`);
|
|
140
|
+
if (names.has(name))
|
|
141
|
+
throw new RdxConfigError(`plugin "${name}" is configured more than once.`);
|
|
142
|
+
names.add(name);
|
|
143
|
+
if (plugin.apiVersion !== 1)
|
|
144
|
+
throw new RdxConfigError(`plugin "${name}" must support apiVersion 1.`);
|
|
145
|
+
if (plugin.setup !== void 0 && typeof plugin.setup !== "function")
|
|
146
|
+
throw new RdxConfigError(`plugin "${name}" setup must be a function.`);
|
|
147
|
+
if (plugin.commands !== void 0 && !Array.isArray(plugin.commands))
|
|
148
|
+
throw new RdxConfigError(`plugin "${name}" commands must be an array.`);
|
|
149
|
+
}
|
|
150
|
+
const typed = config;
|
|
151
|
+
return {
|
|
152
|
+
...typed,
|
|
153
|
+
description: typed.description ?? "",
|
|
154
|
+
lang: typed.lang ?? "en",
|
|
155
|
+
docsDir: typed.docsDir ?? "docs",
|
|
156
|
+
theme: { defaultTheme: typed.theme?.defaultTheme ?? "system" },
|
|
157
|
+
plugins: typed.plugins ?? []
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function defineConfig(config) {
|
|
161
|
+
resolveConfig(config);
|
|
162
|
+
return config;
|
|
163
|
+
}
|
|
164
|
+
async function createRdxProject(input, {
|
|
165
|
+
rootDir = process.cwd(),
|
|
166
|
+
command = "runtime"
|
|
167
|
+
} = {}) {
|
|
168
|
+
rootDir = path.resolve(rootDir);
|
|
169
|
+
const config = resolveConfig(input);
|
|
170
|
+
const docsRoot = resolveProjectPath(rootDir, config.docsDir, "docsDir");
|
|
171
|
+
const sources = [
|
|
172
|
+
{
|
|
173
|
+
id: "current",
|
|
174
|
+
label: "Current",
|
|
175
|
+
docsDir: config.docsDir,
|
|
176
|
+
routePrefix: ""
|
|
177
|
+
}
|
|
178
|
+
];
|
|
179
|
+
const commands = [];
|
|
180
|
+
const commandNames = /* @__PURE__ */ new Set(["dev", "build", "start", "prepare", "help"]);
|
|
181
|
+
for (const plugin of config.plugins) {
|
|
182
|
+
for (const value of plugin.commands ?? []) {
|
|
183
|
+
const entry = object(value, `plugin "${plugin.name}" command`);
|
|
184
|
+
const name = text(entry.name, `plugin "${plugin.name}" command.name`);
|
|
185
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name) || commandNames.has(name)) {
|
|
186
|
+
throw new RdxConfigError(
|
|
187
|
+
`plugin "${plugin.name}" command "${name}" is invalid or already registered.`
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
text(entry.description, `command ${name}.description`);
|
|
191
|
+
if (typeof entry.run !== "function")
|
|
192
|
+
throw new RdxConfigError(`command ${name}.run must be a function.`);
|
|
193
|
+
commandNames.add(name);
|
|
194
|
+
commands.push(value);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
for (const plugin of config.plugins) {
|
|
198
|
+
let result;
|
|
199
|
+
try {
|
|
200
|
+
result = await plugin.setup?.({ rootDir, command, config });
|
|
201
|
+
} catch (error) {
|
|
202
|
+
throw new Error(
|
|
203
|
+
`[${plugin.name}] ${error instanceof Error ? error.message : String(error)}`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
if (result === void 0) continue;
|
|
207
|
+
const contribution = object(result, `plugin "${plugin.name}" setup result`);
|
|
208
|
+
if (contribution.sources === void 0) continue;
|
|
209
|
+
if (!Array.isArray(contribution.sources))
|
|
210
|
+
throw new RdxConfigError(
|
|
211
|
+
`plugin "${plugin.name}" sources must be an array.`
|
|
212
|
+
);
|
|
213
|
+
for (const value of contribution.sources) {
|
|
214
|
+
const source = object(value, `plugin "${plugin.name}" content source`);
|
|
215
|
+
const id = text(source.id, "content source id");
|
|
216
|
+
const prefix = text(
|
|
217
|
+
source.routePrefix,
|
|
218
|
+
`content source ${id}.routePrefix`
|
|
219
|
+
);
|
|
220
|
+
text(source.label, `content source ${id}.label`);
|
|
221
|
+
const docsDir = text(source.docsDir, `content source ${id}.docsDir`);
|
|
222
|
+
resolveProjectPath(rootDir, docsDir, `content source ${id}.docsDir`);
|
|
223
|
+
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(prefix) || prefix === "." || prefix === "..") {
|
|
224
|
+
throw new RdxConfigError(
|
|
225
|
+
`content source ${id}.routePrefix must be a single safe URL segment.`
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
if (sources.some((other) => other.id === id || other.routePrefix === prefix)) {
|
|
229
|
+
throw new RdxConfigError(
|
|
230
|
+
`duplicate content source id or route prefix: ${id}.`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
if (existsSync(path.join(docsRoot, prefix)) || existsSync(path.join(docsRoot, `${prefix}.mdx`))) {
|
|
234
|
+
throw new RdxConfigError(
|
|
235
|
+
`content source ${id} conflicts with live documentation at /docs/${prefix}.`
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
if (source.badge !== void 0)
|
|
239
|
+
text(source.badge, `content source ${id}.badge`);
|
|
240
|
+
sources.push(source);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return { rootDir, config, sources, commands };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export {
|
|
247
|
+
RdxConfigError,
|
|
248
|
+
resolveProjectPath,
|
|
249
|
+
resolveConfig,
|
|
250
|
+
defineConfig,
|
|
251
|
+
createRdxProject
|
|
252
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RdxProject, RdxConfig, ResolvedRdxConfig } from '@lexibase/rdx-types';
|
|
2
|
+
|
|
3
|
+
declare class RdxConfigError extends Error {
|
|
4
|
+
constructor(message: string);
|
|
5
|
+
}
|
|
6
|
+
/** Resolve project-owned paths without allowing traversal or filesystem roots. */
|
|
7
|
+
declare function resolveProjectPath(rootDir: string, value: string, field: string): string;
|
|
8
|
+
declare function resolveConfig(input: unknown): ResolvedRdxConfig;
|
|
9
|
+
declare function defineConfig(config: RdxConfig): RdxConfig;
|
|
10
|
+
declare function createRdxProject(input: unknown, { rootDir, command, }?: {
|
|
11
|
+
rootDir?: string;
|
|
12
|
+
command?: string;
|
|
13
|
+
}): Promise<RdxProject>;
|
|
14
|
+
|
|
15
|
+
export { RdxConfigError, createRdxProject, defineConfig, resolveConfig, resolveProjectPath };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
declare function docsIndexer(version?: string): DocCategory[];
|
|
5
|
-
|
|
6
|
-
declare function mdxLoader({ filename, version, versionsRaw, mdxComponents, docsRoot, }: MDXLoader): Promise<{
|
|
7
|
-
content: react.ReactElement<unknown, string | react.JSXElementConstructor<any>>;
|
|
8
|
-
frontmatter: Record<string, unknown>;
|
|
9
|
-
}>;
|
|
1
|
+
import * as _lexibase_rdx_types from '@lexibase/rdx-types';
|
|
2
|
+
import { RdxDocsOptions, RdxDocCategory, RdxDocument, RdxProject, RdxContentSource } from '@lexibase/rdx-types';
|
|
3
|
+
export { RdxConfigError, createRdxProject, defineConfig, resolveConfig, resolveProjectPath } from './config/index.js';
|
|
10
4
|
|
|
11
5
|
/**
|
|
12
6
|
* Reads the documentation tree from the user's project. The URL slug always
|
|
@@ -17,4 +11,16 @@ declare function getDocsIndex({ docsDir, }?: RdxDocsOptions): RdxDocCategory[];
|
|
|
17
11
|
declare function getDocsPaths(options?: RdxDocsOptions): string[];
|
|
18
12
|
declare function getDocument(slug: string, { docsDir }?: RdxDocsOptions): RdxDocument | null;
|
|
19
13
|
|
|
20
|
-
|
|
14
|
+
declare function getSourceOptions(project: RdxProject, source: RdxContentSource): {
|
|
15
|
+
docsDir: string;
|
|
16
|
+
};
|
|
17
|
+
/** Match plugin routes before looking up pages in the live documentation. */
|
|
18
|
+
declare function resolveDocsRoute(project: RdxProject, segments?: string[]): {
|
|
19
|
+
source: RdxContentSource;
|
|
20
|
+
slug: string | undefined;
|
|
21
|
+
document: _lexibase_rdx_types.RdxDocument | null;
|
|
22
|
+
categories: _lexibase_rdx_types.RdxDocCategory[];
|
|
23
|
+
};
|
|
24
|
+
declare function getProjectDocsPaths(project: RdxProject): string[];
|
|
25
|
+
|
|
26
|
+
export { getDocsIndex, getDocsPaths, getDocument, getProjectDocsPaths, getSourceOptions, resolveDocsRoute };
|
package/dist/index.js
CHANGED
|
@@ -1,154 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
import {
|
|
2
|
+
RdxConfigError,
|
|
3
|
+
createRdxProject,
|
|
4
|
+
defineConfig,
|
|
5
|
+
resolveConfig,
|
|
6
|
+
resolveProjectPath
|
|
7
|
+
} from "./chunk-LEDAZNXX.js";
|
|
8
|
+
|
|
9
|
+
// src/content/index.ts
|
|
2
10
|
import fs from "fs";
|
|
3
11
|
import path from "path";
|
|
4
12
|
import matter from "gray-matter";
|
|
5
|
-
function docsIndexer(version) {
|
|
6
|
-
const resolvedVersion = version || "canary";
|
|
7
|
-
let basePath;
|
|
8
|
-
if (resolvedVersion === "canary") {
|
|
9
|
-
basePath = path.join(process.cwd(), "app/docs/(canary)");
|
|
10
|
-
} else {
|
|
11
|
-
const versionedPath = path.join(
|
|
12
|
-
process.cwd(),
|
|
13
|
-
"app/docs/(versioned)",
|
|
14
|
-
`version-${resolvedVersion}`
|
|
15
|
-
);
|
|
16
|
-
const archivedPath = path.join(
|
|
17
|
-
process.cwd(),
|
|
18
|
-
"app/docs/(archived)",
|
|
19
|
-
`version-${resolvedVersion}`
|
|
20
|
-
);
|
|
21
|
-
basePath = fs.existsSync(versionedPath) ? versionedPath : archivedPath;
|
|
22
|
-
}
|
|
23
|
-
if (!fs.existsSync(basePath)) {
|
|
24
|
-
console.warn(`Docs Folder not found: ${basePath}`);
|
|
25
|
-
return [];
|
|
26
|
-
}
|
|
27
|
-
const categories = [];
|
|
28
|
-
const entries = fs.readdirSync(basePath, { withFileTypes: true });
|
|
29
|
-
entries.forEach((entry) => {
|
|
30
|
-
if (!entry.isDirectory()) {
|
|
31
|
-
if (entry.name.endsWith(".mdx")) {
|
|
32
|
-
throw new Error(
|
|
33
|
-
`File '${entry.name}' detected inside "${basePath}".
|
|
34
|
-
Please move it into a folder`
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
const categoryPath = path.join(basePath, entry.name);
|
|
39
|
-
const hasMdxFile = fs.readdirSync(categoryPath).some((file) => file.endsWith(".mdx"));
|
|
40
|
-
if (!hasMdxFile) return;
|
|
41
|
-
const categoryMetaPath = path.join(categoryPath, "_category.json");
|
|
42
|
-
let categoryTitle = entry.name;
|
|
43
|
-
let categoryPosition = 999;
|
|
44
|
-
if (fs.existsSync(categoryMetaPath)) {
|
|
45
|
-
try {
|
|
46
|
-
const rawMeta = fs.readFileSync(categoryMetaPath, "utf-8");
|
|
47
|
-
const meta = JSON.parse(rawMeta);
|
|
48
|
-
categoryTitle = meta.title || categoryTitle;
|
|
49
|
-
categoryPosition = meta.sidebar_position ?? categoryPosition;
|
|
50
|
-
} catch (err) {
|
|
51
|
-
console.warn(`Error reading _category.json em ${entry.name}:`, err);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
const files = fs.readdirSync(categoryPath);
|
|
55
|
-
const links = files.filter((file) => file.endsWith(".mdx")).map((file) => {
|
|
56
|
-
const filePath = path.join(categoryPath, file);
|
|
57
|
-
const raw = fs.readFileSync(filePath, "utf-8");
|
|
58
|
-
const { data } = matter(raw);
|
|
59
|
-
const filename = file.replace(".mdx", "");
|
|
60
|
-
const slug = resolvedVersion === "canary" ? `/docs/canary/${filename}` : `/docs/${resolvedVersion}/${filename}`;
|
|
61
|
-
return {
|
|
62
|
-
href: slug,
|
|
63
|
-
label: data.title || filename,
|
|
64
|
-
description: data.description || "",
|
|
65
|
-
position: data.link_position ?? 1
|
|
66
|
-
};
|
|
67
|
-
}).sort((a, b) => a.position - b.position).map(({ position: _position, ...link }) => link);
|
|
68
|
-
categories.push({
|
|
69
|
-
title: categoryTitle,
|
|
70
|
-
links,
|
|
71
|
-
position: categoryPosition
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
return categories.sort((a, b) => a.position - b.position).map(({ position: _position, ...category }) => category);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// src/loaders/mdx-loader/index.ts
|
|
78
|
-
import fs2 from "fs";
|
|
79
|
-
import path2 from "path";
|
|
80
|
-
import { compileMDX } from "next-mdx-remote/rsc";
|
|
81
|
-
async function mdxLoader({
|
|
82
|
-
filename,
|
|
83
|
-
version,
|
|
84
|
-
versionsRaw,
|
|
85
|
-
mdxComponents,
|
|
86
|
-
docsRoot = "app/docs"
|
|
87
|
-
}) {
|
|
88
|
-
const defaultVersion = versionsRaw.active[0];
|
|
89
|
-
if (!defaultVersion) {
|
|
90
|
-
throw new Error("No static versions provided");
|
|
91
|
-
}
|
|
92
|
-
let basePath;
|
|
93
|
-
if (version === "canary") {
|
|
94
|
-
basePath = path2.join(process.cwd(), docsRoot, "(canary)");
|
|
95
|
-
} else if (version) {
|
|
96
|
-
const versionedPath = path2.join(
|
|
97
|
-
process.cwd(),
|
|
98
|
-
docsRoot,
|
|
99
|
-
"(versioned)",
|
|
100
|
-
`version-${version}`
|
|
101
|
-
);
|
|
102
|
-
const archivedPath = path2.join(
|
|
103
|
-
process.cwd(),
|
|
104
|
-
docsRoot,
|
|
105
|
-
"(archived)",
|
|
106
|
-
`version-${version}`
|
|
107
|
-
);
|
|
108
|
-
basePath = fs2.existsSync(versionedPath) ? versionedPath : archivedPath;
|
|
109
|
-
} else {
|
|
110
|
-
basePath = path2.join(
|
|
111
|
-
process.cwd(),
|
|
112
|
-
docsRoot,
|
|
113
|
-
"(versioned)",
|
|
114
|
-
`version-${defaultVersion}`
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
const findFile = (dir) => {
|
|
118
|
-
const entries = fs2.readdirSync(dir, { withFileTypes: true });
|
|
119
|
-
for (const entry of entries) {
|
|
120
|
-
const fullPath = path2.join(dir, entry.name);
|
|
121
|
-
if (entry.isDirectory()) {
|
|
122
|
-
const found = findFile(fullPath);
|
|
123
|
-
if (found) return found;
|
|
124
|
-
}
|
|
125
|
-
if (entry.isFile() && entry.name === `${filename}.mdx`) {
|
|
126
|
-
return fullPath;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return null;
|
|
130
|
-
};
|
|
131
|
-
const filePath = findFile(basePath);
|
|
132
|
-
if (!filePath) {
|
|
133
|
-
throw new Error(`MDX File not found: ${filename}`);
|
|
134
|
-
}
|
|
135
|
-
const source = fs2.readFileSync(filePath, "utf8");
|
|
136
|
-
const { content, frontmatter } = await compileMDX({
|
|
137
|
-
source,
|
|
138
|
-
components: mdxComponents,
|
|
139
|
-
options: { parseFrontmatter: true }
|
|
140
|
-
});
|
|
141
|
-
return { content, frontmatter };
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// src/content/index.ts
|
|
145
|
-
import fs3 from "fs";
|
|
146
|
-
import path3 from "path";
|
|
147
|
-
import matter2 from "gray-matter";
|
|
148
13
|
var MDX_EXTENSION = ".mdx";
|
|
149
14
|
var DEFAULT_POSITION = Number.MAX_SAFE_INTEGER;
|
|
150
15
|
function getDocsRoot(docsDir = "docs") {
|
|
151
|
-
return
|
|
16
|
+
return path.resolve(process.cwd(), docsDir);
|
|
152
17
|
}
|
|
153
18
|
function toTitle(value) {
|
|
154
19
|
return value.replace(/[-_]/g, " ").replace(/\b\w/g, (character) => character.toUpperCase());
|
|
@@ -163,12 +28,12 @@ function numberValue(value, fallback = DEFAULT_POSITION) {
|
|
|
163
28
|
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
164
29
|
}
|
|
165
30
|
function readCategoryMeta(categoryPath, fallbackTitle) {
|
|
166
|
-
const metaPath =
|
|
167
|
-
if (!
|
|
31
|
+
const metaPath = path.join(categoryPath, "_category.json");
|
|
32
|
+
if (!fs.existsSync(metaPath)) {
|
|
168
33
|
return { title: fallbackTitle, position: DEFAULT_POSITION };
|
|
169
34
|
}
|
|
170
35
|
try {
|
|
171
|
-
const parsed = JSON.parse(
|
|
36
|
+
const parsed = JSON.parse(fs.readFileSync(metaPath, "utf8"));
|
|
172
37
|
if (!isRecord(parsed)) {
|
|
173
38
|
return { title: fallbackTitle, position: DEFAULT_POSITION };
|
|
174
39
|
}
|
|
@@ -181,32 +46,32 @@ function readCategoryMeta(categoryPath, fallbackTitle) {
|
|
|
181
46
|
}
|
|
182
47
|
}
|
|
183
48
|
function findMdxFiles(directory) {
|
|
184
|
-
const entries =
|
|
49
|
+
const entries = fs.readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
|
|
185
50
|
return entries.flatMap((entry) => {
|
|
186
|
-
const entryPath =
|
|
51
|
+
const entryPath = path.join(directory, entry.name);
|
|
187
52
|
if (entry.isDirectory()) return findMdxFiles(entryPath);
|
|
188
53
|
if (entry.isFile() && entry.name.endsWith(MDX_EXTENSION)) return [entryPath];
|
|
189
54
|
return [];
|
|
190
55
|
});
|
|
191
56
|
}
|
|
192
57
|
function slugFromRelativePath(relativePath) {
|
|
193
|
-
return relativePath.split(
|
|
58
|
+
return relativePath.split(path.sep).join("/").replace(/\.mdx$/, "");
|
|
194
59
|
}
|
|
195
60
|
function isSafeSlug(slug) {
|
|
196
|
-
if (
|
|
61
|
+
if (path.isAbsolute(slug) || slug.includes("\\")) return false;
|
|
197
62
|
return slug.split("/").every((segment) => segment && segment !== "." && segment !== "..");
|
|
198
63
|
}
|
|
199
64
|
function getDocsIndex({
|
|
200
65
|
docsDir
|
|
201
66
|
} = {}) {
|
|
202
67
|
const docsRoot = getDocsRoot(docsDir);
|
|
203
|
-
if (!
|
|
68
|
+
if (!fs.existsSync(docsRoot)) return [];
|
|
204
69
|
const categories = /* @__PURE__ */ new Map();
|
|
205
70
|
for (const filePath of findMdxFiles(docsRoot)) {
|
|
206
|
-
const relativePath =
|
|
207
|
-
const pathParts = relativePath.split(
|
|
71
|
+
const relativePath = path.relative(docsRoot, filePath);
|
|
72
|
+
const pathParts = relativePath.split(path.sep);
|
|
208
73
|
const categoryId = pathParts.length > 1 ? pathParts[0] ?? "_root" : "_root";
|
|
209
|
-
const categoryPath = categoryId === "_root" ? docsRoot :
|
|
74
|
+
const categoryPath = categoryId === "_root" ? docsRoot : path.join(docsRoot, categoryId);
|
|
210
75
|
const fallbackCategoryTitle = categoryId === "_root" ? "Overview" : toTitle(categoryId);
|
|
211
76
|
let category = categories.get(categoryId);
|
|
212
77
|
if (!category) {
|
|
@@ -221,11 +86,11 @@ function getDocsIndex({
|
|
|
221
86
|
categories.set(categoryId, createdCategory);
|
|
222
87
|
category = createdCategory;
|
|
223
88
|
}
|
|
224
|
-
const source =
|
|
225
|
-
const parsed =
|
|
89
|
+
const source = fs.readFileSync(filePath, "utf8");
|
|
90
|
+
const parsed = matter(source);
|
|
226
91
|
const frontmatter = isRecord(parsed.data) ? parsed.data : {};
|
|
227
92
|
const slug = slugFromRelativePath(relativePath);
|
|
228
|
-
const fallbackTitle = toTitle(
|
|
93
|
+
const fallbackTitle = toTitle(path.basename(slug));
|
|
229
94
|
category.pages.push({
|
|
230
95
|
slug,
|
|
231
96
|
title: stringValue(frontmatter.title, fallbackTitle),
|
|
@@ -251,12 +116,12 @@ function getDocsPaths(options) {
|
|
|
251
116
|
function getDocument(slug, { docsDir } = {}) {
|
|
252
117
|
if (!isSafeSlug(slug)) return null;
|
|
253
118
|
const docsRoot = getDocsRoot(docsDir);
|
|
254
|
-
const sourcePath =
|
|
255
|
-
const relativePath =
|
|
256
|
-
if (relativePath.startsWith("..") ||
|
|
119
|
+
const sourcePath = path.resolve(docsRoot, `${slug}${MDX_EXTENSION}`);
|
|
120
|
+
const relativePath = path.relative(docsRoot, sourcePath);
|
|
121
|
+
if (relativePath.startsWith("..") || path.isAbsolute(relativePath))
|
|
257
122
|
return null;
|
|
258
|
-
if (!
|
|
259
|
-
const parsed =
|
|
123
|
+
if (!fs.existsSync(sourcePath)) return null;
|
|
124
|
+
const parsed = matter(fs.readFileSync(sourcePath, "utf8"));
|
|
260
125
|
return {
|
|
261
126
|
slug,
|
|
262
127
|
sourcePath,
|
|
@@ -264,10 +129,54 @@ function getDocument(slug, { docsDir } = {}) {
|
|
|
264
129
|
frontmatter: isRecord(parsed.data) ? parsed.data : {}
|
|
265
130
|
};
|
|
266
131
|
}
|
|
132
|
+
|
|
133
|
+
// src/project/index.ts
|
|
134
|
+
import path2 from "path";
|
|
135
|
+
function getSourceOptions(project, source) {
|
|
136
|
+
return { docsDir: path2.resolve(project.rootDir, source.docsDir) };
|
|
137
|
+
}
|
|
138
|
+
function resolveDocsRoute(project, segments = []) {
|
|
139
|
+
const source = project.sources.find(
|
|
140
|
+
(entry) => entry.routePrefix && entry.routePrefix === segments[0]
|
|
141
|
+
) ?? project.sources[0];
|
|
142
|
+
const options = getSourceOptions(project, source);
|
|
143
|
+
const slug = (source.routePrefix ? segments.slice(1) : segments).join("/") || getDocsPaths(options)[0];
|
|
144
|
+
return {
|
|
145
|
+
source,
|
|
146
|
+
slug,
|
|
147
|
+
document: slug ? getDocument(slug, options) : null,
|
|
148
|
+
categories: getDocsIndex(options)
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function getProjectDocsPaths(project) {
|
|
152
|
+
const paths = /* @__PURE__ */ new Set();
|
|
153
|
+
for (const source of project.sources) {
|
|
154
|
+
for (const slug of getDocsPaths(getSourceOptions(project, source))) {
|
|
155
|
+
const route = [source.routePrefix, slug].filter(Boolean).join("/");
|
|
156
|
+
if (paths.has(route))
|
|
157
|
+
throw new Error(`Conflicting documentation route: /docs/${route}`);
|
|
158
|
+
if (!source.routePrefix && project.sources.some(
|
|
159
|
+
(other) => other.routePrefix === slug.split("/")[0]
|
|
160
|
+
)) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
`Live documentation route /docs/${slug} conflicts with a plugin content source.`
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
paths.add(route);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return [...paths];
|
|
169
|
+
}
|
|
267
170
|
export {
|
|
268
|
-
|
|
171
|
+
RdxConfigError,
|
|
172
|
+
createRdxProject,
|
|
173
|
+
defineConfig,
|
|
269
174
|
getDocsIndex,
|
|
270
175
|
getDocsPaths,
|
|
271
176
|
getDocument,
|
|
272
|
-
|
|
177
|
+
getProjectDocsPaths,
|
|
178
|
+
getSourceOptions,
|
|
179
|
+
resolveConfig,
|
|
180
|
+
resolveDocsRoute,
|
|
181
|
+
resolveProjectPath
|
|
273
182
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lexibase/rdx-core",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.3",
|
|
4
4
|
"description": "Content indexing and document utilities for RDX documentation projects.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"import": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./config": {
|
|
15
|
+
"types": "./dist/config/index.d.ts",
|
|
16
|
+
"import": "./dist/config/index.js"
|
|
13
17
|
}
|
|
14
18
|
},
|
|
15
19
|
"files": [
|
|
@@ -30,20 +34,17 @@
|
|
|
30
34
|
"homepage": "https://github.com/lexibase/rdx#readme",
|
|
31
35
|
"dependencies": {
|
|
32
36
|
"gray-matter": "^4.0.3",
|
|
33
|
-
"
|
|
34
|
-
"@lexibase/rdx-types": "1.0.0-beta.1"
|
|
35
|
-
},
|
|
36
|
-
"peerDependencies": {
|
|
37
|
-
"react": "^19.1.0"
|
|
37
|
+
"@lexibase/rdx-types": "1.0.0-beta.3"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/node": "^22.15.3",
|
|
41
41
|
"@types/react": "19.1.0",
|
|
42
|
-
"@rdx/
|
|
43
|
-
"@rdx/
|
|
42
|
+
"@rdx/eslint-config": "1.0.0",
|
|
43
|
+
"@rdx/typescript-config": "1.0.0"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
|
-
"build": "tsup src/index.ts --format esm --dts --clean --external @lexibase/rdx-types --external gray-matter
|
|
47
|
-
"check-types": "tsc --noEmit"
|
|
46
|
+
"build": "tsup src/index.ts src/config/index.ts --format esm --dts --clean --external @lexibase/rdx-types --external gray-matter",
|
|
47
|
+
"check-types": "tsc --noEmit",
|
|
48
|
+
"test": "node --test tests/*.test.mjs"
|
|
48
49
|
}
|
|
49
50
|
}
|