@writedocs/generator 0.17.0 → 0.18.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,145 @@
|
|
|
1
|
+
// Scalar's API client as the "Try it" of an API page (writedocs.json
|
|
2
|
+
// `api.playground: "scalar"`). Nothing here is on a page until a reader
|
|
3
|
+
// clicks Try it: ScalarTryIt.astro's page script import()s this module on
|
|
4
|
+
// that click, and Vite puts it - Scalar, Vue and Scalar's CSS with it - in
|
|
5
|
+
// chunks of their own under _astro/, from the generator's own node_modules
|
|
6
|
+
// (never a CDN).
|
|
7
|
+
//
|
|
8
|
+
// - The OpenAPI document is the page's own operation, from the JSON
|
|
9
|
+
// ScalarTryIt.astro wrote into the page (lib/scalar-document.js).
|
|
10
|
+
// - Requests: through tryItFetch() (lib/scalar-fetch.js) - straight to the
|
|
11
|
+
// API, then writedocs' proxy when `api.proxy` allows; never Scalar's proxy
|
|
12
|
+
// (no proxyUrl is given, and in its modal Scalar then uses none).
|
|
13
|
+
// - The look: Scalar's CSS plus the site's tokens mapped onto Scalar's
|
|
14
|
+
// variables (lib/scalar-theme.js), in a <style> added on the first open
|
|
15
|
+
// (and again after a page swap drops it); the host's .light-mode /
|
|
16
|
+
// .dark-mode class follows <html data-theme> while the modal is open.
|
|
17
|
+
// - Credentials: Scalar keeps what a reader types in memory. To carry it
|
|
18
|
+
// across the pages of one visit like writedocs' own playground does
|
|
19
|
+
// (sessionStorage - forgotten when the tab closes), each change is copied
|
|
20
|
+
// to sessionStorage and replayed into the next page's client.
|
|
21
|
+
// - One client per page: Astro's client-side navigation replaces the body,
|
|
22
|
+
// so the client is unmounted before each swap and made again on the next
|
|
23
|
+
// page's first click.
|
|
24
|
+
import scalarCss from '@scalar/api-client/style.css?inline';
|
|
25
|
+
import { createApiClientModal } from '@scalar/api-client/modal';
|
|
26
|
+
import { createWorkspaceStore } from '@scalar/workspace-store/client';
|
|
27
|
+
import { createWorkspaceEventBus } from '@scalar/workspace-store/events';
|
|
28
|
+
import { tryItFetch } from '../lib/scalar-fetch.js';
|
|
29
|
+
import { scalarThemeCss, SCALAR_HOST_CLASS } from '../lib/scalar-theme.js';
|
|
30
|
+
|
|
31
|
+
export interface TryItRequest {
|
|
32
|
+
/** The page's operation as an OpenAPI document (lib/scalar-document.js). */
|
|
33
|
+
document: Record<string, unknown>;
|
|
34
|
+
method: string;
|
|
35
|
+
path: string;
|
|
36
|
+
/** writedocs.json's `api.proxy`. */
|
|
37
|
+
proxy: boolean;
|
|
38
|
+
/** Identifies the page's client: a new page, or another operation, gets a new one. */
|
|
39
|
+
key: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const STYLE_ID = 'wd-scalar-style';
|
|
43
|
+
const AUTH_STORAGE_KEY = 'wd-scalar-auth';
|
|
44
|
+
|
|
45
|
+
type Modal = ReturnType<typeof createApiClientModal>;
|
|
46
|
+
let current: { key: string; host: HTMLElement; modal: Modal; observer: MutationObserver } | null = null;
|
|
47
|
+
|
|
48
|
+
function ensureStyles() {
|
|
49
|
+
if (document.getElementById(STYLE_ID)) return;
|
|
50
|
+
const style = document.createElement('style');
|
|
51
|
+
style.id = STYLE_ID;
|
|
52
|
+
style.textContent = `${scalarCss}\n${scalarThemeCss()}`;
|
|
53
|
+
document.head.appendChild(style);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function applyTheme(host: HTMLElement) {
|
|
57
|
+
const dark = document.documentElement.dataset.theme === 'dark';
|
|
58
|
+
host.classList.toggle('dark-mode', dark);
|
|
59
|
+
host.classList.toggle('light-mode', !dark);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type SavedAuth = Record<string, Record<string, unknown>>;
|
|
63
|
+
function readAuth(): SavedAuth {
|
|
64
|
+
try {
|
|
65
|
+
const parsed = JSON.parse(sessionStorage.getItem(AUTH_STORAGE_KEY) ?? '{}');
|
|
66
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
67
|
+
} catch {
|
|
68
|
+
return {};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function writeAuth(all: SavedAuth) {
|
|
72
|
+
try {
|
|
73
|
+
sessionStorage.setItem(AUTH_STORAGE_KEY, JSON.stringify(all));
|
|
74
|
+
} catch {
|
|
75
|
+
// storage unavailable - credentials just won't carry across pages
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function merged(base: unknown, patch: unknown): unknown {
|
|
79
|
+
if (!base || typeof base !== 'object' || !patch || typeof patch !== 'object' || Array.isArray(patch)) return patch;
|
|
80
|
+
const out: Record<string, unknown> = { ...(base as Record<string, unknown>) };
|
|
81
|
+
for (const [k, v] of Object.entries(patch as Record<string, unknown>)) out[k] = merged(out[k], v);
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function unmount() {
|
|
86
|
+
if (!current) return;
|
|
87
|
+
current.observer.disconnect();
|
|
88
|
+
current.modal.app.unmount();
|
|
89
|
+
current.host.remove();
|
|
90
|
+
current = null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function create(request: TryItRequest): Promise<Modal> {
|
|
94
|
+
const host = document.createElement('div');
|
|
95
|
+
host.className = SCALAR_HOST_CLASS;
|
|
96
|
+
host.dataset.wdScalar = request.key;
|
|
97
|
+
applyTheme(host);
|
|
98
|
+
document.body.appendChild(host);
|
|
99
|
+
const observer = new MutationObserver(() => applyTheme(host));
|
|
100
|
+
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
|
|
101
|
+
|
|
102
|
+
const workspaceStore = createWorkspaceStore();
|
|
103
|
+
await workspaceStore.addDocument({ name: 'default', document: request.document });
|
|
104
|
+
const eventBus = createWorkspaceEventBus({ debug: false });
|
|
105
|
+
const modal = createApiClientModal({
|
|
106
|
+
el: host,
|
|
107
|
+
eventBus,
|
|
108
|
+
workspaceStore,
|
|
109
|
+
options: {
|
|
110
|
+
// No proxyUrl: Scalar's modal then sends straight to the API, and
|
|
111
|
+
// customFetch decides the rest.
|
|
112
|
+
customFetch: tryItFetch({ proxy: request.proxy }),
|
|
113
|
+
hideClientButton: true,
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Credentials typed on an earlier page of this visit, then every change.
|
|
118
|
+
const schemes = (request.document.components as { securitySchemes?: Record<string, unknown> } | undefined)?.securitySchemes ?? {};
|
|
119
|
+
for (const [name, payload] of Object.entries(readAuth())) {
|
|
120
|
+
if (name in schemes) eventBus.emit('auth:update:security-scheme-secrets', { name, payload, overwrite: true } as never);
|
|
121
|
+
}
|
|
122
|
+
eventBus.on('auth:update:security-scheme-secrets', (({ name, payload, overwrite }: { name: string; payload: Record<string, unknown>; overwrite?: boolean }) => {
|
|
123
|
+
const all = readAuth();
|
|
124
|
+
all[name] = (overwrite ? payload : merged(all[name], payload)) as Record<string, unknown>;
|
|
125
|
+
writeAuth(all);
|
|
126
|
+
}) as never);
|
|
127
|
+
eventBus.on('auth:clear:security-scheme-secrets', (({ name }: { name: string }) => {
|
|
128
|
+
const all = readAuth();
|
|
129
|
+
delete all[name];
|
|
130
|
+
writeAuth(all);
|
|
131
|
+
}) as never);
|
|
132
|
+
|
|
133
|
+
current = { key: request.key, host, modal, observer };
|
|
134
|
+
return modal;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Opens Scalar's client on the page's operation. */
|
|
138
|
+
export async function openTryIt(request: TryItRequest) {
|
|
139
|
+
ensureStyles();
|
|
140
|
+
if (current && (current.key !== request.key || !current.host.isConnected)) unmount();
|
|
141
|
+
const modal = current?.modal ?? (await create(request));
|
|
142
|
+
modal.open({ path: request.path, method: request.method.toLowerCase() as never });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
document.addEventListener('astro:before-swap', unmount);
|
package/writedocs.schema.json
CHANGED
|
@@ -708,7 +708,8 @@
|
|
|
708
708
|
},
|
|
709
709
|
"api": {
|
|
710
710
|
"default": {
|
|
711
|
-
"proxy": true
|
|
711
|
+
"proxy": true,
|
|
712
|
+
"playground": "writedocs"
|
|
712
713
|
},
|
|
713
714
|
"type": "object",
|
|
714
715
|
"properties": {
|
|
@@ -717,6 +718,16 @@
|
|
|
717
718
|
"type": "boolean",
|
|
718
719
|
"description": "When the browser blocks a \"Try it\" request (the API doesn't allow calls from other sites - CORS), retry it through writedocs' proxy. Requests always go to the API directly first. Default true.",
|
|
719
720
|
"markdownDescription": "When the browser blocks a \"Try it\" request (the API doesn't allow calls from other sites - CORS), retry it through writedocs' proxy. Requests always go to the API directly first. Default true."
|
|
721
|
+
},
|
|
722
|
+
"playground": {
|
|
723
|
+
"default": "writedocs",
|
|
724
|
+
"type": "string",
|
|
725
|
+
"enum": [
|
|
726
|
+
"writedocs",
|
|
727
|
+
"scalar"
|
|
728
|
+
],
|
|
729
|
+
"description": "Which client the \"Try it\" button of the API pages opens: \"writedocs\" (the default) or \"scalar\" (Scalar's API client, loaded only when a reader clicks Try it). The API pages are the same either way; api.proxy applies to both.",
|
|
730
|
+
"markdownDescription": "Which client the \"Try it\" button of the API pages opens: \"writedocs\" (the default) or \"scalar\" (Scalar's API client, loaded only when a reader clicks Try it). The API pages are the same either way; api.proxy applies to both."
|
|
720
731
|
}
|
|
721
732
|
},
|
|
722
733
|
"additionalProperties": false,
|
|
@@ -1276,6 +1287,15 @@
|
|
|
1276
1287
|
"type": "string",
|
|
1277
1288
|
"description": "URL path the generated pages go under, like \"/api\". Each OpenAPI group needs its own.",
|
|
1278
1289
|
"markdownDescription": "URL path the generated pages go under, like \"/api\". Each OpenAPI group needs its own."
|
|
1290
|
+
},
|
|
1291
|
+
"renderer": {
|
|
1292
|
+
"type": "string",
|
|
1293
|
+
"enum": [
|
|
1294
|
+
"writedocs",
|
|
1295
|
+
"scalar"
|
|
1296
|
+
],
|
|
1297
|
+
"description": "How readers see the group: \"writedocs\" (the default), a page per operation grouped by tag; or \"scalar\", one page at `path` with Scalar's API reference and its own Try it. Search, llms.txt, MCP and the .md copies still cover every operation.",
|
|
1298
|
+
"markdownDescription": "How readers see the group: \"writedocs\" (the default), a page per operation grouped by tag; or \"scalar\", one page at `path` with Scalar's API reference and its own Try it. Search, llms.txt, MCP and the .md copies still cover every operation."
|
|
1279
1299
|
}
|
|
1280
1300
|
},
|
|
1281
1301
|
"required": [
|