@writedocs/generator 0.19.0 → 0.21.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.
@@ -9,10 +9,20 @@
9
9
  // the page and never a file of its own, so a group in a protected section
10
10
  // stays behind the section's rule - and the small script below, which
11
11
  // import()s Scalar (src/scripts/scalar-reference.ts) and mounts it.
12
+ //
13
+ // Until Scalar has drawn the reference - 8 to 10 s on a slow phone network -
14
+ // the page shows its skeleton: the reference's shape in HTML and CSS (no
15
+ // script of its own), drawn over the place Scalar mounts in, with the sizes
16
+ // and positions of the mounted reference, so nothing moves when it's
17
+ // swapped for the real one. Without JavaScript, or when Scalar can't be
18
+ // loaded, the page lists the group's operations instead - each has a page
19
+ // of its own that shows everything. docs/dev/docs/scalar-reference.mdx has
20
+ // the details.
12
21
  import fs from 'node:fs';
13
22
  import { jsonForScript } from '../lib/scalar-document.js';
14
23
  import { scalarDocumentFile } from '../lib/scalar-reference.js';
15
- import { SCALAR_REFERENCE_CLASS } from '../lib/scalar-theme.js';
24
+ import { SCALAR_REFERENCE_CLASS, SCALAR_THEME_VARIABLES } from '../lib/scalar-theme.js';
25
+ import { scalarSkeleton } from '../lib/scalar-skeleton.js';
16
26
 
