@lexibase/rdx-core 1.0.0-beta.5 → 1.0.0-beta.7

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.
@@ -61,11 +61,17 @@ function resolveConfig(input) {
61
61
  "theme",
62
62
  "navbar",
63
63
  "plugins",
64
- "homepage"
64
+ "homepage",
65
+ "TOC",
66
+ "search"
65
67
  ],
66
68
  ""
67
69
  );
68
70
  text(config.title, "title");
71
+ if (config.search !== void 0 && typeof config.search !== "boolean")
72
+ throw new RdxConfigError("search must be a boolean.");
73
+ if (config.TOC !== void 0 && typeof config.TOC !== "boolean")
74
+ throw new RdxConfigError("TOC must be a boolean.");
69
75
  if (config.description !== void 0)
70
76
  text(config.description, "description", true);
71
77
  if (config.lang !== void 0) text(config.lang, "lang");
@@ -171,6 +177,8 @@ function resolveConfig(input) {
171
177
  const typed = config;
172
178
  return {
173
179
  ...typed,
180
+ TOC: typed.TOC ?? false,
181
+ search: typed.search ?? false,
174
182
  description: typed.description ?? "",
175
183
  lang: typed.lang ?? "en",
176
184
  docsDir: typed.docsDir ?? "docs",
@@ -198,7 +206,14 @@ async function createRdxProject(input, {
198
206
  }
199
207
  ];
200
208
  const commands = [];
201
- const commandNames = /* @__PURE__ */ new Set(["dev", "build", "start", "prepare", "help"]);
209
+ const commandNames = /* @__PURE__ */ new Set([
210
+ "dev",
211
+ "build",
212
+ "start",
213
+ "prepare",
214
+ "help",
215
+ "add"
216
+ ]);
202
217
  for (const plugin of config.plugins) {
203
218
  for (const value of plugin.commands ?? []) {
204
219
  const entry = object(value, `plugin "${plugin.name}" command`);
@@ -0,0 +1,107 @@
1
+ // src/content/document-links.ts
2
+ import path from "path";
3
+ var RdxDocumentLinkError = class extends Error {
4
+ constructor(sourcePath, href, reason) {
5
+ super(`[RDX] Invalid document link "${href}" in ${sourcePath}: ${reason}`);
6
+ this.name = "RdxDocumentLinkError";
7
+ }
8
+ };
9
+ function createDocumentLinkResolver(options) {
10
+ const root = path.resolve(options.sourceRoot);
11
+ const current = path.resolve(options.sourcePath);
12
+ const documents = new Map(
13
+ options.documents.map((page) => [path.resolve(page.sourcePath), page.slug])
14
+ );
15
+ const routeCounts = /* @__PURE__ */ new Map();
16
+ for (const page of options.documents)
17
+ routeCounts.set(page.slug, (routeCounts.get(page.slug) ?? 0) + 1);
18
+ return (href) => {
19
+ if (/^(?:[a-z][a-z\d+.-]*:|\/|#|\?)/i.test(href)) return href;
20
+ const suffixAt = href.search(/[?#]/);
21
+ const pathname = suffixAt < 0 ? href : href.slice(0, suffixAt);
22
+ const suffix = suffixAt < 0 ? "" : href.slice(suffixAt);
23
+ let decoded;
24
+ try {
25
+ decoded = decodeURIComponent(pathname);
26
+ } catch {
27
+ if (/\.mdx$/i.test(pathname))
28
+ throw new RdxDocumentLinkError(
29
+ current,
30
+ href,
31
+ "the path contains invalid URL encoding."
32
+ );
33
+ return href;
34
+ }
35
+ if (!decoded.endsWith(".mdx")) return href;
36
+ const fail = (reason) => {
37
+ throw new RdxDocumentLinkError(current, href, reason);
38
+ };
39
+ if (path.isAbsolute(decoded) || decoded.includes("\\") || decoded.includes("\0"))
40
+ fail("use forward-slash relative paths without null characters.");
41
+ const target = path.resolve(path.dirname(current), decoded);
42
+ const relative = path.relative(root, target);
43
+ if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
44
+ fail("the destination escapes this documentation source.");
45
+ const slug = documents.get(target) ?? fail(
46
+ `"${relative}" does not exist in this documentation source${options.routePrefix ? ` (${options.routePrefix})` : ""}.`
47
+ );
48
+ if ((routeCounts.get(slug) ?? 0) > 1)
49
+ fail("the destination has an ambiguous documentation route.");
50
+ const route = [
51
+ "/docs",
52
+ options.routePrefix && encodeURIComponent(options.routePrefix),
53
+ ...slug.split("/").map(encodeURIComponent)
54
+ ].filter(Boolean).join("/");
55
+ return route + suffix;
56
+ };
57
+ }
58
+ function record(value) {
59
+ return value !== null && typeof value === "object";
60
+ }
61
+ function remarkDocumentLinks(options) {
62
+ if (!options) return;
63
+ const resolve = createDocumentLinkResolver(options);
64
+ return (tree) => {
65
+ const definitions = /* @__PURE__ */ new Map();
66
+ const identifier = (value) => value.trim().replace(/\s+/g, " ").toLowerCase();
67
+ const visit = (node, action) => {
68
+ if (!record(node)) return;
69
+ action(node);
70
+ if (Array.isArray(node.children))
71
+ for (const child of node.children) visit(child, action);
72
+ };
73
+ visit(tree, (node) => {
74
+ if (node.type === "definition" && typeof node.identifier === "string") {
75
+ const key = identifier(node.identifier);
76
+ if (!definitions.has(key)) definitions.set(key, node);
77
+ }
78
+ });
79
+ visit(tree, (node) => {
80
+ if (node.type === "link" && typeof node.url === "string")
81
+ node.url = resolve(node.url);
82
+ if (node.type === "linkReference" && typeof node.identifier === "string") {
83
+ const definition = definitions.get(identifier(node.identifier));
84
+ if (typeof definition?.url === "string") {
85
+ node.type = "link";
86
+ node.url = resolve(definition.url);
87
+ node.title = definition.title ?? null;
88
+ delete node.identifier;
89
+ delete node.referenceType;
90
+ delete node.label;
91
+ }
92
+ }
93
+ if ((node.type === "mdxJsxFlowElement" || node.type === "mdxJsxTextElement") && node.name === "a" && Array.isArray(node.attributes)) {
94
+ for (const attribute of node.attributes) {
95
+ if (record(attribute) && attribute.type === "mdxJsxAttribute" && attribute.name === "href" && typeof attribute.value === "string")
96
+ attribute.value = resolve(attribute.value);
97
+ }
98
+ }
99
+ });
100
+ };
101
+ }
102
+
103
+ export {
104
+ RdxDocumentLinkError,
105
+ createDocumentLinkResolver,
106
+ remarkDocumentLinks
107
+ };
@@ -4,7 +4,7 @@ import {
4
4
  defineConfig,
5
5
  resolveConfig,
6
6
  resolveProjectPath
7
- } from "../chunk-IUJY5VH3.js";
7
+ } from "../chunk-IPSIYQDQ.js";
8
8
  export {
9
9
  RdxConfigError,
10
10
  createRdxProject,
@@ -0,0 +1,19 @@
1
+ interface DocumentLinkOptions {
2
+ sourceRoot: string;
3
+ sourcePath: string;
4
+ routePrefix?: string;
5
+ documents: readonly {
6
+ sourcePath: string;
7
+ slug: string;
8
+ }[];
9
+ }
10
+ declare class RdxDocumentLinkError extends Error {
11
+ constructor(sourcePath: string, href: string, reason: string);
12
+ }
13
+ /** Resolve only explicit relative MDX links within the current content source. */
14
+ declare function createDocumentLinkResolver(options: DocumentLinkOptions): (href: string) => string;
15
+ /** A remark adapter shared by generated sites and the development website. */
16
+ declare function remarkDocumentLinks(options?: DocumentLinkOptions): ((tree: unknown) => void) | undefined;
17
+
18
+ export { RdxDocumentLinkError as R, createDocumentLinkResolver as c, remarkDocumentLinks as r };
19
+ export type { DocumentLinkOptions as D };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as _lexibase_rdx_types from '@lexibase/rdx-types';
2
2
  import { RdxDocsOptions, RdxDocCategory, RdxDocument, RdxProject, RdxContentSource } from '@lexibase/rdx-types';
3
+ export { D as DocumentLinkOptions, R as RdxDocumentLinkError, c as createDocumentLinkResolver } from './document-links-BtWSWs2x.js';
3
4
  export { RdxConfigError, createRdxProject, defineConfig, resolveConfig, resolveProjectPath } from './config/index.js';
4
5
 
5
6
  /**
package/dist/index.js CHANGED
@@ -4,7 +4,11 @@ import {
4
4
  defineConfig,
5
5
  resolveConfig,
6
6
  resolveProjectPath
7
- } from "./chunk-IUJY5VH3.js";
7
+ } from "./chunk-IPSIYQDQ.js";
8
+ import {
9
+ RdxDocumentLinkError,
10
+ createDocumentLinkResolver
11
+ } from "./chunk-Z6GCGXVC.js";
8
12
 
9
13
  // src/content/index.ts
10
14
  import fs from "fs";
@@ -169,6 +173,8 @@ function getProjectDocsPaths(project) {
169
173
  }
170
174
  export {
171
175
  RdxConfigError,
176
+ RdxDocumentLinkError,
177
+ createDocumentLinkResolver,
172
178
  createRdxProject,
173
179
  defineConfig,
174
180
  getDocsIndex,
@@ -0,0 +1,8 @@
1
+ export { D as DocumentLinkOptions, r as remarkDocumentLinks } from '../document-links-BtWSWs2x.js';
2
+ export { default as rehypeDocumentationHeadings } from 'rehype-slug';
3
+ import rehypeShiki from '@shikijs/rehype';
4
+
5
+ /** shadcn's Shiki/GitHub palette, applied to MDX on the server. */
6
+ declare function rehypeDocumentationCode(this: ThisParameterType<typeof rehypeShiki>): ReturnType<typeof rehypeShiki>;
7
+
8
+ export { rehypeDocumentationCode };
@@ -0,0 +1,28 @@
1
+ import {
2
+ remarkDocumentLinks
3
+ } from "../chunk-Z6GCGXVC.js";
4
+
5
+ // src/mdx/index.ts
6
+ import { default as default2 } from "rehype-slug";
7
+
8
+ // src/mdx/code-highlighting.ts
9
+ import rehypeShiki from "@shikijs/rehype";
10
+ function rehypeDocumentationCode() {
11
+ return rehypeShiki.call(this, {
12
+ themes: { light: "github-light", dark: "github-dark" },
13
+ defaultColor: false,
14
+ addLanguageClass: true,
15
+ // Load grammars only when a document uses them, not the whole bundle.
16
+ langs: [],
17
+ lazy: true,
18
+ fallbackLanguage: "text",
19
+ // The copy button must retain MDX's original trailing newline.
20
+ stripEndNewline: false,
21
+ inline: false
22
+ });
23
+ }
24
+ export {
25
+ rehypeDocumentationCode,
26
+ default2 as rehypeDocumentationHeadings,
27
+ remarkDocumentLinks
28
+ };
@@ -0,0 +1,14 @@
1
+ import { RdxSearchIndex } from '@lexibase/rdx-types';
2
+
3
+ interface SearchDocument {
4
+ title: string;
5
+ description?: string;
6
+ category: string;
7
+ href: string;
8
+ content: string;
9
+ }
10
+ /** Parse text/sections without compiling MDX, evaluating JS, or exposing paths. */
11
+ declare function buildSearchIndex(documents: SearchDocument[]): RdxSearchIndex;
12
+
13
+ export { buildSearchIndex };
14
+ export type { SearchDocument };
@@ -0,0 +1,95 @@
1
+ // src/search/index.ts
2
+ import GithubSlugger from "github-slugger";
3
+ import remarkGfm from "remark-gfm";
4
+ import remarkMdx from "remark-mdx";
5
+ import remarkParse from "remark-parse";
6
+ import { unified } from "unified";
7
+ var parser = unified().use(remarkParse).use(remarkGfm).use(remarkMdx);
8
+ function text(node) {
9
+ if (node.type === "break") return " ";
10
+ if (["mdxjsEsm", "mdxFlowExpression", "mdxTextExpression", "html"].includes(
11
+ node.type
12
+ ))
13
+ return "";
14
+ if (node.name && ["script", "style", "svg"].includes(node.name.toLowerCase()))
15
+ return "";
16
+ if (node.type === "image") return node.alt ?? "";
17
+ if (node.children)
18
+ return node.children.map(text).join(
19
+ [
20
+ "paragraph",
21
+ "heading",
22
+ "link",
23
+ "emphasis",
24
+ "strong",
25
+ "delete",
26
+ "mdxJsxTextElement"
27
+ ].includes(node.type) ? "" : " "
28
+ );
29
+ return node.value ?? "";
30
+ }
31
+ function plain(value) {
32
+ return value.replace(/\s+/g, " ").trim();
33
+ }
34
+ function headingText(node) {
35
+ if (["image", "imageReference", "break"].includes(node.type)) return "";
36
+ if (node.children) return node.children.map(headingText).join("");
37
+ return text(node);
38
+ }
39
+ function buildSearchIndex(documents) {
40
+ const entries = [];
41
+ for (const document of documents) {
42
+ let visit2 = function(node, interactive = false) {
43
+ if (node.type === "heading") {
44
+ const title = plain(text(node));
45
+ const id = slugger.slug(headingText(node));
46
+ if (!title) return;
47
+ if (interactive) {
48
+ current.text += ` ${title}`;
49
+ return;
50
+ }
51
+ current = {
52
+ href: `${document.href}#${encodeURIComponent(id)}`,
53
+ title: document.title,
54
+ category: document.category,
55
+ section: title,
56
+ text: ""
57
+ };
58
+ entries.push(current);
59
+ } else if ([
60
+ "mdxJsxFlowElement",
61
+ "root",
62
+ "blockquote",
63
+ "list",
64
+ "listItem"
65
+ ].includes(node.type)) {
66
+ if (node.name && ["script", "style", "svg"].includes(node.name.toLowerCase()))
67
+ return;
68
+ const nestedInteractive = interactive || node.name === "TabsContent" || node.name === "AccordionContent";
69
+ node.children?.forEach((child) => visit2(child, nestedInteractive));
70
+ } else {
71
+ current.text += ` ${text(node)}`;
72
+ }
73
+ };
74
+ var visit = visit2;
75
+ const firstEntry = entries.length;
76
+ const slugger = new GithubSlugger();
77
+ const tree = parser.parse(document.content);
78
+ let current = {
79
+ href: document.href,
80
+ title: document.title,
81
+ category: document.category,
82
+ section: "",
83
+ text: document.description ?? ""
84
+ };
85
+ entries.push(current);
86
+ tree.children?.forEach((child) => visit2(child));
87
+ for (const entry of entries.slice(firstEntry)) {
88
+ entry.text = plain(entry.text);
89
+ }
90
+ }
91
+ return { schemaVersion: 1, entries };
92
+ }
93
+ export {
94
+ buildSearchIndex
95
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lexibase/rdx-core",
3
- "version": "1.0.0-beta.5",
3
+ "version": "1.0.0-beta.7",
4
4
  "description": "Content indexing and document utilities for RDX documentation projects.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -14,6 +14,14 @@
14
14
  "./config": {
15
15
  "types": "./dist/config/index.d.ts",
16
16
  "import": "./dist/config/index.js"
17
+ },
18
+ "./mdx": {
19
+ "types": "./dist/mdx/index.d.ts",
20
+ "import": "./dist/mdx/index.js"
21
+ },
22
+ "./search": {
23
+ "types": "./dist/search/index.d.ts",
24
+ "import": "./dist/search/index.js"
17
25
  }
18
26
  },
19
27
  "files": [
@@ -33,17 +41,24 @@
33
41
  },
34
42
  "homepage": "https://github.com/lexibase/rdx#readme",
35
43
  "dependencies": {
44
+ "github-slugger": "^2.0.0",
45
+ "remark-mdx": "^3.1.1",
46
+ "remark-parse": "^11.0.0",
47
+ "remark-gfm": "^4.0.1",
48
+ "unified": "^11.0.5",
49
+ "@shikijs/rehype": "^4.5.0",
36
50
  "gray-matter": "^4.0.3",
37
- "@lexibase/rdx-types": "1.0.0-beta.4"
51
+ "rehype-slug": "^6.0.0",
52
+ "@lexibase/rdx-types": "1.0.0-beta.5"
38
53
  },
39
54
  "devDependencies": {
40
55
  "@types/node": "^22.15.3",
41
- "@types/react": "19.1.0",
42
- "@rdx/eslint-config": "1.0.0",
43
- "@rdx/typescript-config": "1.0.0"
56
+ "@types/react": "19.3.0",
57
+ "@rdx/typescript-config": "1.0.0",
58
+ "@rdx/eslint-config": "1.0.0"
44
59
  },
45
60
  "scripts": {
46
- "build": "tsup src/index.ts src/config/index.ts --format esm --dts --clean --external @lexibase/rdx-types --external gray-matter",
61
+ "build": "tsup src/index.ts src/config/index.ts src/mdx/index.ts src/search/index.ts --format esm --dts --clean --external @lexibase/rdx-types --external gray-matter",
47
62
  "check-types": "tsc --noEmit",
48
63
  "test": "node --test tests/*.test.mjs"
49
64
  }