@writedocs/generator 0.18.0 → 0.20.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.
- package/bin/writedocs.js +5 -3
- package/package.json +1 -1
- package/src/cli/build.js +13 -2
- package/src/cli/generate-api-pages.js +6 -28
- package/src/cli/run-pagefind.js +57 -2
- package/src/components/ScalarReference.astro +402 -18
- package/src/lib/config-schema.js +5 -0
- package/src/lib/config-schema.ts +10 -0
- package/src/lib/content-check.js +21 -3
- package/src/lib/pages.js +16 -0
- package/src/lib/scalar-path.js +53 -0
- package/src/lib/scalar-skeleton.js +59 -0
- package/src/lib/scalar-theme.js +10 -11
- package/src/styles/global.css +14 -0
package/bin/writedocs.js
CHANGED
|
@@ -343,9 +343,11 @@ program
|
|
|
343
343
|
// is asynchronous, and `writedocs validate` with 1,000 errors reached a CI log
|
|
344
344
|
// or the platform's build log with 1,198 of its 5,003 lines. exitWhenFlushed()
|
|
345
345
|
// waits for stdout and stderr first (10 s at most, for a reader that stopped
|
|
346
|
-
// reading). The exit codes are a contract with the platform: 0, 1, and 75
|
|
347
|
-
// from requireBuildKey() (src/cli/build-auth.js), which
|
|
348
|
-
// a few lines - they always fit in an empty pipe
|
|
346
|
+
// reading). The exit codes are a contract with the platform: 0, 1, and 75 -
|
|
347
|
+
// worth trying again - from requireBuildKey() (src/cli/build-auth.js), which
|
|
348
|
+
// exits on its own after a few lines - they always fit in an empty pipe - and
|
|
349
|
+
// from the build when Pagefind leaves a search index file empty twice
|
|
350
|
+
// (src/cli/run-pagefind.js), through CliExit like any failed build. Tested in
|
|
349
351
|
// scripts/cli-exit-flush.test.js.
|
|
350
352
|
program
|
|
351
353
|
.parseAsync(process.argv)
|
package/package.json
CHANGED
package/src/cli/build.js
CHANGED
|
@@ -2,7 +2,8 @@ import { canonicalPath } from '../lib/canonical-path.js';
|
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { runAstro } from './run-astro.js';
|
|
5
|
-
import { runPagefind } from './run-pagefind.js';
|
|
5
|
+
import { runPagefind, PagefindEmptyFilesError } from './run-pagefind.js';
|
|
6
|
+
import { EX_TEMPFAIL } from './build-auth.js';
|
|
6
7
|
import { preflightCheck, sameDriveCheck, writableInstallCheck } from './preflight.js';
|
|
7
8
|
import { generateApiPages } from './generate-api-pages.js';
|
|
8
9
|
import { writeRedirectsFile } from './write-redirects-file.js';
|
|
@@ -215,10 +216,20 @@ export async function runBuild({ contentDir, packageRoot, verbose = false, draft
|
|
|
215
216
|
const indexing = step('Indexing search');
|
|
216
217
|
let searchSpaces = [];
|
|
217
218
|
try {
|
|
218
|
-
|
|
219
|
+
const search = await runPagefind(siteDir, { packageRoot });
|
|
220
|
+
searchSpaces = search.spaces;
|
|
221
|
+
if (search.rewritten) notes.push(`Search: Pagefind left ${plural(search.rewritten.length, 'index file')} empty (${search.rewritten.join(', ')}); the indexes were written again, whole.`);
|
|
219
222
|
} catch (err) {
|
|
220
223
|
indexing.fail('Search indexing failed');
|
|
221
224
|
log.line();
|
|
225
|
+
// A file of the index left empty twice (run-pagefind.js): a passing
|
|
226
|
+
// fault, not the pages' - exit 75 (EX_TEMPFAIL), the code the WriteDocs
|
|
227
|
+
// platform tries a build again on, as when no key server answers.
|
|
228
|
+
if (err instanceof PagefindEmptyFilesError) {
|
|
229
|
+
log.error(`The search index is incomplete: Pagefind wrote ${plural(err.files.length, 'file')} empty, and again when written a second time - ${err.files.join(', ')}.`);
|
|
230
|
+
log.detail(color.dim('This is a fault on the machine running the build, not in your pages. Build again.'));
|
|
231
|
+
throw new CliExit(EX_TEMPFAIL);
|
|
232
|
+
}
|
|
222
233
|
log.line(color.dim(err.message));
|
|
223
234
|
throw new CliExit(1);
|
|
224
235
|
}
|
|
@@ -4,7 +4,7 @@ import SwaggerParser from '@apidevtools/swagger-parser';
|
|
|
4
4
|
import matter from 'gray-matter';
|
|
5
5
|
import { writedocsTempDir } from '../lib/writedocs-temp-dir.js';
|
|
6
6
|
import { parseOpenApiRef, openApiOperationKey, specDirName } from '../lib/openapi-ref.js';
|
|
7
|
-
import { findAllPages } from '../lib/pages.js';
|
|
7
|
+
import { findAllPages, projectPageIds } from '../lib/pages.js';
|
|
8
8
|
import { projectIgnore } from '../lib/project-ignore.js';
|
|
9
9
|
import { readConfigText } from '../lib/config-file.js';
|
|
10
10
|
import { HTTP_METHODS, collectOpenApiGroups } from '../lib/openapi-spec.js';
|
|
@@ -12,6 +12,7 @@ import { operationMetaDescription, plainLine, truncateAtWord } from '../lib/meta
|
|
|
12
12
|
import { movedApiPagesFile, untaggedRedirects } from '../lib/moved-api-pages.js';
|
|
13
13
|
import { scalarReferencesFile, scalarDocumentFile } from '../lib/scalar-reference.js';
|
|
14
14
|
import { scalarOperationAnchor } from '../lib/scalar-anchors.js';
|
|
15
|
+
import { scalarRootPathMessage, scalarPagePathMessage } from '../lib/scalar-path.js';
|
|
15
16
|
import { canonicalPath } from '../lib/canonical-path.js';
|
|
16
17
|
|
|
17
18
|
|
|
@@ -253,19 +254,10 @@ async function generateApiPagesForGroup({ contentDir, generatedDocsDir, group, o
|
|
|
253
254
|
}
|
|
254
255
|
// Scalar's reference (lib/scalar-reference.js) is a page of its own, at the
|
|
255
256
|
// group's path - which then can't be the site's root, nor a page's.
|
|
257
|
+
// `writedocs validate` refuses both with the same words (lib/scalar-path.js).
|
|
256
258
|
const scalar = group.openapi.renderer === 'scalar';
|
|
257
|
-
if (scalar && !pathPrefix) {
|
|
258
|
-
|
|
259
|
-
`[writedocs] writedocs.json's "${group.group}" group has openapi.renderer "scalar" and openapi.path "${group.openapi.path}" - ` +
|
|
260
|
-
'the reference is a page at that path, so give it one of its own, like "/api".'
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
if (scalar && pageIds.has(pathPrefix)) {
|
|
264
|
-
throw new Error(
|
|
265
|
-
`[writedocs] writedocs.json's "${group.group}" group has openapi.renderer "scalar" - its reference is the page at /${pathPrefix}/, ` +
|
|
266
|
-
'and a page of the project is already there. Move that page or give the group another openapi.path.'
|
|
267
|
-
);
|
|
268
|
-
}
|
|
259
|
+
if (scalar && !pathPrefix) throw new Error(`[writedocs] ${scalarRootPathMessage(group.group, group.openapi.path)}`);
|
|
260
|
+
if (scalar && pageIds.has(pathPrefix)) throw new Error(`[writedocs] ${scalarPagePathMessage(group.group, pathPrefix)}`);
|
|
269
261
|
|
|
270
262
|
const spec = await dereferenceSpec(specPath, sources);
|
|
271
263
|
const operations = buildOperations(spec);
|
|
@@ -438,7 +430,7 @@ export async function planApiPages({ contentDir, drafts }) {
|
|
|
438
430
|
// Groups shown as Scalar's reference (lib/scalar-reference.js), and the
|
|
439
431
|
// addresses of the project's own pages - a reference can't take one.
|
|
440
432
|
const references = [];
|
|
441
|
-
const pageIds = groups.some((g) => g.openapi.renderer === 'scalar') ? projectPageIds(contentDir, drafts) : new Set();
|
|
433
|
+
const pageIds = groups.some((g) => g.openapi.renderer === 'scalar') ? projectPageIds(contentDir, { drafts }) : new Set();
|
|
442
434
|
for (const group of groups) {
|
|
443
435
|
await generateApiPagesForGroup({ contentDir, generatedDocsDir, group, overrides, summary, moves, files, sources, references, pageIds });
|
|
444
436
|
}
|
|
@@ -459,20 +451,6 @@ export async function planApiPages({ contentDir, drafts }) {
|
|
|
459
451
|
return { summary, files, roots };
|
|
460
452
|
}
|
|
461
453
|
|
|
462
|
-
/** The addresses (file ids) of the project's own pages: its path without the
|
|
463
|
-
* extension or a trailing /index, or its frontmatter `slug`. */
|
|
464
|
-
function projectPageIds(contentDir, drafts) {
|
|
465
|
-
const ids = new Set();
|
|
466
|
-
for (const rel of findAllPages(contentDir, { drafts })) {
|
|
467
|
-
ids.add(rel.replace(/\.mdx?$/i, '').replace(/\/index$/, ''));
|
|
468
|
-
try {
|
|
469
|
-
const { data } = matter(fs.readFileSync(path.join(contentDir, rel), 'utf-8'));
|
|
470
|
-
if (typeof data?.slug === 'string') ids.add(data.slug.replace(/^\/+|\/+$/g, ''));
|
|
471
|
-
} catch {}
|
|
472
|
-
}
|
|
473
|
-
return ids;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
454
|
/** Text for a Markdown link's label inside MDX: what MDX or Markdown would
|
|
477
455
|
* read as syntax is escaped. */
|
|
478
456
|
function markdownText(text) {
|
package/src/cli/run-pagefind.js
CHANGED
|
@@ -63,9 +63,64 @@ function resolvePagefindBin(packageRoot) {
|
|
|
63
63
|
/**
|
|
64
64
|
* Returns the ids of the search spaces it wrote, each in
|
|
65
65
|
* pagefind-spaces/<id>/ - the build lists a protected one's folder in
|
|
66
|
-
* dist/.writedocs/access.json (lib/protected-sections.js)
|
|
66
|
+
* dist/.writedocs/access.json (lib/protected-sections.js) - and, when the
|
|
67
|
+
* indexes had to be written again, `rewritten`: the files left empty the
|
|
68
|
+
* first time.
|
|
67
69
|
*/
|
|
68
|
-
export async function runPagefind(siteDir, { packageRoot }) {
|
|
70
|
+
export async function runPagefind(siteDir, { packageRoot, index = writeIndexes }) {
|
|
71
|
+
// An index file Pagefind left empty (none is on purpose): its Node API's
|
|
72
|
+
// writeFiles() resolves while files are still being written, and close()
|
|
73
|
+
// then ends its process - in gerador/14's probe, 3 of 60 writes followed
|
|
74
|
+
// by close() kept a file at 0 bytes (wasm.unknown.pagefind,
|
|
75
|
+
// pagefind-entry.json...), which breaks that index's search without a
|
|
76
|
+
// word. So: written again once, and if a file is still empty, the build
|
|
77
|
+
// fails as a passing fault - PagefindEmptyFilesError, exit 75 (build.js).
|
|
78
|
+
// `index` is writeIndexes unless a test passes a stand-in.
|
|
79
|
+
const result = await index(siteDir, packageRoot);
|
|
80
|
+
const empty = emptyPagefindFiles(siteDir);
|
|
81
|
+
if (!empty.length) return result;
|
|
82
|
+
removePagefindFolders(siteDir);
|
|
83
|
+
const again = await index(siteDir, packageRoot);
|
|
84
|
+
const still = emptyPagefindFiles(siteDir);
|
|
85
|
+
if (still.length) throw new PagefindEmptyFilesError(still);
|
|
86
|
+
return { ...again, rewritten: empty };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The search indexes' folders in a built site. */
|
|
90
|
+
const PAGEFIND_FOLDERS = ['pagefind', 'pagefind-spaces'];
|
|
91
|
+
|
|
92
|
+
/** Every file of 0 bytes in the site's search indexes, as paths in the site
|
|
93
|
+
* with forward slashes. Exported for scripts/run-pagefind.test.js. */
|
|
94
|
+
export function emptyPagefindFiles(siteDir) {
|
|
95
|
+
const empty = [];
|
|
96
|
+
const walk = (dir) => {
|
|
97
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
98
|
+
const full = path.join(dir, entry.name);
|
|
99
|
+
if (entry.isDirectory()) walk(full);
|
|
100
|
+
else if (fs.statSync(full).size === 0) empty.push(path.relative(siteDir, full).split(path.sep).join('/'));
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
for (const folder of PAGEFIND_FOLDERS) if (fs.existsSync(path.join(siteDir, folder))) walk(path.join(siteDir, folder));
|
|
104
|
+
return empty.sort();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function removePagefindFolders(siteDir) {
|
|
108
|
+
for (const folder of PAGEFIND_FOLDERS) fs.rmSync(path.join(siteDir, folder), { recursive: true, force: true });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Pagefind left a file empty twice: the search index isn't whole. A fault
|
|
112
|
+
* of the machine or the moment, not of the pages - the build exits 75, the
|
|
113
|
+
* code the WriteDocs platform tries again on. */
|
|
114
|
+
export class PagefindEmptyFilesError extends Error {
|
|
115
|
+
constructor(files) {
|
|
116
|
+
super(`Pagefind wrote ${files.length === 1 ? 'a search index file' : `${files.length} search index files`} empty, twice: ${files.join(', ')}`);
|
|
117
|
+
this.files = files;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Writes the site's search indexes - `{ spaces }`, the ids of the search
|
|
122
|
+
* spaces written. */
|
|
123
|
+
async function writeIndexes(siteDir, packageRoot) {
|
|
69
124
|
// With a hidden or protected space, or a page whose result leads elsewhere
|
|
70
125
|
// - the operation page of a Scalar group, whose result is its operation in
|
|
71
126
|
// the reference (lib/scalar-reference.js) - the indexes are written by
|
|
@@ -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
|
|
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-
|
|
33
|
-
|
|
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
|
-
<
|
|
36
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
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-
|
|
78
|
-
|
|
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>
|
package/src/lib/config-schema.js
CHANGED
|
@@ -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([
|
package/src/lib/config-schema.ts
CHANGED
|
@@ -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';
|
package/src/lib/content-check.js
CHANGED
|
@@ -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
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Where an OpenAPI group with `openapi.renderer: "scalar"` may put its
|
|
2
|
+
// reference: it's a page of its own at the group's `openapi.path`
|
|
3
|
+
// (lib/scalar-reference.js), so that path can't be the site's root, nor the
|
|
4
|
+
// address of one of the project's pages. One wording for every command that
|
|
5
|
+
// refuses it: the build, `dev` and `broken-links` (generate-api-pages.js),
|
|
6
|
+
// and `writedocs validate` - the root in writedocs.json's schema
|
|
7
|
+
// (config-schema.ts, which the WriteDocs platform validates writedocs.json
|
|
8
|
+
// with too), a page's address in its pages pass (content-check.js), the only
|
|
9
|
+
// one with the project's files.
|
|
10
|
+
//
|
|
11
|
+
// Plain JavaScript with no Node import: config-schema.ts imports it, and the
|
|
12
|
+
// platform's Worker runs that.
|
|
13
|
+
|
|
14
|
+
/** `openapi.path` without its slashes - "" for the root. */
|
|
15
|
+
export function scalarPathPrefix(rawPath) {
|
|
16
|
+
return String(rawPath ?? '').replace(/^\/+|\/+$/g, '');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The refusal of a "scalar" group at the root. */
|
|
20
|
+
export function scalarRootPathMessage(group, rawPath) {
|
|
21
|
+
return (
|
|
22
|
+
`writedocs.json's "${group}" group has openapi.renderer "scalar" and openapi.path "${rawPath}" - ` +
|
|
23
|
+
'the reference is a page at that path, so give it one of its own, like "/api".'
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The refusal of a "scalar" group at a page's address (`prefix`, no slashes). */
|
|
28
|
+
export function scalarPagePathMessage(group, prefix) {
|
|
29
|
+
return (
|
|
30
|
+
`writedocs.json's "${group}" group has openapi.renderer "scalar" - its reference is the page at /${prefix}/, ` +
|
|
31
|
+
'and a page of the project is already there. Move that page or give the group another openapi.path.'
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Every OpenAPI group of a `navigation` value with `renderer: "scalar"`,
|
|
36
|
+
* with its path from `navigation` (keys and indexes). Any shape - it walks
|
|
37
|
+
* every object and array, so it finds a group wherever the schema allows
|
|
38
|
+
* one. */
|
|
39
|
+
export function scalarGroups(navigation) {
|
|
40
|
+
const found = [];
|
|
41
|
+
const walk = (node, trail) => {
|
|
42
|
+
if (!node || typeof node !== 'object') return;
|
|
43
|
+
if (Array.isArray(node)) return node.forEach((child, i) => walk(child, [...trail, i]));
|
|
44
|
+
const openapi = node.openapi;
|
|
45
|
+
if (typeof node.group === 'string' && openapi && typeof openapi === 'object' && !Array.isArray(openapi)) {
|
|
46
|
+
if (openapi.renderer === 'scalar' && typeof openapi.path === 'string') found.push({ group: node.group, path: openapi.path, trail });
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
for (const [key, value] of Object.entries(node)) walk(value, [...trail, key]);
|
|
50
|
+
};
|
|
51
|
+
walk(navigation, []);
|
|
52
|
+
return found;
|
|
53
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// What the skeleton of Scalar's API reference shows while Scalar loads
|
|
2
|
+
// (ScalarReference.astro): the parts of the reference's first screen whose
|
|
3
|
+
// size depends on the spec - its title, the two badges, the first paragraph
|
|
4
|
+
// of its description, and which of the Server and Authentication blocks it
|
|
5
|
+
// has - so the skeleton takes the same room as the reference that replaces
|
|
6
|
+
// it. The rest of the skeleton (the sidebar, the bar of operations, the
|
|
7
|
+
// Client Libraries block) is the same for every spec.
|
|
8
|
+
//
|
|
9
|
+
// The text is drawn as bars the shape of the text (transparent, in the
|
|
10
|
+
// reference's own sizes): a title or description that wraps to two lines in
|
|
11
|
+
// the reference wraps to two lines in the skeleton too.
|
|
12
|
+
//
|
|
13
|
+
// Plain JavaScript so the tests can import it.
|
|
14
|
+
import { plainLine, truncateAtWord } from './meta-description.js';
|
|
15
|
+
|
|
16
|
+
/** The longest description paragraph the skeleton carries, in characters:
|
|
17
|
+
* a few lines - the page shouldn't carry a long introduction twice. */
|
|
18
|
+
export const SKELETON_TEXT_LENGTH = 400;
|
|
19
|
+
|
|
20
|
+
/** Scalar's Authentication block, by the type of the scheme it shows first:
|
|
21
|
+
* an HTTP bearer token is one row (67px, measured); an API key - and every
|
|
22
|
+
* other type - more (100px, an API key's, measured). */
|
|
23
|
+
export const AUTH_BLOCK_HEIGHT = { bearer: 67, other: 100 };
|
|
24
|
+
|
|
25
|
+
/** The first paragraph of a Markdown description, as plain text. */
|
|
26
|
+
function firstParagraph(markdown) {
|
|
27
|
+
const blocks = String(markdown ?? '')
|
|
28
|
+
.replace(/\r\n/g, '\n')
|
|
29
|
+
.split(/\n\s*\n/)
|
|
30
|
+
.map((block) => plainLine(block))
|
|
31
|
+
.filter(Boolean);
|
|
32
|
+
return blocks.length ? truncateAtWord(blocks[0], SKELETON_TEXT_LENGTH) : '';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The scheme Scalar selects first: the first one the spec's top-level
|
|
36
|
+
* `security` asks for, else the first it defines. */
|
|
37
|
+
function firstScheme(spec) {
|
|
38
|
+
const schemes = spec?.components?.securitySchemes ?? spec?.securityDefinitions ?? {};
|
|
39
|
+
const names = Object.keys(schemes);
|
|
40
|
+
if (!names.length) return null;
|
|
41
|
+
const asked = (Array.isArray(spec?.security) ? spec.security : []).flatMap((requirement) => Object.keys(requirement ?? {}));
|
|
42
|
+
const name = asked.find((n) => n in schemes) ?? names[0];
|
|
43
|
+
return schemes[name] ?? null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The skeleton's spec-dependent parts, from the bundled spec. */
|
|
47
|
+
export function scalarSkeleton(spec) {
|
|
48
|
+
const info = spec?.info ?? {};
|
|
49
|
+
const openapi = spec?.openapi ?? spec?.swagger;
|
|
50
|
+
const scheme = firstScheme(spec);
|
|
51
|
+
const bearer = scheme?.type === 'http' && String(scheme.scheme ?? '').toLowerCase() === 'bearer';
|
|
52
|
+
return {
|
|
53
|
+
title: plainLine(info.title) || 'API',
|
|
54
|
+
badges: [info.version ? String(info.version) : null, openapi ? `OpenAPI ${openapi}` : null].filter(Boolean),
|
|
55
|
+
description: firstParagraph(info.description),
|
|
56
|
+
server: Array.isArray(spec?.servers) ? spec.servers.length > 0 : Boolean(spec?.host),
|
|
57
|
+
auth: scheme ? (bearer ? AUTH_BLOCK_HEIGHT.bearer : AUTH_BLOCK_HEIGHT.other) : 0,
|
|
58
|
+
};
|
|
59
|
+
}
|
package/src/lib/scalar-theme.js
CHANGED
|
@@ -114,7 +114,7 @@ export const SCALAR_REFERENCE_RULES = [
|
|
|
114
114
|
[`${R} .operation-title`, '--font-size: 1.5rem; --font-weight: 700; line-height: 1.6;', "an operation's title: the site's h2"],
|
|
115
115
|
[`${R} .markdown`, '--markdown-line-height: 1.6; color: var(--wd-text);', "text: the site's line height and text color (Scalar's is the heading color)"],
|
|
116
116
|
[`${R} .section`, 'padding-block: 2.75rem;', "the space around a section: the site's before an h2"],
|
|
117
|
-
[`${R} .introduction-section`, 'padding-top:
|
|
117
|
+
[`${R} .introduction-section`, 'padding-top: 3.5rem;', "room above the reference's title for its version badges (placed above it, the next rule), and the title below them"],
|
|
118
118
|
[`${R} .introduction-section .section-content`, 'position: relative;', 'so the version badges can sit above the title'],
|
|
119
119
|
[`${R} .introduction-section .section-content > :first-child:has(.badge)`, 'position: absolute; bottom: 100%; margin-bottom: 0.5rem;', "the version badges above the title, without pushing it down"],
|
|
120
120
|
];
|
|
@@ -128,13 +128,10 @@ export const SCALAR_REFERENCE_RULES = [
|
|
|
128
128
|
* custom header's height. The content's left margin is the site's article
|
|
129
129
|
* padding when Scalar's own container query would give it 60px. */
|
|
130
130
|
export function scalarReferenceThemeCss() {
|
|
131
|
-
//
|
|
132
|
-
//
|
|
133
|
-
//
|
|
134
|
-
//
|
|
135
|
-
// this function, not at the top of the module: a top-level call can't be
|
|
136
|
-
// dropped by the bundler, and was left in the Try it's chunk.
|
|
137
|
-
const CONTAINER_QUERY = ['@', 'container'].join('');
|
|
131
|
+
// Words here are safe from Tailwind: src/lib isn't read for class names
|
|
132
|
+
// (src/styles/global.css). Before it was left out, this at-rule's name and
|
|
133
|
+
// the word in the focus ring's reason above each put a rule into every
|
|
134
|
+
// site's stylesheet (gerador/13 and 14).
|
|
138
135
|
const declarations = (vars) =>
|
|
139
136
|
Object.entries(vars)
|
|
140
137
|
.map(([name, value]) => ` ${name}: ${value};`)
|
|
@@ -157,15 +154,17 @@ ${declarations(SCALAR_REFERENCE_VARIABLES)}
|
|
|
157
154
|
}
|
|
158
155
|
}
|
|
159
156
|
` +
|
|
160
|
-
//
|
|
161
|
-
//
|
|
157
|
+
// In the site's phone layout (base.css, 860px) Scalar's bar of
|
|
158
|
+
// operations is right above the title, and the badges above the title
|
|
159
|
+
// need less room: 2.25rem under the bar, as a page's title sits under
|
|
160
|
+
// the site's top bar there.
|
|
162
161
|
`@media (max-width: 860px) {
|
|
163
162
|
${R} .introduction-section {
|
|
164
163
|
padding-top: 2.25rem;
|
|
165
164
|
}
|
|
166
165
|
}
|
|
167
166
|
` +
|
|
168
|
-
|
|
167
|
+
`@container references-container (min-width: 901px) {
|
|
169
168
|
${R} .section-container {
|
|
170
169
|
padding-inline: 1.5rem;
|
|
171
170
|
}
|
package/src/styles/global.css
CHANGED
|
@@ -29,6 +29,20 @@
|
|
|
29
29
|
../../ is the package's folder. */
|
|
30
30
|
@source not "../../node_modules";
|
|
31
31
|
|
|
32
|
+
/* Nor the package's modules that never become a page's markup: the
|
|
33
|
+
build's own Node code (src/cli, bin) and src/lib, whose modules make
|
|
34
|
+
pages, styles and data but give no element one of Tailwind's classes.
|
|
35
|
+
A word in one of them - in a comment, a message, a string of CSS - made
|
|
36
|
+
a rule for it in every site's stylesheet (gerador/13 and 14).
|
|
37
|
+
scripts/tailwind-sources.test.js holds the list of rules the package's
|
|
38
|
+
files make, and fails on a new one. */
|
|
39
|
+
@source not "../lib";
|
|
40
|
+
@source not "../cli";
|
|
41
|
+
@source not "../../bin";
|
|
42
|
+
/* Kept, so no site's stylesheet changes: every one has had this rule
|
|
43
|
+
since a word in src/cli made it. */
|
|
44
|
+
@source inline("isolate");
|
|
45
|
+
|
|
32
46
|
/* Tailwind's `dark:` variant follows the site's own theme toggle
|
|
33
47
|
([data-theme="dark"] on <html>, set by BaseLayout.astro), not the OS
|
|
34
48
|
setting it defaults to - otherwise Mintlify-style light/dark image
|