@lexibase/rdx-core 2.0.0 → 2.2.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.
@@ -2,14 +2,18 @@ import {
2
2
  getDocsIndex,
3
3
  getSourceOptions,
4
4
  parseStaticDocument
5
- } from "../chunk-MALRN4SS.js";
5
+ } from "../chunk-M32EH5KP.js";
6
6
  import {
7
- headingText
8
- } from "../chunk-FPBRKD4M.js";
7
+ getContentSources,
8
+ isFallbackPage
9
+ } from "../chunk-SNNEXRLQ.js";
9
10
  import {
10
11
  RdxDocumentLinkError,
11
12
  createDocumentLinkResolver
12
- } from "../chunk-FWPJPY46.js";
13
+ } from "../chunk-36CTLWNI.js";
14
+ import {
15
+ headingText
16
+ } from "../chunk-FPBRKD4M.js";
13
17
  import {
14
18
  remarkDocumentationComponents
15
19
  } from "../chunk-J7VCQHSL.js";
@@ -23,15 +27,19 @@ import remarkMdx from "remark-mdx";
23
27
  import remarkParse from "remark-parse";
24
28
  import { unified } from "unified";
25
29
  var parser = unified().use(remarkParse).use(remarkGfm).use(remarkMdx).use(remarkDocumentationComponents);
26
- var sourceRoute = (source) => ["/docs", source.routePrefix].filter(Boolean).join("/");
30
+ var markdownParser = unified().use(remarkParse).use(remarkGfm).use(remarkDocumentationComponents);
31
+ var sourceRoute = (source) => [source.basePath ?? "/docs", source.routePrefix].filter(Boolean).join("/");
27
32
  var documentRoute = (source, page) => `${sourceRoute(source)}/${page.slug.split("/").map(encodeURIComponent).join("/")}`;
28
33
  var identifier = (value) => value.trim().replace(/\s+/g, " ").toLowerCase();
29
34
  function walk(node, visit) {
30
35
  visit(node);
31
36
  for (const child of node.children ?? []) walk(child, visit);
32
37
  }
33
- function parse(content) {
34
- const tree = parser.runSync(parser.parse(content), { value: content });
38
+ function parse(content, markdown) {
39
+ const processor = markdown ? markdownParser : parser;
40
+ const tree = processor.runSync(processor.parse(content), {
41
+ value: content
42
+ });
35
43
  const anchors = /* @__PURE__ */ new Set();
36
44
  const references = [];
37
45
  const definitions = /* @__PURE__ */ new Map();
@@ -100,16 +108,77 @@ function checkDocumentation(project, options = {}) {
100
108
  message: value.message ?? String(error)
101
109
  });
102
110
  }
