@lexibase/rdx-core 0.0.0-stage → 1.0.0-beta.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.
- package/LICENSE +8 -0
- package/README.md +2 -2
- package/dist/index.d.ts +20 -0
- package/dist/index.js +273 -0
- package/package.json +46 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright © 2025 Lexibase
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@rdx/rdx-core`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Content RDX core.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { DocCategory, MDXLoader, RdxDocsOptions, RdxDocCategory, RdxDocument } from '@lexibase/rdx-types';
|
|
2
|
+
import * as react from 'react';
|
|
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
|
+
}>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Reads the documentation tree from the user's project. The URL slug always
|
|
13
|
+
* preserves the full path, so `guides/intro.mdx` and `api/intro.mdx` never
|
|
14
|
+
* collide.
|
|
15
|
+
*/
|
|
16
|
+
declare function getDocsIndex({ docsDir, }?: RdxDocsOptions): RdxDocCategory[];
|
|
17
|
+
declare function getDocsPaths(options?: RdxDocsOptions): string[];
|
|
18
|
+
declare function getDocument(slug: string, { docsDir }?: RdxDocsOptions): RdxDocument | null;
|
|
19
|
+
|
|
20
|
+
export { docsIndexer, getDocsIndex, getDocsPaths, getDocument, mdxLoader };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// src/loaders/docs-indexer/index.ts
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
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
|
+
var MDX_EXTENSION = ".mdx";
|
|
149
|
+
var DEFAULT_POSITION = Number.MAX_SAFE_INTEGER;
|
|
150
|
+
function getDocsRoot(docsDir = "docs") {
|
|
151
|
+
return path3.resolve(process.cwd(), docsDir);
|
|
152
|
+
}
|
|
153
|
+
function toTitle(value) {
|
|
154
|
+
return value.replace(/[-_]/g, " ").replace(/\b\w/g, (character) => character.toUpperCase());
|
|
155
|
+
}
|
|
156
|
+
function isRecord(value) {
|
|
157
|
+
return typeof value === "object" && value !== null;
|
|
158
|
+
}
|
|
159
|
+
function stringValue(value, fallback) {
|
|
160
|
+
return typeof value === "string" && value.trim() ? value : fallback;
|
|
161
|
+
}
|
|
162
|
+
function numberValue(value, fallback = DEFAULT_POSITION) {
|
|
163
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
164
|
+
}
|
|
165
|
+
function readCategoryMeta(categoryPath, fallbackTitle) {
|
|
166
|
+
const metaPath = path3.join(categoryPath, "_category.json");
|
|
167
|
+
if (!fs3.existsSync(metaPath)) {
|
|
168
|
+
return { title: fallbackTitle, position: DEFAULT_POSITION };
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
const parsed = JSON.parse(fs3.readFileSync(metaPath, "utf8"));
|
|
172
|
+
if (!isRecord(parsed)) {
|
|
173
|
+
return { title: fallbackTitle, position: DEFAULT_POSITION };
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
title: stringValue(parsed.title, fallbackTitle),
|
|
177
|
+
position: numberValue(parsed.sidebar_position)
|
|
178
|
+
};
|
|
179
|
+
} catch {
|
|
180
|
+
return { title: fallbackTitle, position: DEFAULT_POSITION };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function findMdxFiles(directory) {
|
|
184
|
+
const entries = fs3.readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
|
|
185
|
+
return entries.flatMap((entry) => {
|
|
186
|
+
const entryPath = path3.join(directory, entry.name);
|
|
187
|
+
if (entry.isDirectory()) return findMdxFiles(entryPath);
|
|
188
|
+
if (entry.isFile() && entry.name.endsWith(MDX_EXTENSION)) return [entryPath];
|
|
189
|
+
return [];
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
function slugFromRelativePath(relativePath) {
|
|
193
|
+
return relativePath.split(path3.sep).join("/").replace(/\.mdx$/, "");
|
|
194
|
+
}
|
|
195
|
+
function isSafeSlug(slug) {
|
|
196
|
+
if (path3.isAbsolute(slug) || slug.includes("\\")) return false;
|
|
197
|
+
return slug.split("/").every((segment) => segment && segment !== "." && segment !== "..");
|
|
198
|
+
}
|
|
199
|
+
function getDocsIndex({
|
|
200
|
+
docsDir
|
|
201
|
+
} = {}) {
|
|
202
|
+
const docsRoot = getDocsRoot(docsDir);
|
|
203
|
+
if (!fs3.existsSync(docsRoot)) return [];
|
|
204
|
+
const categories = /* @__PURE__ */ new Map();
|
|
205
|
+
for (const filePath of findMdxFiles(docsRoot)) {
|
|
206
|
+
const relativePath = path3.relative(docsRoot, filePath);
|
|
207
|
+
const pathParts = relativePath.split(path3.sep);
|
|
208
|
+
const categoryId = pathParts.length > 1 ? pathParts[0] ?? "_root" : "_root";
|
|
209
|
+
const categoryPath = categoryId === "_root" ? docsRoot : path3.join(docsRoot, categoryId);
|
|
210
|
+
const fallbackCategoryTitle = categoryId === "_root" ? "Overview" : toTitle(categoryId);
|
|
211
|
+
let category = categories.get(categoryId);
|
|
212
|
+
if (!category) {
|
|
213
|
+
const meta = readCategoryMeta(categoryPath, fallbackCategoryTitle);
|
|
214
|
+
const createdCategory = {
|
|
215
|
+
id: categoryId,
|
|
216
|
+
title: meta.title,
|
|
217
|
+
position: meta.position,
|
|
218
|
+
pages: [],
|
|
219
|
+
categoryPath
|
|
220
|
+
};
|
|
221
|
+
categories.set(categoryId, createdCategory);
|
|
222
|
+
category = createdCategory;
|
|
223
|
+
}
|
|
224
|
+
const source = fs3.readFileSync(filePath, "utf8");
|
|
225
|
+
const parsed = matter2(source);
|
|
226
|
+
const frontmatter = isRecord(parsed.data) ? parsed.data : {};
|
|
227
|
+
const slug = slugFromRelativePath(relativePath);
|
|
228
|
+
const fallbackTitle = toTitle(path3.basename(slug));
|
|
229
|
+
category.pages.push({
|
|
230
|
+
slug,
|
|
231
|
+
title: stringValue(frontmatter.title, fallbackTitle),
|
|
232
|
+
description: stringValue(frontmatter.description, ""),
|
|
233
|
+
position: numberValue(frontmatter.link_position),
|
|
234
|
+
sourcePath: filePath
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
return [...categories.values()].map(({ categoryPath: _categoryPath, ...category }) => ({
|
|
238
|
+
...category,
|
|
239
|
+
pages: category.pages.sort(
|
|
240
|
+
(first, second) => first.position - second.position || first.slug.localeCompare(second.slug)
|
|
241
|
+
)
|
|
242
|
+
})).sort(
|
|
243
|
+
(first, second) => first.position - second.position || first.title.localeCompare(second.title)
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
function getDocsPaths(options) {
|
|
247
|
+
return getDocsIndex(options).flatMap(
|
|
248
|
+
(category) => category.pages.map((page) => page.slug)
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
function getDocument(slug, { docsDir } = {}) {
|
|
252
|
+
if (!isSafeSlug(slug)) return null;
|
|
253
|
+
const docsRoot = getDocsRoot(docsDir);
|
|
254
|
+
const sourcePath = path3.resolve(docsRoot, `${slug}${MDX_EXTENSION}`);
|
|
255
|
+
const relativePath = path3.relative(docsRoot, sourcePath);
|
|
256
|
+
if (relativePath.startsWith("..") || path3.isAbsolute(relativePath))
|
|
257
|
+
return null;
|
|
258
|
+
if (!fs3.existsSync(sourcePath)) return null;
|
|
259
|
+
const parsed = matter2(fs3.readFileSync(sourcePath, "utf8"));
|
|
260
|
+
return {
|
|
261
|
+
slug,
|
|
262
|
+
sourcePath,
|
|
263
|
+
content: parsed.content,
|
|
264
|
+
frontmatter: isRecord(parsed.data) ? parsed.data : {}
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
export {
|
|
268
|
+
docsIndexer,
|
|
269
|
+
getDocsIndex,
|
|
270
|
+
getDocsPaths,
|
|
271
|
+
getDocument,
|
|
272
|
+
mdxLoader
|
|
273
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lexibase/rdx-core",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
|
+
"description": "Content indexing and document utilities for RDX documentation projects.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/lexibase/rdx.git",
|
|
25
|
+
"directory": "packages/rdx-core"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/lexibase/rdx/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/lexibase/rdx#readme",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"gray-matter": "^4.0.3",
|
|
33
|
+
"next-mdx-remote": "^5.0.0",
|
|
34
|
+
"@lexibase/rdx-types": "1.0.0-beta.1"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"react": "^19.1.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^22.15.3",
|
|
41
|
+
"@types/react": "19.1.0",
|
|
42
|
+
"@rdx/typescript-config": "1.0.0",
|
|
43
|
+
"@rdx/eslint-config": "1.0.0"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsup src/index.ts --format esm --dts --clean --external @lexibase/rdx-types --external gray-matter --external next-mdx-remote",
|
|
47
|
+
"check-types": "tsc --noEmit"
|
|
48
|
+
}
|
|
6
49
|
}
|