@lexibase/rdx-core 2.0.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.
@@ -2,14 +2,18 @@ import {
2
2
  getDocsIndex,
3
3
  getSourceOptions,
4
4
  parseStaticDocument
5
- } from "../chunk-MALRN4SS.js";
5
+ } from "../chunk-CGOXC3S6.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-LCBSNAUH.js";
14
+ import {
15
+ headingText
16
+ } from "../chunk-FPBRKD4M.js";
13
17
  import {
14
18
  remarkDocumentationComponents
15
19
  } from "../chunk-J7VCQHSL.js";
@@ -23,7 +27,7 @@ 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 sourceRoute = (source) => [source.basePath ?? "/docs", source.routePrefix].filter(Boolean).join("/");
27
31
  var documentRoute = (source, page) => `${sourceRoute(source)}/${page.slug.split("/").map(encodeURIComponent).join("/")}`;
28
32
  var identifier = (value) => value.trim().replace(/\s+/g, " ").toLowerCase();
29
33
  function walk(node, visit) {
@@ -100,16 +104,46 @@ function checkDocumentation(project, options = {}) {
100
104
  message: value.message ?? String(error)
101
105
  });
102
106
  }
103
- for (const source of project.sources) {
107
+ const sources = getContentSources(
108
+ project.locale ? { ...project, locale: void 0 } : project
109
+ );
110
+ if (project.localization) {
111
+ const primary = project.localization.locales.find(
112
+ (locale) => locale.code === project.localization.defaultLocale
113
+ );
114
+ prefixes.push(["/docs", `/${primary.path}/docs`]);
115
+ }
116
+ for (const source of sources) {
104
117
  if (source.redirectTo !== void 0) {
105
- const destination = project.sources.find(
106
- (entry) => entry.routePrefix === source.redirectTo
118
+ const destination = sources.find(
119
+ (entry) => entry.routePrefix === source.redirectTo && entry.basePath === source.basePath
107
120
  );
108
121
  prefixes.push([key(rootFor(source)), key(rootFor(destination))]);
109
122
  continue;
110
123
  }
111
124
  let pages;
112
125
  try {
126
+ if (source.fallbackDocsDir) {
127
+ const primaryPages = new Set(
128
+ getDocsIndex(
129
+ { docsDir: path.resolve(project.rootDir, source.fallbackDocsDir) },
130
+ (error, file) => syntax(error, file)
131
+ ).flatMap((category) => category.pages.map((page) => page.slug))
132
+ );
133
+ for (const category of getDocsIndex(
134
+ { docsDir: path.resolve(project.rootDir, source.docsDir) },
135
+ (error, file) => syntax(error, file)
136
+ ))
137
+ for (const page of category.pages)
138
+ if (!primaryPages.has(page.slug))
139
+ diagnostics.push({
140
+ file: relativeFile(page.sourcePath),
141
+ line: 1,
142
+ column: 1,
143
+ code: "page",
144
+ message: "Translation has no primary-language counterpart with the same relative MDX path."
145
+ });
146
+ }
113
147
  pages = getDocsIndex(getSourceOptions(project, source), (error, file) => {
114
148
  syntax(error, file);
115
149
  failed.add(file);
@@ -170,16 +204,28 @@ function checkDocumentation(project, options = {}) {
170
204
  for (const document of documents) {
171
205
  const parsed = document.parsed;
172
206
  if (!parsed) continue;
207
+ const contentSource = isFallbackPage(
208
+ project,
209
+ document.source,
210
+ document.page.sourcePath
211
+ ) ? sources.find(
212
+ (source) => source.id === document.source.id && source.docsDir === document.source.fallbackDocsDir && !source.fallbackDocsDir
213
+ ) : document.source;
173
214
  const resolve = createDocumentLinkResolver({
174
- sourceRoot: getSourceOptions(project, document.source).docsDir,
215
+ sourceRoot: getSourceOptions(project, contentSource).docsDir,
175
216
  sourcePath: document.page.sourcePath,
176
- routePrefix: document.source.routePrefix,
177
- documents: documents.filter((entry) => entry.source === document.source).map((entry) => entry.page)
217
+ routePrefix: contentSource.routePrefix,
218
+ basePath: contentSource.basePath,
219
+ virtualPaths: !!contentSource.fallbackDocsDir,
220
+ documents: documents.filter((entry) => entry.source === contentSource).map((entry) => entry.page)
178
221
  });
179
222
  for (const reference of parsed.references) {
180
223
  let code = reference.image ? "asset" : "page";
181
224
  try {
182
- const url = new URL(reference.href, `${origin}${document.href}`);
225
+ const url = new URL(
226
+ reference.href,
227
+ `${origin}${contentSource === document.source ? document.href : documentRoute(contentSource, document.page)}`
228
+ );
183
229
  if (!["http:", "https:"].includes(url.protocol)) continue;
184
230
  if (url.origin !== origin) continue;
185
231
  if (!project.config.metadata.metadataBase && /^(?:[a-z][\w+.-]*:|\/\/)/i.test(reference.href))
@@ -207,7 +253,7 @@ function checkDocumentation(project, options = {}) {
207
253
  continue;
208
254
  }
209
255
  if (!reference.image && roots.has(pathname)) continue;
210
- code = reference.image || !/^\/docs(?:\/|$)/.test(pathname) ? "asset" : "page";
256
+ code = reference.image || !/^\/(?:[^/]+\/)?docs(?:\/|$)/.test(pathname) ? "asset" : "page";
211
257
  if (pathname.includes("\\") || pathname.includes("\0"))
212
258
  throw new Error("Use a valid URL with forward slashes.");
213
259
  const publicRoot = path.resolve(project.rootDir, "public");
@@ -239,7 +285,15 @@ function checkDocumentation(project, options = {}) {
239
285
  diagnostics.sort(
240
286
  (a, b) => a.file.localeCompare(b.file) || a.line - b.line || a.column - b.column
241
287
  );
242
- return { checkedPages: documents.length, diagnostics };
288
+ const unique = [
289
+ ...new Map(
290
+ diagnostics.map((diagnostic) => [JSON.stringify(diagnostic), diagnostic])
291
+ ).values()
292
+ ];
293
+ return {
294
+ checkedPages: new Set(documents.map((document) => document.page.sourcePath)).size,
295
+ diagnostics: unique
296
+ };
243
297
  }
244
298
  function formatDocumentationCheck(report) {
245
299
  const lines = report.diagnostics.map(
@@ -71,7 +71,32 @@ function isSafeSlug(slug) {
71
71
  if (path.isAbsolute(slug) || slug.includes("\\")) return false;
72
72
  return slug.split("/").every((segment) => segment && segment !== "." && segment !== "..");
73
73
  }
74
- function getDocsIndex({ docsDir } = {}, onError) {
74
+ function getDocsIndex({ docsDir, fallbackDocsDir, fallbackOnly } = {}, onError) {
75
+ if (fallbackOnly)
76
+ return getDocsIndex({ docsDir: fallbackDocsDir ?? docsDir }, onError);
77
+ if (fallbackDocsDir) {
78
+ const primary = getDocsIndex({ docsDir: fallbackDocsDir }, onError);
79
+ const translated = getDocsIndex({ docsDir }, onError);
80
+ const pages = new Map(
81
+ translated.flatMap(
82
+ (category) => category.pages.map((page) => [page.slug, page])
83
+ )
84
+ );
85
+ return primary.map((category) => ({
86
+ ...category,
87
+ title: readCategoryMeta(
88
+ path.join(
89
+ getDocsRoot(docsDir),
90
+ category.id === "_root" ? "" : category.id
91
+ ),
92
+ category.title
93
+ ).title,
94
+ pages: category.pages.map((page) => ({
95
+ ...pages.get(page.slug) ?? page,
96
+ position: page.position
97
+ }))
98
+ }));
99
+ }
75
100
  const docsRoot = getDocsRoot(docsDir);
76
101
  if (!fs.existsSync(docsRoot)) return [];
77
102
  const categories = /* @__PURE__ */ new Map();
@@ -127,14 +152,19 @@ function getDocsPaths(options) {
127
152
  (category) => category.pages.map((page) => page.slug)
128
153
  );
129
154
  }
130
- function getDocument(slug, { docsDir } = {}) {
155
+ function getDocument(slug, { docsDir, fallbackDocsDir, fallbackOnly } = {}) {
131
156
  if (!isSafeSlug(slug)) return null;
157
+ if (fallbackOnly)
158
+ return getDocument(slug, { docsDir: fallbackDocsDir ?? docsDir });
132
159
  const docsRoot = getDocsRoot(docsDir);
133
160
  const sourcePath = path.resolve(docsRoot, `${slug}${MDX_EXTENSION}`);
134
161
  const relativePath = path.relative(docsRoot, sourcePath);
135
162
  if (relativePath.startsWith("..") || path.isAbsolute(relativePath))
136
163
  return null;
137
- if (!fs.existsSync(sourcePath)) return null;
164
+ if (fallbackDocsDir && !getDocument(slug, { docsDir: fallbackDocsDir }))
165
+ return null;
166
+ if (!fs.existsSync(sourcePath))
167
+ return fallbackDocsDir ? getDocument(slug, { docsDir: fallbackDocsDir }) : null;
138
168
  const parsed = matter2(fs.readFileSync(sourcePath, "utf8"));
139
169
  return {
140
170
  slug,
@@ -147,7 +177,16 @@ function getDocument(slug, { docsDir } = {}) {
147
177
  // src/project/index.ts
148
178
  import path2 from "path";
149
179
  function getSourceOptions(project, source) {
150
- return { docsDir: path2.resolve(project.rootDir, source.docsDir) };
180
+ return {
181
+ docsDir: path2.resolve(project.rootDir, source.docsDir),
182
+ ...source.fallbackOnly ? { fallbackOnly: true } : {},
183
+ ...source.fallbackDocsDir ? {
184
+ fallbackDocsDir: path2.resolve(
185
+ project.rootDir,
186
+ source.fallbackDocsDir
187
+ )
188
+ } : {}
189
+ };
151
190
  }
152
191
  function resolveDocsRoute(project, segments = []) {
153
192
  const source = project.sources.find(
@@ -12,7 +12,10 @@ 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 ? [[path.resolve(root, `${page.slug}.mdx`), page.slug]] : []
18
+ ])
16
19
  );
17
20
  const routeCounts = /* @__PURE__ */ new Map();
18
21
  for (const page of options.documents)
@@ -50,7 +53,7 @@ function createDocumentLinkResolver(options) {
50
53
  if ((routeCounts.get(slug) ?? 0) > 1)
51
54
  fail("the destination has an ambiguous documentation route.");
52
55
  const route = [
53
- "/docs",
56
+ options.basePath ?? "/docs",
54
57
  options.routePrefix && encodeURIComponent(options.routePrefix),
55
58
  ...slug.split("/").map(encodeURIComponent)
56
59
  ].filter(Boolean).join("/");
@@ -0,0 +1,75 @@
1
+ // src/project/localization.ts
2
+ import path from "path";
3
+ function getLocalizedProject(project, locale) {
4
+ const selected = project.localization?.locales.find(
5
+ (entry) => entry.path === locale
6
+ );
7
+ if (!selected) throw new Error(`Unknown documentation language: ${locale}`);
8
+ return {
9
+ ...project,
10
+ locale: selected,
11
+ config: { ...project.config, lang: selected.code },
12
+ sources: project.sources.map((source) => {
13
+ const primary = source.localization?.docs[project.localization.defaultLocale] ?? source.fallbackDocsDir ?? source.docsDir;
14
+ const translated = source.localization?.docs[selected.code] ?? (!source.localization && !source.version && selected.folder ? `locale/${selected.folder}/docs` : void 0);
15
+ const isPrimary = selected.code === project.localization.defaultLocale;
16
+ return {
17
+ ...source,
18
+ docsDir: isPrimary ? primary : translated ?? primary,
19
+ fallbackDocsDir: isPrimary ? void 0 : primary,
20
+ fallbackOnly: !isPrimary && !translated,
21
+ basePath: `/${selected.path}/docs`
22
+ };
23
+ })
24
+ };
25
+ }
26
+ function getContentSources(project) {
27
+ return project.localization && !project.locale ? project.localization.locales.flatMap(
28
+ (locale) => getLocalizedProject(project, locale.path).sources
29
+ ) : project.sources;
30
+ }
31
+ function getDocsHref(project, source = project.sources[0], slug = "") {
32
+ const base = source.basePath ?? (project.localization ? `/${project.localization.locales.find((entry) => entry.code === project.localization.defaultLocale).path}/docs` : "/docs");
33
+ return [
34
+ base,
35
+ source.routePrefix,
36
+ ...slug.split("/").filter(Boolean).map(encodeURIComponent)
37
+ ].filter(Boolean).join("/");
38
+ }
39
+ function getLanguageLinks(project, source, slug = "") {
40
+ if (!project.localization) return [];
41
+ const visible = new Set(
42
+ (project.sources[0]?.localization?.locales ?? project.localization.locales).map((locale) => locale.code)
43
+ );
44
+ for (const locale of source.localization?.locales ?? [])
45
+ visible.add(locale.code);
46
+ if (project.locale) visible.add(project.locale.code);
47
+ return project.localization.locales.filter((locale) => visible.has(locale.code)).map((locale) => {
48
+ const view = getLocalizedProject(project, locale.path);
49
+ return {
50
+ code: locale.code,
51
+ label: source.localization?.locales.find(
52
+ (entry) => entry.code === locale.code
53
+ )?.label ?? locale.label,
54
+ href: getDocsHref(
55
+ view,
56
+ view.sources.find((entry) => entry.id === source.id),
57
+ slug
58
+ )
59
+ };
60
+ });
61
+ }
62
+ function isFallbackPage(project, source, sourcePath) {
63
+ return !!source.fallbackDocsDir && !path.relative(
64
+ path.resolve(project.rootDir, source.fallbackDocsDir),
65
+ sourcePath
66
+ ).startsWith("..");
67
+ }
68
+
69
+ export {
70
+ getLocalizedProject,
71
+ getContentSources,
72
+ getDocsHref,
73
+ getLanguageLinks,
74
+ isFallbackPage
75
+ };