@scalar/asyncapi-to-markdown 0.0.0-stage → 0.2.1
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/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +145 -2
- package/dist/binding-schemas.d.ts +3 -0
- package/dist/binding-schemas.d.ts.map +1 -0
- package/dist/binding-schemas.js +33 -0
- package/dist/browser.d.ts +5 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +3 -0
- package/dist/document.d.ts +22 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +85 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/load-document.d.ts +4 -0
- package/dist/load-document.d.ts.map +1 -0
- package/dist/load-document.js +66 -0
- package/dist/preserve-data.d.ts +4 -0
- package/dist/preserve-data.d.ts.map +1 -0
- package/dist/preserve-data.js +66 -0
- package/dist/render-content.d.ts +16 -0
- package/dist/render-content.d.ts.map +1 -0
- package/dist/render-content.js +306 -0
- package/dist/render-document.d.ts +4 -0
- package/dist/render-document.d.ts.map +1 -0
- package/dist/render-document.js +140 -0
- package/dist/render-examples.d.ts +4 -0
- package/dist/render-examples.d.ts.map +1 -0
- package/dist/render-examples.js +153 -0
- package/dist/render-schema.d.ts +10 -0
- package/dist/render-schema.d.ts.map +1 -0
- package/dist/render-schema.js +205 -0
- package/dist/schema-format.d.ts +3 -0
- package/dist/schema-format.d.ts.map +1 -0
- package/dist/schema-format.js +3 -0
- package/dist/schema-references.d.ts +12 -0
- package/dist/schema-references.d.ts.map +1 -0
- package/dist/schema-references.js +121 -0
- package/dist/schema-view.d.ts +5 -0
- package/dist/schema-view.d.ts.map +1 -0
- package/dist/schema-view.js +97 -0
- package/dist/types.d.ts +38 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +73 -3
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { isXmlMediaType } from '@scalar/helpers/http/is-xml-media-type';
|
|
2
|
+
import { getRaw } from '@scalar/json-magic/magic-proxy';
|
|
3
|
+
import { anchor, createDocumentAnchors, describe, heading, inlineCode, item, link, list, paragraph, strong, text, } from '@scalar/openapi-to-markdown/markdown';
|
|
4
|
+
import { resolveTraits } from '@scalar/workspace-store/helpers/resolve-traits';
|
|
5
|
+
import { getXmlBodyExample } from '@scalar/workspace-store/request-example';
|
|
6
|
+
import { getBindingSchemas } from './binding-schemas.js';
|
|
7
|
+
import { object, string } from './document.js';
|
|
8
|
+
import { createExampleRenderer } from './render-examples.js';
|
|
9
|
+
import { code, createSchemaRenderer } from './render-schema.js';
|
|
10
|
+
const deeper = (depth) => Math.min(depth + 1, 6);
|
|
11
|
+
const title = (value, fallback) => string(value.title) || string(value.summary) || string(value.name) || fallback;
|
|
12
|
+
const entries = (value) => Object.entries(object(value));
|
|
13
|
+
const withTraits = (value) => {
|
|
14
|
+
const target = object(value);
|
|
15
|
+
return resolveTraits(target, Array.isArray(target.traits) ? target.traits.map(object) : []);
|
|
16
|
+
};
|
|
17
|
+
/** Build the sections of one AsyncAPI page without retaining mutable state between calls. */
|
|
18
|
+
export const createContentRenderer = (document, references, view, linked, selectedModel, selectedTag, inlinePrimitives = true) => {
|
|
19
|
+
const examples = createExampleRenderer(inlinePrimitives);
|
|
20
|
+
const renderSchema = createSchemaRenderer(references.reference, view, linked);
|
|
21
|
+
const schema = (input, expand = false, mediaType = 'application/json') => expand
|
|
22
|
+
? renderSchema(input, true, mediaType)
|
|
23
|
+
: references.root(input, () => renderSchema(input, false, mediaType), mediaType);
|
|
24
|
+
const anchors = createDocumentAnchors();
|
|
25
|
+
const shownMessages = new Map();
|
|
26
|
+
const shownServers = new Map();
|
|
27
|
+
const shownSecurity = new Map();
|
|
28
|
+
const blocks = [];
|
|
29
|
+
const metadata = (value, includeTagDocumentation = true) => {
|
|
30
|
+
if (value.summary && value.summary !== value.title) {
|
|
31
|
+
blocks.push(...describe(value.summary));
|
|
32
|
+
}
|
|
33
|
+
blocks.push(...describe(value.description));
|
|
34
|
+
const tags = Array.isArray(value.tags) ? value.tags.map((tag) => string(object(tag).name)).filter(Boolean) : [];
|
|
35
|
+
const visibleTags = selectedTag ? tags.filter((tag) => tag === selectedTag) : tags;
|
|
36
|
+
if (visibleTags.length) {
|
|
37
|
+
blocks.push(paragraph(strong(text('Tags:')), text(' '), text(visibleTags.join(', '))));
|
|
38
|
+
}
|
|
39
|
+
if (includeTagDocumentation && Array.isArray(value.tags)) {
|
|
40
|
+
for (const entry of value.tags) {
|
|
41
|
+
const tag = object(entry);
|
|
42
|
+
if (selectedTag && tag.name !== selectedTag) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const external = object(tag.externalDocs);
|
|
46
|
+
if (tag.description || typeof external.url === 'string') {
|
|
47
|
+
blocks.push(paragraph(strong(text(`Tag: ${string(tag.name) ?? ''}`))), ...describe(tag.description));
|
|
48
|
+
if (typeof external.url === 'string') {
|
|
49
|
+
blocks.push(paragraph(link(external.url, string(external.description) || 'External documentation')));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const external = object(value.externalDocs);
|
|
55
|
+
if (typeof external.url === 'string') {
|
|
56
|
+
blocks.push(paragraph(link(external.url, string(external.description) || 'External documentation')));
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const fields = (value, names) => {
|
|
60
|
+
const items = names.flatMap((name) => value[name] === undefined
|
|
61
|
+
? []
|
|
62
|
+
: [
|
|
63
|
+
item(paragraph(strong(text(`${name}:`)), text(' '), inlineCode(typeof value[name] === 'string' ? value[name] : JSON.stringify(getRaw(value[name]))))),
|
|
64
|
+
]);
|
|
65
|
+
if (items.length) {
|
|
66
|
+
blocks.push(list(items));
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const bindings = (value, depth) => {
|
|
70
|
+
const protocols = entries(value);
|
|
71
|
+
if (!protocols.length) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
blocks.push(heading(depth, text('Bindings')));
|
|
75
|
+
for (const [protocol, binding] of protocols) {
|
|
76
|
+
const resolved = object(binding);
|
|
77
|
+
blocks.push(paragraph(strong(text(protocol))), code(resolved));
|
|
78
|
+
for (const reference of getBindingSchemas(resolved)) {
|
|
79
|
+
blocks.push(paragraph(strong(text('Referenced schema:')), text(' '), inlineCode(reference.$ref)), ...schema(reference));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const security = (value, depth, operationSecurity = false) => {
|
|
84
|
+
if (!Array.isArray(value) || !value.length) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
blocks.push(heading(depth, text('Authentication')), paragraph(text('Satisfy one of the following security schemes.')));
|
|
88
|
+
if (operationSecurity) {
|
|
89
|
+
blocks.push(paragraph(text('Applicable server security must also be satisfied.')));
|
|
90
|
+
}
|
|
91
|
+
for (const entry of value) {
|
|
92
|
+
const scheme = object(entry);
|
|
93
|
+
const identity = getRaw(scheme);
|
|
94
|
+
const previous = shownSecurity.get(identity);
|
|
95
|
+
const label = string(scheme.name) || string(scheme.type) || 'Security scheme';
|
|
96
|
+
if (previous) {
|
|
97
|
+
blocks.push(paragraph(link(`#${previous}`, label)));
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const id = anchors.get('security', String(shownSecurity.size), label);
|
|
101
|
+
shownSecurity.set(identity, id);
|
|
102
|
+
blocks.push(anchor(id));
|
|
103
|
+
fields(scheme, ['type', 'name', 'in', 'scheme', 'bearerFormat', 'openIdConnectUrl', 'scopes']);
|
|
104
|
+
blocks.push(...describe(scheme.description));
|
|
105
|
+
if (scheme.flows) {
|
|
106
|
+
blocks.push(code(scheme.flows));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const servers = (value, depth) => {
|
|
111
|
+
if (!value.length) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
blocks.push(heading(depth, text('Servers')));
|
|
115
|
+
for (const [name, entry] of value) {
|
|
116
|
+
const server = object(entry);
|
|
117
|
+
const identity = getRaw(server);
|
|
118
|
+
const previous = shownServers.get(identity);
|
|
119
|
+
if (previous) {
|
|
120
|
+
blocks.push(paragraph(link(`#${previous}`, title(server, name))));
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const serverId = anchors.get('server', String(shownServers.size), name);
|
|
124
|
+
shownServers.set(identity, serverId);
|
|
125
|
+
blocks.push(anchor(serverId));
|
|
126
|
+
blocks.push(heading(deeper(depth), text(title(server, name))));
|
|
127
|
+
fields(server, ['host', 'pathname', 'protocol', 'protocolVersion']);
|
|
128
|
+
metadata(server);
|
|
129
|
+
for (const [variableName, variable] of entries(server.variables)) {
|
|
130
|
+
blocks.push(paragraph(strong(text('Server variable:')), text(' '), inlineCode(variableName)));
|
|
131
|
+
fields(object(variable), ['default', 'enum', 'examples']);
|
|
132
|
+
blocks.push(...describe(object(variable).description));
|
|
133
|
+
}
|
|
134
|
+
security(server.security, deeper(deeper(depth)));
|
|
135
|
+
bindings(server.bindings, deeper(deeper(depth)));
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const channelServers = (channel) => Array.isArray(channel.servers) && channel.servers.length > 0
|
|
139
|
+
? channel.servers.map((server, index) => [string(object(server).title) || `Server ${index + 1}`, server])
|
|
140
|
+
: entries(document.servers);
|
|
141
|
+
const message = (name, value, depth) => {
|
|
142
|
+
const identity = getRaw(object(value));
|
|
143
|
+
if (selectedTag) {
|
|
144
|
+
const previous = shownMessages.get(identity);
|
|
145
|
+
if (previous) {
|
|
146
|
+
blocks.push(paragraph(link(`#${previous}`, title(withTraits(value), name))));
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const id = anchors.get('message', String(shownMessages.size + 1), name);
|
|
150
|
+
shownMessages.set(identity, id);
|
|
151
|
+
blocks.push(anchor(id));
|
|
152
|
+
}
|
|
153
|
+
const msg = withTraits(value);
|
|
154
|
+
const contentType = string(msg.contentType ?? document.defaultContentType) ?? 'application/json';
|
|
155
|
+
blocks.push(heading(depth, text(title(msg, name))));
|
|
156
|
+
metadata(msg);
|
|
157
|
+
fields({ ...msg, contentType: msg.contentType ?? document.defaultContentType }, ['name', 'contentType']);
|
|
158
|
+
if (msg.correlationId) {
|
|
159
|
+
blocks.push(heading(deeper(depth), text('Correlation ID')));
|
|
160
|
+
fields(object(msg.correlationId), ['location']);
|
|
161
|
+
metadata(object(msg.correlationId));
|
|
162
|
+
}
|
|
163
|
+
for (const key of ['headers', 'payload']) {
|
|
164
|
+
if (msg[key] !== undefined) {
|
|
165
|
+
blocks.push(heading(deeper(depth), text(key === 'headers' ? 'Headers' : 'Payload')), ...schema(key === 'payload' && typeof msg.schemaFormat === 'string'
|
|
166
|
+
? { schemaFormat: msg.schemaFormat, schema: msg[key] }
|
|
167
|
+
: msg[key], false, key === 'payload' ? contentType : 'application/json'));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (Array.isArray(msg.examples)) {
|
|
171
|
+
for (const example of msg.examples) {
|
|
172
|
+
const authored = object(example);
|
|
173
|
+
blocks.push(heading(deeper(depth), text(string(authored.name) || 'Example')));
|
|
174
|
+
blocks.push(...describe(authored.summary));
|
|
175
|
+
// Message examples contain authored headers and payload, not generated values.
|
|
176
|
+
for (const key of ['headers', 'payload']) {
|
|
177
|
+
if (authored[key] !== undefined) {
|
|
178
|
+
const xml = key === 'payload' && isXmlMediaType(contentType);
|
|
179
|
+
const result = xml && typeof authored[key] !== 'string'
|
|
180
|
+
? getXmlBodyExample(object(msg.payload), { value: authored[key] })
|
|
181
|
+
: undefined;
|
|
182
|
+
blocks.push(paragraph(strong(text(`${key}:`))));
|
|
183
|
+
blocks.push(xml
|
|
184
|
+
? typeof authored[key] === 'string' || result?.xml !== undefined
|
|
185
|
+
? code(result?.xml ?? authored[key], 'xml')
|
|
186
|
+
: paragraph(text('Unable to generate an XML example.'))
|
|
187
|
+
: code(authored[key]));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (!Array.isArray(msg.examples) || !msg.examples.length) {
|
|
193
|
+
for (const key of ['headers', 'payload']) {
|
|
194
|
+
if (msg[key] !== undefined) {
|
|
195
|
+
blocks.push(...examples(key === 'payload' && typeof msg.schemaFormat === 'string'
|
|
196
|
+
? { schemaFormat: msg.schemaFormat, schema: msg[key] }
|
|
197
|
+
: msg[key], linked, false, key === 'payload' ? contentType : 'application/json', key));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
bindings(msg.bindings, deeper(depth));
|
|
202
|
+
};
|
|
203
|
+
const channelContext = (channel, depth) => {
|
|
204
|
+
if (channel.address === undefined || channel.address === null) {
|
|
205
|
+
blocks.push(paragraph(strong(text('Address:')), text(' Unknown.')));
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
fields(channel, ['address']);
|
|
209
|
+
}
|
|
210
|
+
metadata(channel);
|
|
211
|
+
for (const [name, parameter] of entries(channel.parameters)) {
|
|
212
|
+
blocks.push(heading(depth, text(`Parameter: ${name}`)));
|
|
213
|
+
fields(object(parameter), ['enum', 'default', 'examples', 'location']);
|
|
214
|
+
metadata(object(parameter));
|
|
215
|
+
}
|
|
216
|
+
servers(channelServers(channel), depth);
|
|
217
|
+
bindings(channel.bindings, depth);
|
|
218
|
+
};
|
|
219
|
+
const messages = (owner, channel) => Array.isArray(owner.messages)
|
|
220
|
+
? owner.messages.map((msg, index) => [title(object(msg), `Message ${index + 1}`), msg])
|
|
221
|
+
: entries(channel.messages);
|
|
222
|
+
const operation = (name, value, depth, includeChannel) => {
|
|
223
|
+
const op = withTraits(value);
|
|
224
|
+
const channel = object(op.channel);
|
|
225
|
+
blocks.push(heading(depth, text(title(op, name))));
|
|
226
|
+
fields(op, ['action']);
|
|
227
|
+
metadata(op);
|
|
228
|
+
if (includeChannel) {
|
|
229
|
+
blocks.push(heading(deeper(depth), text('Channel')));
|
|
230
|
+
channelContext(channel, deeper(deeper(depth)));
|
|
231
|
+
}
|
|
232
|
+
security(op.security, deeper(depth), true);
|
|
233
|
+
bindings(op.bindings, deeper(depth));
|
|
234
|
+
for (const [messageName, msg] of messages(op, channel)) {
|
|
235
|
+
message(messageName, msg, deeper(depth));
|
|
236
|
+
}
|
|
237
|
+
if (op.reply) {
|
|
238
|
+
const reply = object(op.reply);
|
|
239
|
+
const replyChannel = object(reply.channel);
|
|
240
|
+
blocks.push(heading(deeper(depth), text('Reply')));
|
|
241
|
+
if (reply.channel !== undefined) {
|
|
242
|
+
channelContext(replyChannel, deeper(deeper(depth)));
|
|
243
|
+
}
|
|
244
|
+
const address = object(reply.address);
|
|
245
|
+
fields(address, ['location']);
|
|
246
|
+
metadata(address);
|
|
247
|
+
for (const [messageName, msg] of messages(reply, {})) {
|
|
248
|
+
message(messageName, msg, deeper(deeper(depth)));
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
const channel = (name, value, depth) => {
|
|
253
|
+
const current = object(value);
|
|
254
|
+
blocks.push(heading(depth, text(title(current, name))));
|
|
255
|
+
channelContext(current, deeper(depth));
|
|
256
|
+
const operations = entries(document.operations).filter(([, op]) => getRaw(object(withTraits(op).channel)) === getRaw(current));
|
|
257
|
+
for (const [operationName, op] of operations) {
|
|
258
|
+
operation(operationName, op, deeper(depth), false);
|
|
259
|
+
}
|
|
260
|
+
// Include channel messages even when no operation uses them.
|
|
261
|
+
const shown = new Set(operations.flatMap(([, op]) => messages(withTraits(op), current).map(([, msg]) => getRaw(object(msg)))));
|
|
262
|
+
for (const [messageName, msg] of entries(current.messages)) {
|
|
263
|
+
if (!shown.has(getRaw(object(msg)))) {
|
|
264
|
+
message(messageName, msg, deeper(depth));
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
const introduction = () => {
|
|
269
|
+
const info = object(document.info);
|
|
270
|
+
blocks.push(heading(1, text(title(info, 'AsyncAPI'))));
|
|
271
|
+
fields({ version: info.version, asyncapi: document['x-original-aas-version'] ?? document.asyncapi }, [
|
|
272
|
+
'version',
|
|
273
|
+
'asyncapi',
|
|
274
|
+
]);
|
|
275
|
+
fields(document, ['id', 'defaultContentType']);
|
|
276
|
+
metadata(info, false);
|
|
277
|
+
fields(info, ['termsOfService']);
|
|
278
|
+
for (const name of ['contact', 'license']) {
|
|
279
|
+
if (info[name]) {
|
|
280
|
+
blocks.push(heading(2, text(name === 'contact' ? 'Contact' : 'License')));
|
|
281
|
+
fields(object(info[name]), ['name', 'email', 'url']);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
servers(entries(document.servers), 2);
|
|
285
|
+
};
|
|
286
|
+
const model = (name, value, depth) => {
|
|
287
|
+
blocks.push(...references.model(value, () => [
|
|
288
|
+
heading(depth, text(title(object(value), name))),
|
|
289
|
+
...schema(value, true),
|
|
290
|
+
...(view(object(value)).type === 'object' || view(object(value)).properties !== undefined
|
|
291
|
+
? examples(value, linked && !selectedModel, linked && selectedModel)
|
|
292
|
+
: []),
|
|
293
|
+
]));
|
|
294
|
+
};
|
|
295
|
+
return {
|
|
296
|
+
blocks,
|
|
297
|
+
introduction,
|
|
298
|
+
channel,
|
|
299
|
+
operation,
|
|
300
|
+
message,
|
|
301
|
+
model,
|
|
302
|
+
finish: () => {
|
|
303
|
+
blocks.push(...references.appendix((value) => schema(value, true)));
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AsyncApiMarkdownRenderer } from './types.js';
|
|
2
|
+
/** Each render owns its selection, schema expansion state and description reference namespace. */
|
|
3
|
+
export declare const createRenderer: (document: Record<string, unknown>) => AsyncApiMarkdownRenderer;
|
|
4
|
+
//# sourceMappingURL=render-document.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render-document.d.ts","sourceRoot":"","sources":["../src/render-document.ts"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAA;AAIvD,kGAAkG;AAClG,eAAO,MAAM,cAAc,GAAI,UAAU,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,wBAkIlE,CAAA"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { getRaw } from '@scalar/json-magic/magic-proxy';
|
|
2
|
+
import { anchor, createDescriptionParser, createDocumentAnchors, describe, expandDescriptions, heading, item, link, list, paragraph, text, } from '@scalar/openapi-to-markdown/markdown';
|
|
3
|
+
import { resolveTraits } from '@scalar/workspace-store/helpers/resolve-traits';
|
|
4
|
+
import remarkGfm from 'remark-gfm';
|
|
5
|
+
import remarkStringify from 'remark-stringify';
|
|
6
|
+
import { unified } from 'unified';
|
|
7
|
+
import { object, own, select, string } from './document.js';
|
|
8
|
+
import { createContentRenderer } from './render-content.js';
|
|
9
|
+
import { createSchemaRegistry } from './schema-references.js';
|
|
10
|
+
import { createSchemaView } from './schema-view.js';
|
|
11
|
+
const serializer = unified().use(remarkGfm).use(remarkStringify, { bullet: '-' }).freeze();
|
|
12
|
+
/** Each render owns its selection, schema expansion state and description reference namespace. */
|
|
13
|
+
export const createRenderer = (document) => {
|
|
14
|
+
const descriptions = createDescriptionParser();
|
|
15
|
+
const registry = createSchemaRegistry(document);
|
|
16
|
+
const view = createSchemaView();
|
|
17
|
+
return {
|
|
18
|
+
render: async (options = {}) => {
|
|
19
|
+
const selection = select(document, options);
|
|
20
|
+
const { blocks, introduction, channel, operation, message, model, finish } = createContentRenderer(document, registry.forPage(options, selection === undefined), view, options.schemaReferences?.mode === 'linked', typeof selection === 'object' && selection.kind === 'model', typeof selection === 'object' && selection.kind === 'tag' ? selection.name : undefined, options.schemaReferences?.inlinePrimitives !== false);
|
|
21
|
+
if (selection === 'introduction' || selection === undefined) {
|
|
22
|
+
introduction();
|
|
23
|
+
}
|
|
24
|
+
if (selection && selection !== 'introduction') {
|
|
25
|
+
if (selection.kind === 'tag') {
|
|
26
|
+
const tagged = (input) => {
|
|
27
|
+
const value = object(input);
|
|
28
|
+
const merged = resolveTraits(value, Array.isArray(value.traits) ? value.traits.map(object) : []);
|
|
29
|
+
return Array.isArray(merged.tags) && merged.tags.some((tag) => object(tag).name === selection.name);
|
|
30
|
+
};
|
|
31
|
+
const infoTags = object(document.info).tags;
|
|
32
|
+
const tags = Array.isArray(infoTags) ? infoTags.filter((tag) => object(tag).name === selection.name) : [];
|
|
33
|
+
if (tags.length > 1) {
|
|
34
|
+
throw new Error(`Multiple tags found for "${selection.name}"`);
|
|
35
|
+
}
|
|
36
|
+
const tag = tags[0];
|
|
37
|
+
introduction();
|
|
38
|
+
blocks.push(heading(2, text(selection.name)), ...describe(object(tag).description));
|
|
39
|
+
const external = object(object(tag).externalDocs);
|
|
40
|
+
if (typeof external.url === 'string') {
|
|
41
|
+
blocks.push(paragraph(link(external.url, string(external.description) || 'External documentation')));
|
|
42
|
+
}
|
|
43
|
+
const shown = new Set();
|
|
44
|
+
let matched = tag !== undefined;
|
|
45
|
+
for (const [name, value] of Object.entries(object(document.channels))) {
|
|
46
|
+
if (tagged(value)) {
|
|
47
|
+
channel(name, value, 2);
|
|
48
|
+
shown.add(getRaw(object(value)));
|
|
49
|
+
matched = true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
for (const [name, value] of Object.entries(object(document.operations))) {
|
|
53
|
+
if (tagged(value) && !shown.has(getRaw(object(object(value).channel)))) {
|
|
54
|
+
operation(name, value, 2, true);
|
|
55
|
+
matched = true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for (const [name, value] of Object.entries(object(own(document.components, 'messages')))) {
|
|
59
|
+
if (tagged(value)) {
|
|
60
|
+
message(name, value, 2);
|
|
61
|
+
matched = true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
for (const [channelName, value] of Object.entries(object(document.channels))) {
|
|
65
|
+
if (!shown.has(getRaw(object(value)))) {
|
|
66
|
+
for (const [name, msg] of Object.entries(object(object(value).messages))) {
|
|
67
|
+
if (tagged(msg)) {
|
|
68
|
+
message(`${channelName}: ${name}`, msg, 2);
|
|
69
|
+
matched = true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (!matched) {
|
|
75
|
+
throw new Error(`Unknown tag: ${selection.name}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else if (selection.kind === 'channel') {
|
|
79
|
+
channel(selection.name, selection.value, 1);
|
|
80
|
+
}
|
|
81
|
+
else if (selection.kind === 'operation') {
|
|
82
|
+
operation(selection.name, selection.value, 1, true);
|
|
83
|
+
}
|
|
84
|
+
else if (selection.kind === 'message') {
|
|
85
|
+
message(selection.name, selection.value, 1);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
model(selection.name, selection.value, 1);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else if (selection === undefined) {
|
|
92
|
+
const tags = object(document.info).tags;
|
|
93
|
+
if (Array.isArray(tags) && tags.length) {
|
|
94
|
+
blocks.push(heading(2, text('Tags')));
|
|
95
|
+
for (const tag of tags) {
|
|
96
|
+
const value = object(tag);
|
|
97
|
+
blocks.push(heading(3, text(string(value.name) ?? 'Tag')), ...describe(value.description));
|
|
98
|
+
const external = object(value.externalDocs);
|
|
99
|
+
if (typeof external.url === 'string') {
|
|
100
|
+
blocks.push(paragraph(link(external.url, string(external.description) || 'External documentation')));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (const [name, value] of Object.entries(object(document.channels))) {
|
|
105
|
+
channel(name, value, 2);
|
|
106
|
+
}
|
|
107
|
+
for (const [name, value] of Object.entries(object(own(document.components, 'messages')))) {
|
|
108
|
+
message(name, value, 2);
|
|
109
|
+
}
|
|
110
|
+
for (const [name, value] of Object.entries(object(own(document.components, 'schemas')))) {
|
|
111
|
+
model(`Schema: ${name}`, value, 2);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
finish();
|
|
115
|
+
// Explicit anchors keep navigation independent of a Markdown viewer's slug rules.
|
|
116
|
+
const anchors = createDocumentAnchors();
|
|
117
|
+
const contents = [];
|
|
118
|
+
for (const block of [...blocks]) {
|
|
119
|
+
if (block.type !== 'heading' || block.depth < 2 || block.depth > 3) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const label = block.children.map((child) => ('value' in child ? child.value : '')).join('');
|
|
123
|
+
const index = blocks.indexOf(block);
|
|
124
|
+
const previous = blocks[index - 1];
|
|
125
|
+
const existing = previous?.type === 'html' ? /<a id="([^"]+)"/.exec(previous.value)?.[1] : undefined;
|
|
126
|
+
const id = existing ?? anchors.get('section', String(index), label);
|
|
127
|
+
if (!existing) {
|
|
128
|
+
blocks.splice(index, 0, anchor(id));
|
|
129
|
+
}
|
|
130
|
+
contents.push(item(paragraph(link(`#${id}`, label))));
|
|
131
|
+
}
|
|
132
|
+
if (contents.length && selection !== 'introduction') {
|
|
133
|
+
const index = blocks.findIndex((block) => block.type === 'heading' && block.depth === 1);
|
|
134
|
+
blocks.splice(index + 1, 0, heading(2, text('Contents')), list(contents));
|
|
135
|
+
}
|
|
136
|
+
await expandDescriptions(blocks, descriptions());
|
|
137
|
+
return serializer.stringify({ type: 'root', children: blocks });
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { RootContent } from 'mdast';
|
|
2
|
+
/** Authored values stay local; generated examples are cached and linked within a page. */
|
|
3
|
+
export declare const createExampleRenderer: (inlinePrimitives?: boolean) => ((input: unknown, linked?: boolean, stubReferences?: boolean, mediaType?: string, context?: string) => RootContent[]);
|
|
4
|
+
//# sourceMappingURL=render-examples.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render-examples.d.ts","sourceRoot":"","sources":["../src/render-examples.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AAyGxC,0FAA0F;AAC1F,eAAO,MAAM,qBAAqB,GAChC,0BAAuB,KACtB,CAAC,CACF,KAAK,EAAE,OAAO,EACd,MAAM,CAAC,EAAE,OAAO,EAChB,cAAc,CAAC,EAAE,OAAO,EACxB,SAAS,CAAC,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,MAAM,KACb,WAAW,EAAE,CAsDjB,CAAA"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { isXmlMediaType } from '@scalar/helpers/http/is-xml-media-type';
|
|
2
|
+
import { isObject } from '@scalar/helpers/object/is-object';
|
|
3
|
+
import { getRaw } from '@scalar/json-magic/magic-proxy';
|
|
4
|
+
import { anchor, createDocumentAnchors, link, paragraph, strong, text } from '@scalar/openapi-to-markdown/markdown';
|
|
5
|
+
import { getExampleFromSchema, getXmlBodyExample } from '@scalar/workspace-store/request-example';
|
|
6
|
+
import { object, resolve } from './document.js';
|
|
7
|
+
import { code } from './render-schema.js';
|
|
8
|
+
import { isJsonSchemaFormat } from './schema-format.js';
|
|
9
|
+
import { createSchemaView, isPrimitiveSchema } from './schema-view.js';
|
|
10
|
+
/** Bound the generator before expanding a shared or recursive schema graph. */
|
|
11
|
+
const size = (input) => {
|
|
12
|
+
const cache = new Map();
|
|
13
|
+
const count = (input, depth) => {
|
|
14
|
+
const resolved = resolve(input);
|
|
15
|
+
if (!isObject(resolved) || depth > 10) {
|
|
16
|
+
return 1;
|
|
17
|
+
}
|
|
18
|
+
const schema = object(resolved);
|
|
19
|
+
if (schema.example !== undefined || (Array.isArray(schema.examples) && schema.examples.length)) {
|
|
20
|
+
return 1;
|
|
21
|
+
}
|
|
22
|
+
const identity = getRaw(resolved);
|
|
23
|
+
const previous = cache.get(identity)?.get(depth);
|
|
24
|
+
if (previous !== undefined) {
|
|
25
|
+
return previous;
|
|
26
|
+
}
|
|
27
|
+
let total = 1;
|
|
28
|
+
const add = (value) => {
|
|
29
|
+
if (total <= 10000) {
|
|
30
|
+
total += count(value, depth + 1);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
for (const keyword of ['properties', 'patternProperties']) {
|
|
34
|
+
Object.values(object(schema[keyword])).forEach(add);
|
|
35
|
+
}
|
|
36
|
+
for (const keyword of ['items', 'additionalProperties', 'additionalItems', 'allOf', 'anyOf', 'oneOf']) {
|
|
37
|
+
const value = schema[keyword];
|
|
38
|
+
if (Array.isArray(value)) {
|
|
39
|
+
value.forEach(add);
|
|
40
|
+
}
|
|
41
|
+
else if (value !== undefined) {
|
|
42
|
+
add(value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const levels = cache.get(identity) ?? new Map();
|
|
46
|
+
levels.set(depth, total);
|
|
47
|
+
cache.set(identity, levels);
|
|
48
|
+
return total;
|
|
49
|
+
};
|
|
50
|
+
return count(input, 0);
|
|
51
|
+
};
|
|
52
|
+
/** Keep linked model examples local, preserving primitive references and authored data. */
|
|
53
|
+
const stubLinkedSchemas = (input, depth = 0, view = createSchemaView(), inlinePrimitives = true) => {
|
|
54
|
+
if (depth >= 10) {
|
|
55
|
+
return {};
|
|
56
|
+
}
|
|
57
|
+
const schema = object(input);
|
|
58
|
+
if (depth > 0 &&
|
|
59
|
+
isObject(input) &&
|
|
60
|
+
typeof input.$ref === 'string' &&
|
|
61
|
+
(!inlinePrimitives || !isPrimitiveSchema(view(schema)))) {
|
|
62
|
+
return { type: 'object', properties: {} };
|
|
63
|
+
}
|
|
64
|
+
return Object.fromEntries(Object.entries(schema).map(([key, value]) => {
|
|
65
|
+
if (['properties', 'patternProperties', 'dependencies'].includes(key)) {
|
|
66
|
+
return [
|
|
67
|
+
key,
|
|
68
|
+
Object.fromEntries(Object.entries(object(value)).map(([name, child]) => [
|
|
69
|
+
name,
|
|
70
|
+
Array.isArray(child) ? child : stubLinkedSchemas(child, depth + 1, view, inlinePrimitives),
|
|
71
|
+
])),
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
if ([
|
|
75
|
+
'items',
|
|
76
|
+
'additionalProperties',
|
|
77
|
+
'additionalItems',
|
|
78
|
+
'allOf',
|
|
79
|
+
'oneOf',
|
|
80
|
+
'anyOf',
|
|
81
|
+
'contains',
|
|
82
|
+
'not',
|
|
83
|
+
'if',
|
|
84
|
+
'then',
|
|
85
|
+
'else',
|
|
86
|
+
].includes(key)) {
|
|
87
|
+
return [
|
|
88
|
+
key,
|
|
89
|
+
typeof value === 'boolean'
|
|
90
|
+
? value
|
|
91
|
+
: Array.isArray(value)
|
|
92
|
+
? value.map((child) => stubLinkedSchemas(child, depth + 1, view, inlinePrimitives))
|
|
93
|
+
: stubLinkedSchemas(value, depth + 1, view, inlinePrimitives),
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
return [key, value];
|
|
97
|
+
}));
|
|
98
|
+
};
|
|
99
|
+
/** Authored values stay local; generated examples are cached and linked within a page. */
|
|
100
|
+
export const createExampleRenderer = (inlinePrimitives = true) => {
|
|
101
|
+
const anchors = createDocumentAnchors();
|
|
102
|
+
const generated = new Map();
|
|
103
|
+
let exampleCount = 0;
|
|
104
|
+
return (input, linked = false, stubReferences = false, mediaType = 'application/json', context = '') => {
|
|
105
|
+
const schema = object(input);
|
|
106
|
+
if ('schema' in schema) {
|
|
107
|
+
// Other schema languages need their own example generators.
|
|
108
|
+
if (!isJsonSchemaFormat(schema.schemaFormat)) {
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
input = schema.schema;
|
|
112
|
+
}
|
|
113
|
+
if (resolve(input) === false) {
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
const value = object(input);
|
|
117
|
+
if (value.example !== undefined || (Array.isArray(value.examples) && value.examples.length) || linked) {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
const identity = getRaw(resolve(input));
|
|
121
|
+
const scope = JSON.stringify([mediaType, context, stubReferences]);
|
|
122
|
+
const label = context ? `Generated ${context} example` : 'Generated example';
|
|
123
|
+
const previous = generated.get(identity)?.get(scope);
|
|
124
|
+
if (previous) {
|
|
125
|
+
return previous.nodes.length ? [paragraph(link(`#${previous.id}`, label))] : [];
|
|
126
|
+
}
|
|
127
|
+
const exampleSchema = stubReferences ? stubLinkedSchemas(value, 0, createSchemaView(), inlinePrimitives) : value;
|
|
128
|
+
if (size(exampleSchema) > 10000) {
|
|
129
|
+
return [paragraph(text('Generated example omitted because it is too large.'))];
|
|
130
|
+
}
|
|
131
|
+
// The shared generator consumes JSON Schema keywords; AsyncAPI direction does not
|
|
132
|
+
// imply OpenAPI request/response readOnly or writeOnly filtering.
|
|
133
|
+
const example = getExampleFromSchema(exampleSchema);
|
|
134
|
+
const id = anchors.get('example', String(++exampleCount));
|
|
135
|
+
const xml = isXmlMediaType(mediaType);
|
|
136
|
+
const xmlExample = xml ? getXmlBodyExample(exampleSchema, { value: example }) : undefined;
|
|
137
|
+
const nodes = example === undefined
|
|
138
|
+
? []
|
|
139
|
+
: [
|
|
140
|
+
anchor(id),
|
|
141
|
+
paragraph(strong(text(`${label}:`))),
|
|
142
|
+
...(xml
|
|
143
|
+
? xmlExample?.xml !== undefined
|
|
144
|
+
? [code(xmlExample.xml, 'xml')]
|
|
145
|
+
: [paragraph(text('Unable to generate an XML example.'))]
|
|
146
|
+
: [code(example)]),
|
|
147
|
+
];
|
|
148
|
+
const scopes = generated.get(identity) ?? new Map();
|
|
149
|
+
scopes.set(scope, { id, nodes });
|
|
150
|
+
generated.set(identity, scopes);
|
|
151
|
+
return nodes;
|
|
152
|
+
};
|
|
153
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Code, RootContent } from 'mdast';
|
|
2
|
+
/** Serialize authored data without adding workspace reference metadata or interpreting literal $ref keys. */
|
|
3
|
+
export declare const code: (value: unknown, lang?: string) => Code;
|
|
4
|
+
/** Render AsyncAPI / Draft 7 schemas without coercing them through OpenAPI's schema dialect. */
|
|
5
|
+
export declare const createSchemaRenderer: (reference?: (input: unknown, depth: number, mediaType?: string) => RootContent[] | undefined, view?: (input: Record<string, unknown>) => Record<string, unknown>, preserveReferences?: boolean) => ((input: unknown, expand?: boolean, mediaType?: string) => RootContent[]);
|
|
6
|
+
/** Authored examples remain at every usage, including when the schema itself becomes a link. */
|
|
7
|
+
export declare const renderAuthoredSchemaExamples: (input: unknown, mediaType?: string) => RootContent[];
|
|
8
|
+
/** Preserve schema documentation at both expanded definitions and linked usages. */
|
|
9
|
+
export declare const renderSchemaDocumentation: (input: unknown) => RootContent[];
|
|
10
|
+
//# sourceMappingURL=render-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render-schema.d.ts","sourceRoot":"","sources":["../src/render-schema.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,IAAI,EAA6B,WAAW,EAAE,MAAM,OAAO,CAAA;AAMzE,6GAA6G;AAC7G,eAAO,MAAM,IAAI,GAAI,OAAO,OAAO,EAAE,aAAa,KAAG,IAInD,CAAA;AAwCF,gGAAgG;AAChG,eAAO,MAAM,oBAAoB,GAC/B,YAAW,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,KAAK,WAAW,EAAE,GAAG,SAA2B,EAC7G,kEAAyB,EACzB,4BAA0B,KACzB,CAAC,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM,KAAK,WAAW,EAAE,CA4J1E,CAAA;AAED,gGAAgG;AAChG,eAAO,MAAM,4BAA4B,GAAI,OAAO,OAAO,EAAE,kBAA8B,KAAG,WAAW,EAwBxG,CAAA;AAED,oFAAoF;AACpF,eAAO,MAAM,yBAAyB,GAAI,OAAO,OAAO,KAAG,WAAW,EASrE,CAAA"}
|