103
- for (const source of project.sources) {
111
+ function publicFileExists(pathname) {
112
+ if (pathname.includes("\\") || pathname.includes("\0"))
113
+ throw new Error("Use a valid URL with forward slashes.");
114
+ const publicRoot = path.resolve(project.rootDir, "public");
115
+ const asset = path.resolve(publicRoot, `.${pathname}`);
116
+ const relative = path.relative(publicRoot, asset);
117
+ if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
118
+ throw new Error("The destination escapes public/.");
119
+ let exists = false;
120
+ try {
121
+ exists = fs.statSync(asset).isFile();
122
+ } catch {
123
+ }
124
+ return exists;
125
+ }
126
+ const logo = project.config.navbar?.logo;
127
+ if (typeof logo === "string") {
128
+ try {
129
+ const pathname = decodeURIComponent(new URL(logo, origin).pathname);
130
+ if (!publicFileExists(pathname))
131
+ throw new Error(`Local file "${pathname}" does not exist in public/.`);
132
+ } catch (error) {
133
+ diagnostics.push({
134
+ file: "rdx.config.ts",
135
+ line: 1,
136
+ column: 1,
137
+ code: "asset",
138
+ message: `navbar.logo "${logo}": ${error instanceof Error ? error.message : String(error)}`
139
+ });
140
+ }
141
+ }
142
+ const sources = getContentSources(
143
+ project.locale ? { ...project, locale: void 0 } : project
144
+ );
145
+ if (project.localization) {
146
+ const primary = project.localization.locales.find(
147
+ (locale) => locale.code === project.localization.defaultLocale
148
+ );
149
+ prefixes.push(["/docs", `/${primary.path}/docs`]);
150
+ }
151
+ for (const source of sources) {
104
152
  if (source.redirectTo !== void 0) {
105
- const destination = project.sources.find(
106
- (entry) => entry.routePrefix === source.redirectTo
153
+ const destination = sources.find(
154
+ (entry) => entry.routePrefix === source.redirectTo && entry.basePath === source.basePath
107
155
  );
108
156
  prefixes.push([key(rootFor(source)), key(rootFor(destination))]);
109
157
  continue;
110
158
  }
111
159
  let pages;
112
160
  try {
161
+ if (source.fallbackDocsDir) {
162
+ const primaryPages = new Set(
163
+ getDocsIndex(
164
+ { docsDir: path.resolve(project.rootDir, source.fallbackDocsDir) },
165
+ (error, file) => syntax(error, file)
166
+ ).flatMap((category) => category.pages.map((page) => page.slug))
167
+ );
168
+ for (const category of getDocsIndex(
169
+ { docsDir: path.resolve(project.rootDir, source.docsDir) },
170
+ (error, file) => syntax(error, file)
171
+ ))
172
+ for (const page of category.pages)
173
+ if (!primaryPages.has(page.slug))
174
+ diagnostics.push({
175
+ file: relativeFile(page.sourcePath),
176
+ line: 1,
177
+ column: 1,
178
+ code: "page",
179
+ message: "Translation has no primary-language counterpart with the same relative document path (without the extension)."
180
+ });
181
+ }
113
182
  pages = getDocsIndex(getSourceOptions(project, source), (error, file) => {
114
183
  syntax(error, file);
115
184
  failed.add(file);
@@ -148,7 +217,7 @@ function checkDocumentation(project, options = {}) {
148
217
  const raw = fs.readFileSync(document.page.sourcePath, "utf8");
149
218
  const content = parseStaticDocument(raw).content;
150
219
  document.lineOffset = (raw.slice(0, raw.length - content.length).match(/\n/g) ?? []).length;
151
- document.parsed = parse(content);
220
+ document.parsed = parse(content, document.page.sourcePath.endsWith(".md"));
152
221
  } catch (error) {
153
222
  syntax(error, document.page.sourcePath, document.lineOffset);
154
223
  }
@@ -170,24 +239,36 @@ function checkDocumentation(project, options = {}) {
170
239
  for (const document of documents) {
171
240
  const parsed = document.parsed;
172
241
  if (!parsed) continue;
242
+ const contentSource = isFallbackPage(
243
+ project,
244
+ document.source,
245
+ document.page.sourcePath
246
+ ) ? sources.find(
247
+ (source) => source.id === document.source.id && source.docsDir === document.source.fallbackDocsDir && !source.fallbackDocsDir
248
+ ) : document.source;
173
249
  const resolve = createDocumentLinkResolver({
174
- sourceRoot: getSourceOptions(project, document.source).docsDir,
250
+ sourceRoot: getSourceOptions(project, contentSource).docsDir,
175
251
  sourcePath: document.page.sourcePath,
176
- routePrefix: document.source.routePrefix,
177
- documents: documents.filter((entry) => entry.source === document.source).map((entry) => entry.page)
252
+ routePrefix: contentSource.routePrefix,
253
+ basePath: contentSource.basePath,
254
+ virtualPaths: !!contentSource.fallbackDocsDir,
255
+ documents: documents.filter((entry) => entry.source === contentSource).map((entry) => entry.page)
178
256
  });
179
257
  for (const reference of parsed.references) {
180
258
  let code = reference.image ? "asset" : "page";
181
259
  try {
182
- const url = new URL(reference.href, `${origin}${document.href}`);
260
+ const url = new URL(
261
+ reference.href,
262
+ `${origin}${contentSource === document.source ? document.href : documentRoute(contentSource, document.page)}`
263
+ );
183
264
  if (!["http:", "https:"].includes(url.protocol)) continue;
184
265
  if (url.origin !== origin) continue;
185
266
  if (!project.config.metadata.metadataBase && /^(?:[a-z][\w+.-]*:|\/\/)/i.test(reference.href))
186
267
  continue;
187
268
  let target;
188
269
  let pathname;
189
- const explicitMdx = !reference.image && !/^(?:[a-z][\w+.-]*:|\/|#|\?)/i.test(reference.href) && /\.mdx(?:[?#]|$)/.test(decodeURIComponent(reference.href));
190
- if (explicitMdx) {
270
+ const explicitDocument = !reference.image && !/^(?:[a-z][\w+.-]*:|\/|#|\?)/i.test(reference.href) && /\.mdx?(?:[?#]|$)/.test(decodeURIComponent(reference.href));
271
+ if (explicitDocument) {
191
272
  const resolved = resolve(reference.href);
192
273
  pathname = key(resolved);
193
274
  target = coreRoutes.get(pathname);
@@ -207,20 +288,8 @@ function checkDocumentation(project, options = {}) {
207
288
  continue;
208
289
  }
209
290
  if (!reference.image && roots.has(pathname)) continue;
210
- code = reference.image || !/^\/docs(?:\/|$)/.test(pathname) ? "asset" : "page";
211
- if (pathname.includes("\\") || pathname.includes("\0"))
212
- throw new Error("Use a valid URL with forward slashes.");
213
- const publicRoot = path.resolve(project.rootDir, "public");
214
- const asset = path.resolve(publicRoot, `.${pathname}`);
215
- const relative = path.relative(publicRoot, asset);
216
- if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
217
- throw new Error("The destination escapes public/.");
218
- let exists = false;
219
- try {
220
- exists = fs.statSync(asset).isFile();
221
- } catch {
222
- }
223
- if (!exists)
291
+ code = reference.image || !/^\/(?:[^/]+\/)?docs(?:\/|$)/.test(pathname) ? "asset" : "page";
292
+ if (!publicFileExists(pathname))
224
293
  throw new Error(
225
294
  code === "page" ? `Documentation page "${pathname}" does not exist.` : `Local file "${pathname}" does not exist in public/.`
226
295
  );
@@ -239,7 +308,15 @@ function checkDocumentation(project, options = {}) {
239
308
  diagnostics.sort(
240
309
  (a, b) => a.file.localeCompare(b.file) || a.line - b.line || a.column - b.column
241
310
  );
242
- return { checkedPages: documents.length, diagnostics };
311
+ const unique = [
312
+ ...new Map(
313
+ diagnostics.map((diagnostic) => [JSON.stringify(diagnostic), diagnostic])
314
+ ).values()
315
+ ];
316
+ return {
317
+ checkedPages: new Set(documents.map((document) => document.page.sourcePath)).size,
318
+ diagnostics: unique
319
+ };
243
320
  }
244
321
  function formatDocumentationCheck(report) {
245
322
  const lines = report.diagnostics.map(
@@ -12,7 +12,18 @@ function createDocumentLinkResolver(options) {
12
12
  const root = path.resolve(options.sourceRoot);
13
13
  const current = path.resolve(options.sourcePath);
14
14
  const documents = new Map(
15
- options.documents.map((page) => [path.resolve(page.sourcePath), page.slug])
15
+ options.documents.flatMap((page) => [
16
+ [path.resolve(page.sourcePath), page.slug],
17
+ ...options.virtualPaths ? [
18
+ [
19
+ path.resolve(
20
+ root,
21
+ `${page.slug}${path.extname(page.sourcePath)}`
22
+ ),
23
+ page.slug
24
+ ]
25
+ ] : []
26
+ ])
16
27
  );
17
28
  const routeCounts = /* @__PURE__ */ new Map();
18
29
  for (const page of options.documents)
@@ -26,7 +37,7 @@ function createDocumentLinkResolver(options) {
26
37
  try {
27
38
  decoded = decodeURIComponent(pathname);
28
39
  } catch {
29
- if (/\.mdx$/i.test(pathname))
40
+ if (/\.mdx?$/i.test(pathname))
30
41
  throw new RdxDocumentLinkError(
31
42
  current,
32
43
  href,
@@ -34,7 +45,7 @@ function createDocumentLinkResolver(options) {
34
45
  );
35
46
  return href;
36
47
  }
37
- if (!decoded.endsWith(".mdx")) return href;
48
+ if (!/\.mdx?$/.test(decoded)) return href;
38
49
  const fail = (reason) => {
39
50
  throw new RdxDocumentLinkError(current, href, reason);
40
51
  };
@@ -50,7 +61,7 @@ function createDocumentLinkResolver(options) {
50
61
  if ((routeCounts.get(slug) ?? 0) > 1)
51
62
  fail("the destination has an ambiguous documentation route.");
52
63
  const route = [
53
- "/docs",
64
+ options.basePath ?? "/docs",
54
65
  options.routePrefix && encodeURIComponent(options.routePrefix),
55
66
  ...slug.split("/").map(encodeURIComponent)
56
67
  ].filter(Boolean).join("/");