17
27
  interface Props {
18
28
  contentDir: string;
@@ -24,28 +34,136 @@ interface Props {
24
34
  download: boolean;
25
35
  }
26
36
  const { contentDir, slug, proxy, download } = Astro.props as Props;
27
- const json = jsonForScript(JSON.parse(fs.readFileSync(scalarDocumentFile(contentDir, slug), 'utf-8')));
37
+ const spec = JSON.parse(fs.readFileSync(scalarDocumentFile(contentDir, slug), 'utf-8'));
38
+ const json = jsonForScript(spec);
39
+ // The spec's title, badges, first paragraph and blocks (lib/scalar-skeleton.js),
40
+ // in the font Scalar is given (--scalar-font).
41
+ const skeleton = scalarSkeleton(spec);
42
+ const skeletonFont = `font-family: ${SCALAR_THEME_VARIABLES['--scalar-font']}`;
43
+
44
+ // The skeleton's sidebar: the first entry selected, then entries and an open
45
+ // group's children (indented) - the shape of a reference's list, not its
46
+ // contents. Each is the width of its text placeholder.
47
+ const ENTRIES = [
48
+ { width: '7em', selected: true },
49
+ { width: '5.5em' },
50
+ { width: '7.5em', child: true },
51
+ { width: '9em', child: true },
52
+ { width: '6.5em', child: true },
53
+ { width: '8em', child: true },
54
+ { width: '6.5em' },
55
+ { width: '5em' },
56
+ { width: '6em' },
57
+ ];
28
58
  ---
29
59
 
30
- <div class={SCALAR_REFERENCE_CLASS} data-wd-scalar-reference data-proxy={String(proxy)} data-download={String(download)}>
60
+ <div class={SCALAR_REFERENCE_CLASS} data-wd-scalar-reference data-proxy={String(proxy)} data-download={String(download)} data-wd-state="loading">
31
61
  <script type="application/json" is:inline data-wd-scalar-reference-document set:html={json} />
32
- <div class="wd-scalar-reference-mount"></div>
33
- <p class="wd-scalar-reference-loading" data-pagefind-ignore>Loading the API reference…</p>
62
+ <div class="wd-scalar-reference-stage" aria-busy="true">
63
+ <div class="wd-scalar-reference-mount"></div>
64
+ <p class="wd-scalar-reference-status" data-pagefind-ignore>Loading the API reference</p>
65
+ <div class="wd-scalar-skeleton" aria-hidden="true" data-pagefind-ignore style={skeletonFont}>
66
+ <div class="wd-sk-header"><span class="wd-sk-bar wd-sk-menu"></span><span class="wd-sk-bar" style="width: 6em"></span></div>
67
+ <div class="wd-sk-body">
68
+ <div class="wd-sk-sidebar">
69
+ {
70
+ ENTRIES.map((entry) => (
71
+ <div class:list={['wd-sk-entry', { 'wd-sk-selected': entry.selected, 'wd-sk-child': entry.child }]}>
72
+ <span class="wd-sk-bar" style={`width: ${entry.width}`} />
73
+ </div>
74
+ ))
75
+ }
76
+ </div>
77
+ <div class="wd-sk-main">
78
+ <div class="wd-sk-intro">
79
+ <div class="wd-sk-heading">
80
+ <div class="wd-sk-badges">{skeleton.badges.map((badge) => <span class="wd-sk-badge">{badge}</span>)}</div>
81
+ <div class="wd-sk-title"><span class="wd-sk-words">{skeleton.title}</span></div>
82
+ </div>
83
+ <div class="wd-sk-cols">
84
+ <div class="wd-sk-col">
85
+ <div class:list={["wd-sk-download", { "wd-sk-no-download": !download }]}>{download && <span class="wd-sk-bar" style="width: 13em" />}</div>
86
+ {skeleton.description && <p class="wd-sk-text"><span class="wd-sk-words">{skeleton.description}</span></p>}
87
+ </div>
88
+ <div class="wd-sk-col">
89
+ {skeleton.server && <div class="wd-sk-card" style="height: 66px"><div class="wd-sk-card-head"><span class="wd-sk-bar" style="width: 3.5em" /></div><div class="wd-sk-card-row"><span class="wd-sk-bar" style="width: 11em" /></div></div>}
90
+ {skeleton.auth > 0 && <div class="wd-sk-card" style={`height: ${skeleton.auth}px`}><div class="wd-sk-card-head"><span class="wd-sk-bar" style="width: 7em" /></div><div class="wd-sk-card-row"><span class="wd-sk-bar" style="width: 8em" /></div></div>}
91
+ <div class="wd-sk-card" style="height: 101px"><div class="wd-sk-card-head"><span class="wd-sk-bar" style="width: 7.5em"></span></div><div class="wd-sk-card-row"><span class="wd-sk-bar" style="width: 60%"></span></div><div class="wd-sk-card-row"><span class="wd-sk-bar" style="width: 5em"></span></div></div>
92
+ </div>
93
+ </div>
94
+ </div>
95
+ </div>
96
+ </div>
97
+ </div>
98
+ </div>
99
+ <div class="wd-scalar-reference-fallback" hidden data-pagefind-ignore>
100
+ <p><span class="wd-scalar-reference-reason">The API reference needs JavaScript.</span> Every operation also has a page of its own:</p>
101
+ <div class="wd-scalar-reference-list"><slot /></div>
102
+ <button type="button" class="wd-scalar-reference-retry" hidden>Try again</button>
103
+ </div>
34
104
  <noscript>
35
- <p class="wd-scalar-reference-loading">The API reference needs JavaScript. Every operation also has a page of its own:</p>
36
- <div class="wd-scalar-reference-list" data-pagefind-ignore><slot /></div>
105
+ <style>
106
+ .wd-scalar-reference-stage {
107
+ display: none;
108
+ }
109
+ .wd-scalar-reference-fallback {
110
+ display: block;
111
+ }
112
+ </style>
37
113
  </noscript>
38
114
  </div>
39
115
 
40
116
  <script>
41
117
  // Mounts the reference when the page loads - and again after a
42
- // client-side navigation back to it.
43
- async function initScalarReference() {
44
- const root = document.querySelector<HTMLElement>('[data-wd-scalar-reference]');
45
- const source = root?.querySelector<HTMLScriptElement>('script[data-wd-scalar-reference-document]');
46
- const mount = root?.querySelector<HTMLElement>('.wd-scalar-reference-mount');
47
- if (!root || !source || !mount || root.dataset.wdInit) return;
48
- root.dataset.wdInit = 'true';
118
+ // client-side navigation back to it - and swaps the skeleton for it once
119
+ // Scalar has drawn its first section. If Scalar can't be loaded (the
120
+ // network went away, say), the list of operations takes the skeleton's
121
+ // place, with a button to try again: never a skeleton that stays.
122
+ const SHOWN_WITHIN_MS = 10_000;
123
+
124
+ function setState(root: HTMLElement, state: 'loading' | 'ready' | 'failed') {
125
+ root.dataset.wdState = state;
126
+ const stage = root.querySelector<HTMLElement>('.wd-scalar-reference-stage');
127
+ if (state === 'loading') stage?.setAttribute('aria-busy', 'true');
128
+ else stage?.removeAttribute('aria-busy');
129
+ const fallback = root.querySelector<HTMLElement>('.wd-scalar-reference-fallback');
130
+ if (fallback) fallback.hidden = state !== 'failed';
131
+ const retry = root.querySelector<HTMLButtonElement>('.wd-scalar-reference-retry');
132
+ if (retry) retry.hidden = state !== 'failed';
133
+ }
134
+
135
+ // Ready once Scalar has drawn its first sections and stopped for a moment
136
+ // (QUIET_MS without a change); at the latest after SHOWN_WITHIN_MS,
137
+ // whatever it has drawn by then. Scalar draws a section at a time, and
138
+ // places the first section after the introduction only when that
139
+ // section's content is in - shown as soon as the introduction was, the
140
+ // page moved (a layout shift of 0.19 at 1280px): under the skeleton, out
141
+ // of sight, it moves nothing.
142
+ const QUIET_MS = 200;
143
+ function showWhenDrawn(root: HTMLElement, mount: HTMLElement) {
144
+ let quiet: ReturnType<typeof setTimeout> | undefined;
145
+ const show = () => {
146
+ observer.disconnect();
147
+ clearTimeout(timer);
148
+ clearTimeout(quiet);
149
+ if (root.dataset.wdState === 'loading') requestAnimationFrame(() => setState(root, 'ready'));
150
+ };
151
+ const check = () => {
152
+ if (!mount.querySelector('.section')) return;
153
+ clearTimeout(quiet);
154
+ quiet = setTimeout(show, QUIET_MS);
155
+ };
156
+ const observer = new MutationObserver(check);
157
+ const timer = setTimeout(show, SHOWN_WITHIN_MS);
158
+ observer.observe(mount, { childList: true, subtree: true });
159
+ check();
160
+ }
161
+
162
+ async function load(root: HTMLElement) {
163
+ const source = root.querySelector<HTMLScriptElement>('script[data-wd-scalar-reference-document]');
164
+ const mount = root.querySelector<HTMLElement>('.wd-scalar-reference-mount');
165
+ if (!source || !mount) return;
166
+ setState(root, 'loading');
49
167
  try {
50
168
  const { mountReference } = await import('../scripts/scalar-reference');
51
169
  mountReference({
@@ -54,10 +172,22 @@ const json = jsonForScript(JSON.parse(fs.readFileSync(scalarDocumentFile(content
54
172
  proxy: root.dataset.proxy === 'true',
55
173
  download: root.dataset.download === 'true',
56
174
  });
57
- root.querySelector('.wd-scalar-reference-loading')?.remove();
58
175
  } catch (err) {
59
176
  console.error('[writedocs] Could not open the API reference.', err);
177
+ const reason = root.querySelector('.wd-scalar-reference-reason');
178
+ if (reason) reason.textContent = "The API reference couldn't be loaded.";
179
+ setState(root, 'failed');
180
+ return;
60
181
  }
182
+ showWhenDrawn(root, mount);
183
+ }
184
+
185
+ function initScalarReference() {
186
+ const root = document.querySelector<HTMLElement>('[data-wd-scalar-reference]');
187
+ if (!root || root.dataset.wdInit) return;
188
+ root.dataset.wdInit = 'true';
189
+ root.querySelector('.wd-scalar-reference-retry')?.addEventListener('click', () => load(root));
190
+ load(root);
61
191
  }
62
192
  initScalarReference();
63
193
  document.addEventListener('astro:page-load', initScalarReference);
@@ -70,11 +200,265 @@ const json = jsonForScript(JSON.parse(fs.readFileSync(scalarDocumentFile(content
70
200
  width: 100%;
71
201
  min-width: 0;
72
202
  }
73
- .wd-scalar-reference-loading {
203
+ /* While Scalar loads: its place is the skeleton's, at least the window's
204
+ height below the top bar - so what follows the reference stays out of
205
+ sight - and what Scalar has drawn so far is kept out of view under it. */
206
+ .wd-scalar-reference-stage {
207
+ position: relative;
208
+ }
209
+ .wd-scalar-reference-stage[aria-busy="true"] {
210
+ min-height: calc(100vh - var(--wd-topbar-offset, 5rem));
211
+ }
212
+ .wd-scalar-reference-stage[aria-busy="true"] > .wd-scalar-reference-mount {
213
+ visibility: hidden;
214
+ }
215
+ [data-wd-state="ready"] .wd-scalar-skeleton,
216
+ [data-wd-state="failed"] .wd-scalar-skeleton,
217
+ [data-wd-state="ready"] .wd-scalar-reference-status,
218
+ [data-wd-state="failed"] .wd-scalar-reference-status {
219
+ display: none;
220
+ }
221
+ /* For screen readers only - the skeleton itself is hidden from them. */
222
+ .wd-scalar-reference-status {
223
+ position: absolute;
224
+ width: 1px;
225
+ height: 1px;
226
+ margin: -1px;
227
+ padding: 0;
228
+ overflow: hidden;
229
+ clip-path: inset(50%);
230
+ white-space: nowrap;
231
+ }
232
+ .wd-scalar-reference-fallback {
74
233
  padding: 1.5rem;
234
+ color: var(--wd-text);
235
+ }
236
+ .wd-scalar-reference-fallback > p {
237
+ margin: 0 0 0.75rem;
75
238
  color: var(--wd-text-muted);
76
239
  }
77
- .wd-scalar-reference-list {
78
- padding: 0 1.5rem 2rem;
240
+ .wd-scalar-reference-retry {
241
+ margin-top: 1rem;
242
+ padding: 0.45rem 0.9rem;
243
+ border: 1px solid var(--wd-border);
244
+ border-radius: 0.4rem;
245
+ background: var(--wd-surface);
246
+ color: var(--wd-heading);
247
+ font: inherit;
248
+ font-size: 0.9rem;
249
+ cursor: pointer;
250
+ }
251
+ .wd-scalar-reference-retry:hover {
252
+ border-color: var(--wd-primary);
253
+ }
254
+
255
+ /* The skeleton: the mounted reference's shape, measured in Chrome against
256
+ it (gerador/14) - Scalar's sidebar from 1000px up, its bar of
257
+ operations below that; the title below the room for the version
258
+ badges (the .introduction-section rule in lib/scalar-theme.js); the
259
+ description
260
+ and the Server, Authentication and Client Libraries blocks side by side
261
+ when the reference's content is wider than 900px, one above the other
262
+ otherwise. Over the place Scalar mounts in, so taking it away moves
263
+ nothing. */
264
+ .wd-scalar-skeleton {
265
+ --wd-sk-fill: color-mix(in srgb, var(--wd-text) 9%, transparent);
266
+ --wd-sk-shine: color-mix(in srgb, var(--wd-background) 55%, transparent);
267
+ position: absolute;
268
+ inset: 0;
269
+ z-index: 1;
270
+ overflow: hidden;
271
+ background: var(--wd-background);
272
+ }
273
+ /* The placeholders: a bar, or text drawn as bars of its own shape (its
274
+ letters transparent, a background on each of its lines). */
275
+ .wd-sk-bar,
276
+ .wd-sk-words,
277
+ .wd-sk-badge {
278
+ border-radius: 0.25rem;
279
+ background-color: var(--wd-sk-fill);
280
+ color: transparent;
281
+ -webkit-box-decoration-break: clone;
282
+ box-decoration-break: clone;
283
+ }
284
+ .wd-sk-bar {
285
+ display: block;
286
+ max-width: 100%;
287
+ height: 0.7rem;
288
+ }
289
+ /* A soft shine passing over every placeholder at once - one band the
290
+ width of the window, the placeholders its window onto it. None with
291
+ reduced motion. */
292
+ .wd-sk-bar,
293
+ .wd-sk-words,
294
+ .wd-sk-badge {
295
+ background-image: linear-gradient(100deg, transparent 35%, var(--wd-sk-shine) 50%, transparent 65%);
296
+ background-size: 200vw 100%;
297
+ background-repeat: no-repeat;
298
+ background-attachment: fixed;
299
+ animation: wd-sk-shine 1.8s ease infinite;
300
+ }
301
+ @keyframes wd-sk-shine {
302
+ from {
303
+ background-position: 100vw 0;
304
+ }
305
+ to {
306
+ background-position: -200vw 0;
307
+ }
308
+ }
309
+ @media (prefers-reduced-motion: reduce) {
310
+ .wd-sk-bar,
311
+ .wd-sk-words,
312
+ .wd-sk-badge {
313
+ animation: none;
314
+ background-image: none;
315
+ }
316
+ }
317
+ /* Scalar's bar of operations, below 1000px: 50px, a line under it. */
318
+ .wd-sk-header {
319
+ display: flex;
320
+ align-items: center;
321
+ gap: 0.75rem;
322
+ box-sizing: border-box;
323
+ height: 50px;
324
+ padding: 0 1.25rem;
325
+ border-bottom: 1px solid var(--wd-border);
326
+ }
327
+ .wd-sk-menu {
328
+ width: 1rem;
329
+ }
330
+ .wd-sk-body {
331
+ display: flex;
332
+ }
333
+ /* Scalar's sidebar, from 1000px: the site's sidebar's width and padding
334
+ (SCALAR_REFERENCE_VARIABLES), as tall as the window below the top bar,
335
+ a line at its right. */
336
+ .wd-sk-sidebar {
337
+ display: none;
338
+ flex: none;
339
+ box-sizing: border-box;
340
+ width: 260px;
341
+ height: calc(100vh - var(--wd-topbar-offset, 5rem));
342
+ padding: 1.5rem 1rem 1.5rem 0;
343
+ border-right: 1px solid var(--wd-border);
344
+ }
345
+ @media (min-width: 1000px) {
346
+ .wd-sk-header {
347
+ display: none;
348
+ }
349
+ .wd-sk-sidebar {
350
+ display: flex;
351
+ flex-direction: column;
352
+ gap: 1px;
353
+ }
354
+ }
355
+ /* An entry: the box of .wd-nav-list a (the site's), its text a bar. */
356
+ .wd-sk-entry {
357
+ display: flex;
358
+ align-items: center;
359
+ box-sizing: border-box;
360
+ height: 1.84rem;
361
+ padding: 0 0.75rem;
362
+ border-left: 1px solid transparent;
363
+ border-radius: 0 0.4rem 0.4rem 0;
364
+ }
365
+ .wd-sk-child {
366
+ padding-left: 2rem;
367
+ }
368
+ .wd-sk-selected {
369
+ border-left-color: var(--wd-primary);
370
+ background: color-mix(in srgb, var(--wd-primary) 12%, transparent);
371
+ }
372
+ .wd-sk-main {
373
+ flex: 1;
374
+ min-width: 0;
375
+ padding: 0 1.5rem;
376
+ }
377
+ /* The title 3.5rem below the top, the version badges in that room above
378
+ it (2.25rem in the site's phone layout, under Scalar's bar). */
379
+ .wd-sk-intro {
380
+ padding-top: 3.5rem;
381
+ }
382
+ @media (max-width: 860px) {
383
+ .wd-sk-intro {
384
+ padding-top: 2.25rem;
385
+ }
386
+ }
387
+ .wd-sk-heading {
388
+ position: relative;
389
+ }
390
+ .wd-sk-badges {
391
+ display: flex;
392
+ gap: 6px;
393
+ position: absolute;
394
+ bottom: 100%;
395
+ margin-bottom: 8px;
396
+ }
397
+ /* Scalar's badge: 13px text, 2px 6px around it, a 1px border. */
398
+ .wd-sk-badge {
399
+ display: block;
400
+ padding: 2px 6px;
401
+ border: 1px solid transparent;
402
+ border-radius: 12px;
403
+ font-size: 13px;
404
+ line-height: 1.15;
405
+ white-space: nowrap;
406
+ }
407
+ /* The title: the site's h1, 2rem bold on a 1.6 line. */
408
+ .wd-sk-title {
409
+ margin: 0 0 6px;
410
+ font-size: 2rem;
411
+ font-weight: 700;
412
+ line-height: 1.6;
413
+ }
414
+ .wd-sk-title > .wd-sk-words {
415
+ border-radius: 0.4rem;
416
+ }
417
+ /* Two columns of at least 402px with 48px between: side by side from
418
+ 852px of content - Scalar's 900px, less the 24px on each side. */
419
+ .wd-sk-cols {
420
+ display: grid;
421
+ grid-template-columns: repeat(auto-fit, minmax(min(100%, 402px), 1fr));
422
+ gap: 24px 48px;
423
+ align-items: start;
424
+ }
425
+ .wd-sk-col {
426
+ display: flex;
427
+ flex-direction: column;
428
+ gap: 12px;
429
+ }
430
+ .wd-sk-col:first-child {
431
+ gap: 24px;
432
+ }
433
+ /* The download link - in a protected section, an empty row. */
434
+ .wd-sk-download {
435
+ display: flex;
436
+ align-items: center;
437
+ height: 26px;
438
+ }
439
+ .wd-sk-no-download {
440
+ height: 0;
441
+ }
442
+ /* The description: the site's text, 1rem on a 1.6 line. */
443
+ .wd-sk-text {
444
+ margin: 0;
445
+ font-size: 1rem;
446
+ line-height: 1.6;
447
+ }
448
+ .wd-sk-card {
449
+ box-sizing: border-box;
450
+ border: 1px solid var(--wd-border);
451
+ border-radius: 0.5rem;
452
+ background: var(--wd-surface);
453
+ }
454
+ .wd-sk-card-head,
455
+ .wd-sk-card-row {
456
+ display: flex;
457
+ align-items: center;
458
+ height: 32px;
459
+ padding: 0 12px;
460
+ }
461
+ .wd-sk-card-head {
462
+ border-bottom: 1px solid var(--wd-border);
79
463
  }
80
464
  </style>
@@ -2,6 +2,7 @@ import { z } from "zod";
2
2
  import { findNodeAtLocation, parseTree, visit } from "jsonc-parser";
3
3
  import { BASE_PATH_FIELD_PATTERN } from "./base-path.js";
4
4
  import { stripBom } from "./bom.js";
5
+ import { scalarGroups, scalarPathPrefix, scalarRootPathMessage } from "./scalar-path.js";
5
6
  const navPageSchema = z.string();
6
7
  const navGroupPagesSchema = z.lazy(
7
8
  () => z.object({
@@ -178,6 +179,10 @@ const navigationSchema = z.union([
178
179
  }
179
180
  };
180
181
  if (!Array.isArray(navigation)) protectedIn(navigation.global, ["global"]);
182
+ for (const group of scalarGroups(navigation)) {
183
+ if (scalarPathPrefix(group.path)) continue;
184
+ ctx.addIssue({ code: "custom", path: [...group.trail, "openapi", "path"], message: scalarRootPathMessage(group.group, group.path) });
185
+ }
181
186
  });
182
187
  const DEFAULT_PRIMARY = "#6366f1";
183
188
  const logoSchema = z.union([
@@ -39,6 +39,9 @@ import { BASE_PATH_FIELD_PATTERN } from './base-path.js';
39
39
  // A leading UTF-8 byte order mark off the text - the one copy, lib/bom.js.
40
40
  // Plain JS with no `node:` import too.
41
41
  import { stripBom } from './bom.js';
42
+ // Where a "scalar" OpenAPI group's reference may go, worded as the build
43
+ // words it - lib/scalar-path.js. Plain JS with no `node:` import too.
44
+ import { scalarGroups, scalarPathPrefix, scalarRootPathMessage } from './scalar-path.js';
42
45
 
43
46
  // ---------------------------------------------------------------------
44
47
  // Pages & groups - the leaf content any container ultimately bottoms out
@@ -367,6 +370,13 @@ const navigationSchema = z.union([
367
370
  }
368
371
  };
369
372
  if (!Array.isArray(navigation)) protectedIn((navigation as { global?: unknown }).global, ['global']);
373
+ // A "scalar" group's reference is a page at its openapi.path - never the
374
+ // site's root (the build refuses it the same way; a path a project page
375
+ // already has needs the project's files - content-check.js).
376
+ for (const group of scalarGroups(navigation)) {
377
+ if (scalarPathPrefix(group.path)) continue;
378
+ ctx.addIssue({ code: 'custom', path: [...group.trail, 'openapi', 'path'], message: scalarRootPathMessage(group.group, group.path) });
379
+ }
370
380
  }) as z.ZodType<NavigationConfig>;
371
381
 
372
382
  const DEFAULT_PRIMARY = '#6366f1';
@@ -14,6 +14,9 @@
14
14
  // relative or /snippets/ import of a file that isn't there
15
15
  // - an OpenAPI group's spec is missing or doesn't parse, or two groups
16
16
  // share one `openapi.path`
17
+ // - a "scalar" OpenAPI group's `openapi.path` is the address of one of the
18
+ // project's pages (its reference is a page there - lib/scalar-path.js;
19
+ // at the root, the schema refuses it)
17
20
  // Warnings - the build succeeds, but not as written:
18
21
  // - an unknown component (the build shows only its content - see
19
22
  // lib/mdx-unknown-components.js)
@@ -30,7 +33,7 @@ import fs from 'node:fs';
30
33
  import path from 'node:path';
31
34
  import matter from 'gray-matter';
32
35
  import { visit } from 'unist-util-visit';
33
- import { findAllPages, fileIdForPath, titleFromPath, navPageId } from './pages.js';
36
+ import { findAllPages, fileIdForPath, titleFromPath, navPageId, projectPageIds } from './pages.js';
34
37
  import { projectIgnore } from './project-ignore.js';
35
38
  import { iconExists } from '../../packages/components/src/lib/icons.js';
36
39
  import { findUnknownComponents } from './mdx-unknown-components.js';
@@ -40,6 +43,7 @@ import { parseOpenApiRef } from './openapi-ref.js';
40
43
  import { readText } from './config-file.js';
41
44
  import { collectOpenApiGroups, operationKeys } from './openapi-spec.js';
42
45
  import { usesAccess, accessOf, firstAccessPath, ACCESS_NEEDS_PLATFORM } from './protected-sections.js';
46
+ import { scalarGroups, scalarPathPrefix, scalarPagePathMessage } from './scalar-path.js';
43
47
 
44
48
  /** An issue: { file, line?, message, suggestion? } - `file` relative to the
45
49
  * content directory, POSIX slashes. */
@@ -478,6 +482,19 @@ export async function checkContent(contentDir, configText) {
478
482
  )
479
483
  );
480
484
  }
485
+ // A "scalar" group's reference is a page at its openapi.path: the build
486
+ // refuses one where a page of the project already is
487
+ // (generate-api-pages.js) - the same pages it builds, drafts as it does.
488
+ const scalar = scalarGroups(nav).filter((g) => scalarPathPrefix(g.path));
489
+ if (scalar.length) {
490
+ const pageIds = projectPageIds(contentDir);
491
+ for (const group of scalar) {
492
+ const prefix = scalarPathPrefix(group.path);
493
+ if (!pageIds.has(prefix)) continue;
494
+ const jsonPath = ['navigation', ...group.trail, 'openapi', 'path'];
495
+ errors.push({ ...issue('writedocs.json', locate(jsonPath)?.line, scalarPagePathMessage(group.group, prefix)), path: jsonPath.join('.') });
496
+ }
497
+ }
481
498
  // A redirect is one exact path - a Mintlify-style pattern (`/old/:slug`,
482
499
  // `/old/*`) becomes a literal page path, and the build fails on it.
483
500
  (Array.isArray(config.redirects) ? config.redirects : []).forEach((r, i) => {
@@ -523,11 +540,12 @@ export async function checkContent(contentDir, configText) {
523
540
  return { pages: pages.length, errors, warnings };
524
541
  }
525
542
 
526
- /** Same layout as formatValidationIssuesDetailed() for writedocs.json. */
543
+ /** Same layout as formatValidationIssuesDetailed() for writedocs.json - with
544
+ * the field's path after the line, for an issue that has one. */
527
545
  export function formatContentIssues(issues) {
528
546
  return issues
529
547
  .map((i) => {
530
- const lines = [` ${i.file}${i.line ? `:${i.line}` : ''}`, ` ${i.message}`];
548
+ const lines = [` ${i.file}${i.line ? `:${i.line}` : ''}${i.path ? ` (${i.path})` : ''}`, ` ${i.message}`];
531
549
  if (i.suggestion) lines.push(` ${i.suggestion}`);
532
550
  return lines.join('\n');
533
551
  })
package/src/lib/pages.js CHANGED
@@ -219,6 +219,22 @@ export function fileIdForPath(relativePath) {
219
219
  return relativePath.replace(/\.mdx?$/i, '').replace(/\/index$/, '');
220
220
  }
221
221
 
222
+ /** The addresses (file ids) of the project's own pages: each one's path
223
+ * without the extension or a trailing /index, or its frontmatter `slug`.
224
+ * What a "scalar" OpenAPI group's reference may not take
225
+ * (generate-api-pages.js, and content-check.js for `writedocs validate`). */
226
+ export function projectPageIds(contentDir, { drafts = draftsIncluded() } = {}) {
227
+ const ids = new Set();
228
+ for (const rel of findAllPages(contentDir, { drafts })) {
229
+ ids.add(fileIdForPath(rel));
230
+ try {
231
+ const { data } = matter(fs.readFileSync(path.join(contentDir, rel), 'utf-8'));
232
+ if (typeof data?.slug === 'string') ids.add(data.slug.replace(/^\/+|\/+$/g, ''));
233
+ } catch {}
234
+ }
235
+ return ids;
236
+ }
237
+
222
238
  /** Any `.css`/`.js` file anywhere in the project - root or any subfolder,
223
239
  * `public/` included - auto-loaded site-wide with zero `writedocs.json`
224
240
  * config, on top of (not instead of) the explicit `scripts` field