@writedocs/generator 0.17.0 → 0.19.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/astro.config.mjs +38 -4
- package/package.json +4 -1
- package/src/cli/build.js +3 -2
- package/src/cli/generate-api-pages.js +94 -3
- package/src/cli/run-pagefind.js +47 -15
- package/src/cli/write-redirects-file.js +5 -3
- package/src/components/ApiReferencePanel.astro +16 -3
- package/src/components/NoScalarReference.astro +6 -0
- package/src/components/NoScalarTryIt.astro +5 -0
- package/src/components/ScalarReference.astro +80 -0
- package/src/components/ScalarTryIt.astro +76 -0
- package/src/layout/BaseLayout.astro +7 -0
- package/src/lib/config-schema.js +17 -3
- package/src/lib/config-schema.ts +16 -2
- package/src/lib/config.ts +17 -2
- package/src/lib/json-schema-descriptions.js +2 -0
- package/src/lib/moved-api-pages.js +16 -1
- package/src/lib/openapi-render.ts +5 -0
- package/src/lib/scalar-anchors.js +56 -0
- package/src/lib/scalar-document.js +63 -0
- package/src/lib/scalar-fetch.js +89 -0
- package/src/lib/scalar-reference-alias.js +32 -0
- package/src/lib/scalar-reference.js +105 -0
- package/src/lib/scalar-theme.js +175 -0
- package/src/lib/scalar-try-it-alias.js +31 -0
- package/src/pages/[...slug].astro +80 -9
- package/src/scripts/scalar-reference.ts +151 -0
- package/src/scripts/scalar-try-it.ts +145 -0
- package/writedocs.schema.json +21 -1
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// The look of Scalar's "Try it" on a writedocs site: Scalar's documented
|
|
2
|
+
// theme variables (--scalar-*, scalar.com/products/api-references/themes),
|
|
3
|
+
// each set from one of the site's own tokens (--wd-*, BaseLayout.astro) -
|
|
4
|
+
// so the modal has the site's colors (writedocs.json `styles.colors`), its
|
|
5
|
+
// body font, and follows its light/dark mode as it changes: the tokens
|
|
6
|
+
// change under [data-theme], and src/scripts/scalar-try-it.ts swaps the
|
|
7
|
+
// host's .light-mode/.dark-mode class (which Scalar's own rules key off) in
|
|
8
|
+
// step with it.
|
|
9
|
+
//
|
|
10
|
+
// Scalar's own fonts (Inter, JetBrains Mono) come from its font CDN; they're
|
|
11
|
+
// never loaded here - the body font is the site's, the code font the
|
|
12
|
+
// system's monospace stack.
|
|
13
|
+
//
|
|
14
|
+
// Plain JavaScript so the tests can import it. A string, injected with
|
|
15
|
+
// Scalar's CSS when a reader first opens Try it - no page carries it before.
|
|
16
|
+
|
|
17
|
+
/** The class of the element the modal is mounted in. */
|
|
18
|
+
export const SCALAR_HOST_CLASS = 'wd-scalar-host';
|
|
19
|
+
|
|
20
|
+
const MONO = 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace';
|
|
21
|
+
|
|
22
|
+
/** Scalar variable -> its value from the site's tokens. */
|
|
23
|
+
export const SCALAR_THEME_VARIABLES = {
|
|
24
|
+
'--scalar-font': 'var(--wd-font-family-body, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif)',
|
|
25
|
+
'--scalar-font-code': MONO,
|
|
26
|
+
'--scalar-background-1': 'var(--wd-background)',
|
|
27
|
+
'--scalar-background-2': 'var(--wd-surface)',
|
|
28
|
+
'--scalar-background-3': 'color-mix(in srgb, var(--wd-text) 8%, var(--wd-surface))',
|
|
29
|
+
'--scalar-background-4': 'color-mix(in srgb, var(--wd-text) 12%, var(--wd-surface))',
|
|
30
|
+
'--scalar-background-accent': 'color-mix(in srgb, var(--wd-primary) 14%, transparent)',
|
|
31
|
+
'--scalar-color-1': 'var(--wd-heading)',
|
|
32
|
+
'--scalar-color-2': 'var(--wd-text)',
|
|
33
|
+
'--scalar-color-3': 'var(--wd-text-muted)',
|
|
34
|
+
'--scalar-color-accent': 'var(--wd-primary)',
|
|
35
|
+
'--scalar-border-color': 'var(--wd-border)',
|
|
36
|
+
'--scalar-link-color': 'var(--wd-primary)',
|
|
37
|
+
'--scalar-link-color-hover': 'var(--wd-primary)',
|
|
38
|
+
'--scalar-button-1': 'var(--wd-primary)',
|
|
39
|
+
'--scalar-button-1-color': 'var(--wd-on-primary)',
|
|
40
|
+
'--scalar-button-1-hover': 'color-mix(in srgb, var(--wd-primary) 88%, var(--wd-text))',
|
|
41
|
+
'--scalar-sidebar-background-1': 'var(--wd-background)',
|
|
42
|
+
'--scalar-sidebar-color-1': 'var(--wd-heading)',
|
|
43
|
+
'--scalar-sidebar-color-2': 'var(--wd-text-muted)',
|
|
44
|
+
'--scalar-sidebar-color-active': 'var(--wd-primary)',
|
|
45
|
+
'--scalar-sidebar-border-color': 'var(--wd-border)',
|
|
46
|
+
'--scalar-sidebar-item-hover-background': 'var(--wd-surface)',
|
|
47
|
+
'--scalar-sidebar-item-hover-color': 'var(--wd-heading)',
|
|
48
|
+
'--scalar-sidebar-item-active-background': 'color-mix(in srgb, var(--wd-primary) 12%, transparent)',
|
|
49
|
+
'--scalar-sidebar-search-background': 'var(--wd-surface)',
|
|
50
|
+
'--scalar-sidebar-search-border-color': 'var(--wd-border)',
|
|
51
|
+
'--scalar-sidebar-search-color': 'var(--wd-text)',
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** The CSS: the variables on the host, in either mode - two classes, so it
|
|
55
|
+
* wins over Scalar's own `.light-mode`/`.dark-mode` defaults on the same
|
|
56
|
+
* element, whatever the order the two style sheets end up in. And the
|
|
57
|
+
* host's stacking level: Scalar's modal is position: fixed with no
|
|
58
|
+
* z-index, so without one the site's sticky top bar (z-index 10) covered
|
|
59
|
+
* its header. 100 is writedocs' own Try-it modal's level
|
|
60
|
+
* (.wd-api-modal-overlay, ApiReferencePanel.astro). */
|
|
61
|
+
export function scalarThemeCss() {
|
|
62
|
+
const body = Object.entries(SCALAR_THEME_VARIABLES)
|
|
63
|
+
.map(([name, value]) => ` ${name}: ${value};`)
|
|
64
|
+
.join('\n');
|
|
65
|
+
return (
|
|
66
|
+
`.${SCALAR_HOST_CLASS} {\n position: relative;\n z-index: 100;\n}\n` +
|
|
67
|
+
`.${SCALAR_HOST_CLASS}.light-mode,\n.${SCALAR_HOST_CLASS}.dark-mode {\n${body}\n}\n`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The element Scalar's API reference (`openapi.renderer: "scalar"`) is
|
|
72
|
+
* mounted in - ScalarReference.astro. */
|
|
73
|
+
export const SCALAR_REFERENCE_CLASS = 'wd-scalar-reference';
|
|
74
|
+
|
|
75
|
+
/** The reference's own variables, on its element too: Scalar's sidebar and
|
|
76
|
+
* sections take the measurements of the site's sidebar (NavTree.astro,
|
|
77
|
+
* Sidebar.astro) wherever Scalar has a variable for one. */
|
|
78
|
+
export const SCALAR_REFERENCE_VARIABLES = {
|
|
79
|
+
// The site's sidebar: 260px wide (.wd-sidebar-col), padded 1.5rem 1rem
|
|
80
|
+
// 1.5rem 0 (.wd-sidebar).
|
|
81
|
+
'--scalar-sidebar-width': '260px',
|
|
82
|
+
'--scalar-sidebar-padding': '1.5rem 1rem 1.5rem 0',
|
|
83
|
+
// An entry: the site's text, weight 400; hovered, the heading color on a
|
|
84
|
+
// 6% tint of the primary; selected, the primary on 12%, weight 500.
|
|
85
|
+
'--scalar-sidebar-color-2': 'var(--wd-nav-text)',
|
|
86
|
+
'--scalar-sidebar-font-weight': '400',
|
|
87
|
+
'--scalar-sidebar-font-weight-active': '500',
|
|
88
|
+
'--scalar-sidebar-item-hover-color': 'var(--wd-nav-title)',
|
|
89
|
+
'--scalar-sidebar-item-hover-background': 'color-mix(in srgb, var(--wd-primary) 6%, transparent)',
|
|
90
|
+
'--scalar-sidebar-item-active-background': 'color-mix(in srgb, var(--wd-primary) 12%, transparent)',
|
|
91
|
+
'--scalar-sidebar-color-active': 'var(--wd-primary)',
|
|
92
|
+
// The vertical guide beside nested entries: the site's border color (the
|
|
93
|
+
// selected entry's own bar is its border, below).
|
|
94
|
+
'--scalar-sidebar-indent-border': 'var(--wd-border)',
|
|
95
|
+
'--scalar-sidebar-indent-border-hover': 'var(--wd-border)',
|
|
96
|
+
'--scalar-sidebar-indent-border-active': 'var(--wd-border)',
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** Where Scalar has no variable: `[selector, declarations, why]`, every
|
|
100
|
+
* selector inside the reference's element, none touching the site's own
|
|
101
|
+
* layout. Scalar's classes they rely on are listed in
|
|
102
|
+
* docs/dev/docs/scalar-reference.mdx, to check when updating Scalar. No
|
|
103
|
+
* !important: each wins on specificity, or on coming after Scalar's CSS in
|
|
104
|
+
* the same <style> (src/scripts/scalar-reference.ts). */
|
|
105
|
+
const R = `.${SCALAR_REFERENCE_CLASS}`;
|
|
106
|
+
const ENTRY = `${R} a:has(> .scalar-sidebar-indent)`;
|
|
107
|
+
export const SCALAR_REFERENCE_RULES = [
|
|
108
|
+
[ENTRY, 'padding: 0.2rem 0.75rem; font-size: 0.9rem; line-height: 1.6; border-radius: 0 0.4rem 0.4rem 0; border-left: 1px solid transparent;', "a sidebar entry (Scalar's link holding its indent): the site's entry box - .wd-nav-list a"],
|
|
109
|
+
[`${ENTRY}:hover, ${R} a:has(> .scalar-sidebar-indent-selected)`, 'border-left-color: var(--wd-primary);', "the bar at the left of a hovered or the selected entry, as the site's"],
|
|
110
|
+
[`${ENTRY}:focus-visible`, 'outline: revert; outline-offset: revert;', "the keyboard focus ring: the browser's own, as on the site's links (Scalar draws its own solid one)"],
|
|
111
|
+
[`${ENTRY} + button`, 'right: calc(0.75rem - 4px); color: var(--wd-text-muted);', "a group's arrow: where the site's chevron is, in its color (the button has 4px of padding)"],
|
|
112
|
+
[`${ENTRY} + button > div, ${ENTRY} + button svg`, 'width: 10px; height: 10px;', "the arrow's size: the site's chevron's (its box too, so it sits where the chevron does)"],
|
|
113
|
+
[`${R} .section-header`, '--font-size: 2rem; --font-weight: 700; line-height: 1.6;', "the reference's title and each section's: the site's h1"],
|
|
114
|
+
[`${R} .operation-title`, '--font-size: 1.5rem; --font-weight: 700; line-height: 1.6;', "an operation's title: the site's h2"],
|
|
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
|
+
[`${R} .section`, 'padding-block: 2.75rem;', "the space around a section: the site's before an h2"],
|
|
117
|
+
[`${R} .introduction-section`, 'padding-top: 3.5rem;', "the reference's title where the site's h1 is"],
|
|
118
|
+
[`${R} .introduction-section .section-content`, 'position: relative;', 'so the version badges can sit above the title'],
|
|
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
|
+
];
|
|
121
|
+
|
|
122
|
+
/** The reference's CSS: the same variables on its element - Scalar sets its
|
|
123
|
+
* own on <body>'s .light-mode/.dark-mode class (src/scripts/scalar-
|
|
124
|
+
* reference.ts keeps that class in step with the site's theme), and these,
|
|
125
|
+
* closer to every element of the reference, win - then its own variables,
|
|
126
|
+
* and the rules above. And where its sticky parts stop: below the site's
|
|
127
|
+
* top bar (--wd-topbar-offset, topbar-offset.ts), which Scalar reads as a
|
|
128
|
+
* custom header's height. The content's left margin is the site's article
|
|
129
|
+
* padding when Scalar's own container query would give it 60px. */
|
|
130
|
+
export function scalarReferenceThemeCss() {
|
|
131
|
+
// The at-rule of a container query, written in two parts: Tailwind scans
|
|
132
|
+
// this package's own files for class names, and the at-rule's name in one
|
|
133
|
+
// piece is also the name of one of its classes - whose rule then went into
|
|
134
|
+
// every site's stylesheet (gerador/13, found by comparing builds). Inside
|
|
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('');
|
|
138
|
+
const declarations = (vars) =>
|
|
139
|
+
Object.entries(vars)
|
|
140
|
+
.map(([name, value]) => ` ${name}: ${value};`)
|
|
141
|
+
.join('\n');
|
|
142
|
+
const rules = SCALAR_REFERENCE_RULES.map(([selector, body]) => `${selector} {\n ${body}\n}`).join('\n');
|
|
143
|
+
return (
|
|
144
|
+
`${R} {
|
|
145
|
+
--scalar-custom-header-height: var(--wd-topbar-offset, 4rem);
|
|
146
|
+
${declarations(SCALAR_THEME_VARIABLES)}
|
|
147
|
+
${declarations(SCALAR_REFERENCE_VARIABLES)}
|
|
148
|
+
}
|
|
149
|
+
` +
|
|
150
|
+
`${rules}
|
|
151
|
+
` +
|
|
152
|
+
// Below Scalar's own breakpoint its sidebar is a menu under its bar - the
|
|
153
|
+
// site's own menu has no padding above its first entry either.
|
|
154
|
+
`@media (width < 1000px) {
|
|
155
|
+
${R} {
|
|
156
|
+
--scalar-sidebar-padding: 0 1rem 1.5rem 0;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
` +
|
|
160
|
+
// The site's phone layout (base.css, 860px) puts the page's title 12px
|
|
161
|
+
// lower under its top bar: the reference's title follows.
|
|
162
|
+
`@media (max-width: 860px) {
|
|
163
|
+
${R} .introduction-section {
|
|
164
|
+
padding-top: 2.25rem;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
` +
|
|
168
|
+
`${CONTAINER_QUERY} references-container (min-width: 901px) {
|
|
169
|
+
${R} .section-container {
|
|
170
|
+
padding-inline: 1.5rem;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
`
|
|
174
|
+
);
|
|
175
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Which component the alias `writedocs/scalar-try-it` (astro.config.mjs)
|
|
2
|
+
// resolves to - ApiReferencePanel.astro imports Scalar's "Try it" through it.
|
|
3
|
+
//
|
|
4
|
+
// Why an alias and not a plain import: Astro bundles the <script> of every
|
|
5
|
+
// component a page imports, rendered or not, and ScalarTryIt.astro's script
|
|
6
|
+
// import()s Scalar - so a plain import would put Scalar's chunks (hundreds of
|
|
7
|
+
// files) in the _astro/ folder of every site with an API page, Scalar or not.
|
|
8
|
+
// Through the alias, a build imports ScalarTryIt.astro only when
|
|
9
|
+
// writedocs.json asks for `api.playground: "scalar"`; otherwise an empty
|
|
10
|
+
// component that renders nothing, and the build is the same as before the
|
|
11
|
+
// option existed.
|
|
12
|
+
//
|
|
13
|
+
// `writedocs dev` always gets the real one: it reads writedocs.json again on
|
|
14
|
+
// every request, so a switch to "scalar" mid-session works without a restart
|
|
15
|
+
// (and dev writes no dist/).
|
|
16
|
+
//
|
|
17
|
+
// Plain JavaScript so the tests can import it.
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
|
|
20
|
+
export const SCALAR_TRY_IT_ALIAS = 'writedocs/scalar-try-it';
|
|
21
|
+
|
|
22
|
+
/** What src/scripts/scalar-try-it.ts imports from Scalar - for Vite's
|
|
23
|
+
* optimizeDeps in `writedocs dev` (astro.config.mjs). Keep in step with its
|
|
24
|
+
* imports (the tests check). */
|
|
25
|
+
export const SCALAR_OPTIMIZED_DEPS = ['@scalar/api-client/modal', '@scalar/workspace-store/client', '@scalar/workspace-store/events'];
|
|
26
|
+
|
|
27
|
+
/** The component file the alias points at. */
|
|
28
|
+
export function scalarTryItComponent({ playground, command, packageRoot }) {
|
|
29
|
+
const file = playground === 'scalar' || command === 'dev' ? 'ScalarTryIt.astro' : 'NoScalarTryIt.astro';
|
|
30
|
+
return path.join(packageRoot, 'src/components', file);
|
|
31
|
+
}
|
|
@@ -74,6 +74,17 @@ import Visibility from "../../packages/components/src/components/Visibility.astr
|
|
|
74
74
|
import { Tree, FileTree, Color, GitHub } from "../../packages/components/src/components/compound";
|
|
75
75
|
import ApiPlayground from "../components/ApiPlayground.astro";
|
|
76
76
|
import ApiReferencePanel from "../components/ApiReferencePanel.astro";
|
|
77
|
+
// Scalar's API reference, the page of an OpenAPI group with `renderer:
|
|
78
|
+
// "scalar"` - through an alias, so a site without one doesn't bundle Scalar
|
|
79
|
+
// (lib/scalar-reference-alias.js).
|
|
80
|
+
import ScalarReference from "writedocs/scalar-reference";
|
|
81
|
+
import {
|
|
82
|
+
readScalarReferences,
|
|
83
|
+
scalarReferenceAt,
|
|
84
|
+
sidebarListsOnly,
|
|
85
|
+
scalarOperationTargets,
|
|
86
|
+
scalarRedirectScript,
|
|
87
|
+
} from "../lib/scalar-reference.js";
|
|
77
88
|
import { searchScope, sectionTrail, ALL_SCOPES } from "../lib/search-scope.js";
|
|
78
89
|
import { selectorPlacements } from "../lib/selector-placement.js";
|
|
79
90
|
import { spaceOf } from "../lib/hidden-sections.js";
|
|
@@ -81,6 +92,7 @@ import { protectedPages, searchSpaceOf } from "../lib/protected-sections.js";
|
|
|
81
92
|
import { isNoindex } from "../lib/seo-tags.js";
|
|
82
93
|
import { navigationPageOrder } from "../lib/pages.js";
|
|
83
94
|
import { folderRedirects } from "../lib/folder-redirects.js";
|
|
95
|
+
import { readMovedApiPages, protectedMovedApiPages } from "../lib/moved-api-pages.js";
|
|
84
96
|
import { DRAFT_BANNER_HTML } from "../lib/drafts.js";
|
|
85
97
|
import { oncePerBuild } from "../lib/build-memo.js";
|
|
86
98
|
// writedocs.json's `basePath` - the addresses below stay site addresses
|
|
@@ -186,17 +198,20 @@ export async function getStaticPaths() {
|
|
|
186
198
|
// a real, directly-linkable page (an in-progress draft, an unlisted
|
|
187
199
|
// landing page, etc.).
|
|
188
200
|
const claimedFileIds = new Set<string>();
|
|
201
|
+
// The operation pages of a Scalar group (lib/scalar-reference.js) aren't in
|
|
202
|
+
// the sidebar either.
|
|
203
|
+
const scalarOperations = scalarOperationTargets(readScalarReferences(contentDir));
|
|
189
204
|
|
|
190
205
|
const pageRoutes = sections.flatMap((section, sectionIndex) => {
|
|
191
206
|
const flatNav = flattenNav(section.pages);
|
|
192
207
|
// Prev/next skip pages that aren't in the sidebar (frontmatter
|
|
193
|
-
// `hidden
|
|
194
|
-
// link) - the reader should land on the
|
|
195
|
-
// have clicked in the sidebar. Inlined for
|
|
196
|
-
// error branch below.
|
|
208
|
+
// `hidden`, a Scalar group's operation pages) or aren't pages at all
|
|
209
|
+
// (frontmatter `url`, an external link) - the reader should land on the
|
|
210
|
+
// next page they could also have clicked in the sidebar. Inlined for
|
|
211
|
+
// the same reason as the error branch below.
|
|
197
212
|
const isPaginationStop = (slug: string) => {
|
|
198
213
|
const data = entryByFileId.get(slug)?.data;
|
|
199
|
-
return Boolean(data) && !data!.hidden && !data!.url;
|
|
214
|
+
return Boolean(data) && !data!.hidden && !data!.url && !scalarOperations.has(slug);
|
|
200
215
|
};
|
|
201
216
|
return flatNav.map((navEntry, i) => {
|
|
202
217
|
const entry = entryByFileId.get(navEntry.slug);
|
|
@@ -265,7 +280,7 @@ export async function getStaticPaths() {
|
|
|
265
280
|
const next =
|
|
266
281
|
flattenNav(landing.pages).find((e) => {
|
|
267
282
|
const data = entryByFileId.get(e.slug)?.data;
|
|
268
|
-
return Boolean(data) && !data!.hidden && !data!.url;
|
|
283
|
+
return Boolean(data) && !data!.hidden && !data!.url && !scalarOperations.has(e.slug);
|
|
269
284
|
}) ?? null;
|
|
270
285
|
const urlSlug = normalizeEntryId(entry.id);
|
|
271
286
|
if (entry.data.url) {
|
|
@@ -364,6 +379,10 @@ export async function getStaticPaths() {
|
|
|
364
379
|
if (rule) protectedRoutes.push({ slug: folder, rule });
|
|
365
380
|
return { params: { slug: folder }, props: { redirectTo: `/${target}/`, temporary: true } };
|
|
366
381
|
});
|
|
382
|
+
// The old addresses of generated API pages (lib/moved-api-pages.js - the
|
|
383
|
+
// redirect pages are astro.config.mjs's): one that leads to a protected
|
|
384
|
+
// page is behind the same rule, and kept out of `_redirects`.
|
|
385
|
+
protectedRoutes.push(...protectedMovedApiPages(readMovedApiPages(contentDir), (slug) => ruleBySlug.get(slug) ?? null));
|
|
367
386
|
// `writedocs build` names a file here; the build makes
|
|
368
387
|
// dist/.writedocs/access.json from it (lib/protected-sections.js,
|
|
369
388
|
// src/cli/build.js) - the og route hands its list over the same way.
|
|
@@ -487,9 +506,17 @@ const { titleByFileId, navTitleByFileId, entryByFileId, methodByFileId } = await
|
|
|
487
506
|
const titleForSlug = (slug: string) => titleByFileId.get(slug) ?? slug;
|
|
488
507
|
const navTitleForSlug = (slug: string) => navTitleByFileId.get(slug) ?? slug;
|
|
489
508
|
const methodForSlug = (slug: string) => methodByFileId.get(slug) ?? null;
|
|
509
|
+
// The operation pages of the site's Scalar groups (lib/scalar-reference.js):
|
|
510
|
+
// left out of the sidebar like a `hidden` page, noindex, and each sends its
|
|
511
|
+
// reader to its operation in the reference.
|
|
512
|
+
const scalarReferences = oncePerBuild("slug:scalar-references", () => readScalarReferences(contentDir));
|
|
513
|
+
const scalarTargets = oncePerBuild("slug:scalar-targets", () => scalarOperationTargets(scalarReferences));
|
|
490
514
|
// Frontmatter that changes how a page appears in navigation (hidden, url,
|
|
491
515
|
// icon, tag, deprecated) - see buildNavTree() in lib/config.ts.
|
|
492
|
-
const navMetaForSlug = (slug: string) =>
|
|
516
|
+
const navMetaForSlug = (slug: string) => {
|
|
517
|
+
const data = entryByFileId.get(slug)?.data;
|
|
518
|
+
return data && scalarTargets.has(slug) ? { ...data, hidden: true } : data;
|
|
519
|
+
};
|
|
493
520
|
const hrefForSlug = (slug: string) => {
|
|
494
521
|
const raw = entryByFileId.get(slug)?.id ?? slug;
|
|
495
522
|
const effective = normalizeEntryId(raw);
|
|
@@ -571,6 +598,14 @@ if (pageRule) {
|
|
|
571
598
|
if (!isNoindex(pageSeo)) pageSeo.robots = undefined;
|
|
572
599
|
pageSeo.noindex = true;
|
|
573
600
|
}
|
|
601
|
+
// An operation page of a Scalar group: the reference is the page to index,
|
|
602
|
+
// and the one the reader is sent to - by the script in <head>, and by search
|
|
603
|
+
// (data-wd-search-url below, cli/run-pagefind.js).
|
|
604
|
+
const scalarTarget = scalarTargets.get(currentFileId) ?? null;
|
|
605
|
+
if (scalarTarget) {
|
|
606
|
+
if (!isNoindex(pageSeo)) pageSeo.robots = undefined;
|
|
607
|
+
pageSeo.noindex = true;
|
|
608
|
+
}
|
|
574
609
|
// A page with no picture of its own for link previews gets the generated
|
|
575
610
|
// card (lib/og-image.js, served by src/pages/og/[...slug].png.ts).
|
|
576
611
|
if (wantsGeneratedCard(pageSeo, entry.data, config.seo)) pageSeo.ogImage = ogImagePath(normalizeEntryId(entry.id));
|
|
@@ -614,7 +649,18 @@ const landingWithoutSidebar =
|
|
|
614
649
|
landing !== undefined &&
|
|
615
650
|
flattenNav(landing.pages).length === 0 &&
|
|
616
651
|
!selectorPlacements(selectors, { sidebar: true }).includes("sidebar");
|
|
617
|
-
|
|
652
|
+
// The page of Scalar's API reference (an OpenAPI group with `renderer:
|
|
653
|
+
// "scalar"` - lib/scalar-reference.js): no table of contents, and the site's
|
|
654
|
+
// sidebar only when it lists more than this page - otherwise Scalar's own
|
|
655
|
+
// sidebar, with the API's operations, stands where it would be.
|
|
656
|
+
const scalarReference = scalarReferenceAt(scalarReferences, currentFileId);
|
|
657
|
+
const pageMode = scalarReference
|
|
658
|
+
? sidebarListsOnly(navTree, currentFileId)
|
|
659
|
+
? "frame"
|
|
660
|
+
: "wide"
|
|
661
|
+
: (isHidden || landingWithoutSidebar) && entry.data.mode === "default"
|
|
662
|
+
? "frame"
|
|
663
|
+
: entry.data.mode;
|
|
618
664
|
const showSidebar = pageMode === "default" || pageMode === "wide";
|
|
619
665
|
// Products inside a tab or dropdown switch at the top of the sidebar, not in
|
|
620
666
|
// the top bar (lib/selector-placement.js - TopBar.astro applies the same rule).
|
|
@@ -702,6 +748,7 @@ const components = {
|
|
|
702
748
|
alternates={alternates}
|
|
703
749
|
breadcrumbs={structuredTrail}
|
|
704
750
|
isApi={Boolean(entry.data.openapi)}
|
|
751
|
+
headScript={scalarTarget ? scalarRedirectScript(siteHref(scalarTarget.href)) : undefined}
|
|
705
752
|
>
|
|
706
753
|
{
|
|
707
754
|
showSidebar && (
|
|
@@ -715,7 +762,29 @@ const components = {
|
|
|
715
762
|
)
|
|
716
763
|
}
|
|
717
764
|
{
|
|
718
|
-
|
|
765
|
+
scalarReference ? (
|
|
766
|
+
// Scalar's reference, the width of the main column. Search finds the
|
|
767
|
+
// page by its title; the operations are found on their own pages
|
|
768
|
+
// (lib/scalar-reference.js). Its list of them is for a reader without
|
|
769
|
+
// JavaScript.
|
|
770
|
+
<div class="wd-canvas" data-pagefind-body>
|
|
771
|
+
<span hidden data-pagefind-meta="title">{entry.data.title}</span>
|
|
772
|
+
<ScalarReference contentDir={contentDir} slug={scalarReference.slug} proxy={config.api.proxy} download={!pageRule}>
|
|
773
|
+
<Content components={components} />
|
|
774
|
+
</ScalarReference>
|
|
775
|
+
<div
|
|
776
|
+
hidden
|
|
777
|
+
data-markdown-ignore
|
|
778
|
+
data-wd-search-scope={pageSearchScope.key}
|
|
779
|
+
data-wd-search-scope-label={pageSearchScope.label}
|
|
780
|
+
data-wd-search-space={pageSearchSpace?.id}
|
|
781
|
+
data-wd-search-public={pageSearchSpace?.searchPublic ? "true" : undefined}
|
|
782
|
+
data-pagefind-filter={`scope:${pageSearchScope.key}`}
|
|
783
|
+
>
|
|
784
|
+
{pageSectionTrail && <span data-pagefind-meta="section">{pageSectionTrail}</span>}
|
|
785
|
+
</div>
|
|
786
|
+
</div>
|
|
787
|
+
) : isCanvasMode ? (
|
|
719
788
|
// 'custom' and 'blank' both get this treatment: no auto <h1>, no
|
|
720
789
|
// prev/next nav, none of .wd-article's own prose max-width/padding -
|
|
721
790
|
// just the page's own MDX/component content, edge to edge. Still gets
|
|
@@ -734,6 +803,7 @@ const components = {
|
|
|
734
803
|
data-wd-search-scope-label={pageSearchScope.label}
|
|
735
804
|
data-wd-search-space={pageSearchSpace?.id}
|
|
736
805
|
data-wd-search-public={pageSearchSpace?.searchPublic ? "true" : undefined}
|
|
806
|
+
data-wd-search-url={scalarTarget?.href}
|
|
737
807
|
data-pagefind-filter={`scope:${pageSearchScope.key}`}
|
|
738
808
|
>
|
|
739
809
|
{pageSectionTrail && <span data-pagefind-meta="section">{pageSectionTrail}</span>}
|
|
@@ -778,6 +848,7 @@ const components = {
|
|
|
778
848
|
data-wd-search-scope-label={pageSearchScope.label}
|
|
779
849
|
data-wd-search-space={pageSearchSpace?.id}
|
|
780
850
|
data-wd-search-public={pageSearchSpace?.searchPublic ? "true" : undefined}
|
|
851
|
+
data-wd-search-url={scalarTarget?.href}
|
|
781
852
|
data-pagefind-filter={`scope:${pageSearchScope.key}`}
|
|
782
853
|
>
|
|
783
854
|
{pageSectionTrail && <span data-pagefind-meta="section">{pageSectionTrail}</span>}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Scalar's API reference, the page of an OpenAPI group with
|
|
2
|
+
// `openapi.renderer: "scalar"`. ScalarReference.astro's page script
|
|
3
|
+
// import()s this module when the page loads; Vite puts it - Scalar, Vue and
|
|
4
|
+
// Scalar's CSS with it - in chunks of their own under _astro/, from the
|
|
5
|
+
// generator's own node_modules (never a CDN), loaded by this page only.
|
|
6
|
+
//
|
|
7
|
+
// - The document: the group's spec, bundled, from the JSON the page carries
|
|
8
|
+
// (ScalarReference.astro) - nothing is fetched.
|
|
9
|
+
// - Requests from its Try it: through tryItFetch() (lib/scalar-fetch.js), the
|
|
10
|
+
// rules of writedocs' playground and of the API pages' Scalar Try it -
|
|
11
|
+
// straight to the API, then writedocs' proxy when `api.proxy` allows;
|
|
12
|
+
// never Scalar's proxy (no proxyUrl; its client opens as a modal, which
|
|
13
|
+
// then uses none).
|
|
14
|
+
// - Off: Scalar's AI agent ("Ask AI" - on by default on localhost), its
|
|
15
|
+
// developer tools (and their "Deploy API"/"Generate SDK"), telemetry, its
|
|
16
|
+
// fonts from fonts.scalar.com, its own search (the site's is the search),
|
|
17
|
+
// its dark-mode toggle (the site's theme decides), and - in a protected
|
|
18
|
+
// section - the spec download.
|
|
19
|
+
// - Anchors: Scalar's tag and operation slugs come from
|
|
20
|
+
// lib/scalar-anchors.js, the functions the build used to send each
|
|
21
|
+
// operation's page and search result to its section.
|
|
22
|
+
// - The look: Scalar's CSS plus the site's tokens on the reference's element
|
|
23
|
+
// (lib/scalar-theme.js). Scalar puts .light-mode/.dark-mode on <body>; it
|
|
24
|
+
// follows <html data-theme> here.
|
|
25
|
+
// - Credentials: Scalar keeps what a reader types in memory (`persistAuth`
|
|
26
|
+
// would write it to localStorage - off). Like writedocs' playground they're
|
|
27
|
+
// kept for the visit and forgotten with the tab: copied to sessionStorage
|
|
28
|
+
// when the reader leaves the page and given back as Scalar's
|
|
29
|
+
// `authentication` when it opens again - under the key the API pages'
|
|
30
|
+
// Scalar Try it uses, by scheme name.
|
|
31
|
+
// - One reference per page: Astro's client-side navigation replaces the
|
|
32
|
+
// body, so it's destroyed before each swap.
|
|
33
|
+
import scalarCss from '@scalar/api-reference/style.css?inline';
|
|
34
|
+
import { createApiReference } from '@scalar/api-reference';
|
|
35
|
+
import { tryItFetch } from '../lib/scalar-fetch.js';
|
|
36
|
+
import { scalarReferenceThemeCss } from '../lib/scalar-theme.js';
|
|
37
|
+
import { scalarTagSlug, scalarOperationSlug } from '../lib/scalar-anchors.js';
|
|
38
|
+
|
|
39
|
+
export interface ReferenceRequest {
|
|
40
|
+
/** The element to mount in. */
|
|
41
|
+
el: HTMLElement;
|
|
42
|
+
/** The group's spec. */
|
|
43
|
+
document: Record<string, unknown>;
|
|
44
|
+
/** writedocs.json's `api.proxy`. */
|
|
45
|
+
proxy: boolean;
|
|
46
|
+
/** Whether the reader may download the spec - not in a protected section. */
|
|
47
|
+
download: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const STYLE_ID = 'wd-scalar-reference-style';
|
|
51
|
+
const AUTH_STORAGE_KEY = 'wd-scalar-auth';
|
|
52
|
+
|
|
53
|
+
type Instance = ReturnType<typeof createApiReference>;
|
|
54
|
+
type AuthAccess = { export: () => Record<string, { secrets?: Record<string, Record<string, unknown>> }> };
|
|
55
|
+
let current: { instance: Instance; observer: MutationObserver; saveAuth: () => void } | null = null;
|
|
56
|
+
|
|
57
|
+
function ensureStyles() {
|
|
58
|
+
if (document.getElementById(STYLE_ID)) return;
|
|
59
|
+
const style = document.createElement('style');
|
|
60
|
+
style.id = STYLE_ID;
|
|
61
|
+
style.textContent = `${scalarCss}\n${scalarReferenceThemeCss()}`;
|
|
62
|
+
document.head.appendChild(style);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const isDark = () => document.documentElement.dataset.theme === 'dark';
|
|
66
|
+
function applyTheme() {
|
|
67
|
+
document.body.classList.toggle('dark-mode', isDark());
|
|
68
|
+
document.body.classList.toggle('light-mode', !isDark());
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
type SavedAuth = Record<string, Record<string, unknown>>;
|
|
72
|
+
function readAuth(): SavedAuth {
|
|
73
|
+
try {
|
|
74
|
+
const parsed = JSON.parse(sessionStorage.getItem(AUTH_STORAGE_KEY) ?? '{}');
|
|
75
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
76
|
+
} catch {
|
|
77
|
+
return {};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function writeAuth(all: SavedAuth) {
|
|
81
|
+
try {
|
|
82
|
+
sessionStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify(all));
|
|
83
|
+
} catch {
|
|
84
|
+
// storage unavailable - credentials just won't carry to the next visit of the page
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function unmount() {
|
|
89
|
+
if (!current) return;
|
|
90
|
+
current.saveAuth();
|
|
91
|
+
current.observer.disconnect();
|
|
92
|
+
current.instance.destroy();
|
|
93
|
+
current = null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Mounts Scalar's reference in `request.el`. */
|
|
97
|
+
export function mountReference(request: ReferenceRequest) {
|
|
98
|
+
unmount();
|
|
99
|
+
ensureStyles();
|
|
100
|
+
applyTheme();
|
|
101
|
+
const observer = new MutationObserver(applyTheme);
|
|
102
|
+
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
|
|
103
|
+
|
|
104
|
+
// Scalar's read-only view of what the reader typed, from a plugin's onInit.
|
|
105
|
+
let auth: AuthAccess | null = null;
|
|
106
|
+
const saveAuth = () => {
|
|
107
|
+
if (!auth) return;
|
|
108
|
+
const all = readAuth();
|
|
109
|
+
for (const documentAuth of Object.values(auth.export() ?? {})) {
|
|
110
|
+
for (const [name, secrets] of Object.entries(documentAuth?.secrets ?? {})) all[name] = secrets;
|
|
111
|
+
}
|
|
112
|
+
writeAuth(all);
|
|
113
|
+
};
|
|
114
|
+
const authPlugin = () => ({
|
|
115
|
+
name: 'writedocs-session-auth',
|
|
116
|
+
extensions: [],
|
|
117
|
+
hooks: {
|
|
118
|
+
onInit: ({ auth: access }: { auth: AuthAccess }) => {
|
|
119
|
+
auth = access;
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const instance = createApiReference(request.el, {
|
|
125
|
+
content: request.document,
|
|
126
|
+
customFetch: tryItFetch({ proxy: request.proxy }),
|
|
127
|
+
persistAuth: false,
|
|
128
|
+
authentication: { securitySchemes: readAuth() },
|
|
129
|
+
agent: { disabled: true },
|
|
130
|
+
mcp: { disabled: true },
|
|
131
|
+
showDeveloperTools: 'never',
|
|
132
|
+
telemetry: false,
|
|
133
|
+
withDefaultFonts: false,
|
|
134
|
+
hideSearch: true,
|
|
135
|
+
hideDarkModeToggle: true,
|
|
136
|
+
forceDarkModeState: isDark() ? 'dark' : 'light',
|
|
137
|
+
documentDownloadType: request.download ? 'both' : 'none',
|
|
138
|
+
generateTagSlug: scalarTagSlug,
|
|
139
|
+
generateOperationSlug: scalarOperationSlug,
|
|
140
|
+
plugins: [authPlugin],
|
|
141
|
+
} as never);
|
|
142
|
+
current = { instance, observer, saveAuth };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Leaving the page (a reload, another site, the tab closing - then
|
|
146
|
+
// sessionStorage goes with it) or hiding it: what the reader typed is kept.
|
|
147
|
+
addEventListener('pagehide', () => current?.saveAuth());
|
|
148
|
+
document.addEventListener('visibilitychange', () => {
|
|
149
|
+
if (document.visibilityState === 'hidden') current?.saveAuth();
|
|
150
|
+
});
|
|
151
|
+
document.addEventListener('astro:before-swap', unmount);
|