@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,205 @@
|
|
|
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 { describe, inlineCode, item, link, list, paragraph, strong, text } from '@scalar/openapi-to-markdown/markdown';
|
|
5
|
+
import { getXmlBodyExample } from '@scalar/workspace-store/request-example';
|
|
6
|
+
import { object, resolve, string } from './document.js';
|
|
7
|
+
import { isJsonSchemaFormat } from './schema-format.js';
|
|
8
|
+
import { createSchemaView } from './schema-view.js';
|
|
9
|
+
/** Serialize authored data without adding workspace reference metadata or interpreting literal $ref keys. */
|
|
10
|
+
export const code = (value, lang = 'json') => ({
|
|
11
|
+
type: 'code',
|
|
12
|
+
lang,
|
|
13
|
+
value: lang !== 'json' && typeof value === 'string' ? value : (JSON.stringify(getRaw(value), null, 2) ?? 'null'),
|
|
14
|
+
});
|
|
15
|
+
const annotations = [
|
|
16
|
+
'title',
|
|
17
|
+
'$id',
|
|
18
|
+
'$schema',
|
|
19
|
+
'contentEncoding',
|
|
20
|
+
'contentMediaType',
|
|
21
|
+
'format',
|
|
22
|
+
'enum',
|
|
23
|
+
'const',
|
|
24
|
+
'default',
|
|
25
|
+
'readOnly',
|
|
26
|
+
'writeOnly',
|
|
27
|
+
'deprecated',
|
|
28
|
+
'minimum',
|
|
29
|
+
'maximum',
|
|
30
|
+
'exclusiveMinimum',
|
|
31
|
+
'exclusiveMaximum',
|
|
32
|
+
'multipleOf',
|
|
33
|
+
'minLength',
|
|
34
|
+
'maxLength',
|
|
35
|
+
'pattern',
|
|
36
|
+
'minItems',
|
|
37
|
+
'maxItems',
|
|
38
|
+
'uniqueItems',
|
|
39
|
+
'minProperties',
|
|
40
|
+
'maxProperties',
|
|
41
|
+
];
|
|
42
|
+
/** Infer the JSON type of an enum or constant without changing its constraints. */
|
|
43
|
+
const literalType = (value) => value === null
|
|
44
|
+
? 'null'
|
|
45
|
+
: Array.isArray(value)
|
|
46
|
+
? 'array'
|
|
47
|
+
: typeof value === 'number' && Number.isInteger(value)
|
|
48
|
+
? 'integer'
|
|
49
|
+
: typeof value;
|
|
50
|
+
/** Render AsyncAPI / Draft 7 schemas without coercing them through OpenAPI's schema dialect. */
|
|
51
|
+
export const createSchemaRenderer = (reference = () => undefined, view = createSchemaView(), preserveReferences = false) => {
|
|
52
|
+
const state = { remaining: 5000 };
|
|
53
|
+
const render = (input, ancestors, depth, expand = false, mediaType = 'application/json') => {
|
|
54
|
+
const linked = expand ? undefined : reference(input, depth, mediaType);
|
|
55
|
+
if (linked) {
|
|
56
|
+
return linked;
|
|
57
|
+
}
|
|
58
|
+
const resolved = resolve(input);
|
|
59
|
+
if (typeof resolved === 'boolean') {
|
|
60
|
+
return [paragraph(text(resolved ? 'Any value is allowed.' : 'No value is allowed.'))];
|
|
61
|
+
}
|
|
62
|
+
if (!isObject(resolved)) {
|
|
63
|
+
return [code(resolved)];
|
|
64
|
+
}
|
|
65
|
+
const raw = object(resolved);
|
|
66
|
+
const hasReferenceBranch = ['allOf', 'anyOf', 'oneOf'].some((keyword) => Array.isArray(raw[keyword]) &&
|
|
67
|
+
raw[keyword].some((branch) => isObject(branch) && typeof branch.$ref === 'string'));
|
|
68
|
+
const schema = preserveReferences && hasReferenceBranch ? raw : view(raw);
|
|
69
|
+
if ('schema' in schema) {
|
|
70
|
+
const format = string(schema.schemaFormat);
|
|
71
|
+
if (isJsonSchemaFormat(format)) {
|
|
72
|
+
return render(schema.schema, ancestors, depth, expand, mediaType);
|
|
73
|
+
}
|
|
74
|
+
return [
|
|
75
|
+
paragraph(strong(text('Schema format:')), text(' '), inlineCode(format ?? '')),
|
|
76
|
+
code(schema.schema, typeof schema.schema === 'string' ? 'text' : 'json'),
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
const identity = getRaw(resolved);
|
|
80
|
+
if (ancestors.has(identity)) {
|
|
81
|
+
const reference = isObject(input) ? string(input.$ref) : undefined;
|
|
82
|
+
return [paragraph(text('Recursive schema'), ...(reference ? [text(': '), inlineCode(reference)] : []))];
|
|
83
|
+
}
|
|
84
|
+
if (depth >= 10 || state.remaining-- <= 0) {
|
|
85
|
+
return [paragraph(text('Schema expansion limit reached.'))];
|
|
86
|
+
}
|
|
87
|
+
const next = new Set(ancestors).add(identity);
|
|
88
|
+
const inferred = schema.const !== undefined
|
|
89
|
+
? literalType(schema.const)
|
|
90
|
+
: Array.isArray(schema.enum) && schema.enum.length
|
|
91
|
+
? [...new Set(schema.enum.map(literalType))].join(' | ')
|
|
92
|
+
: schema.properties || isObject(schema.additionalProperties)
|
|
93
|
+
? 'object'
|
|
94
|
+
: schema.items !== undefined
|
|
95
|
+
? 'array'
|
|
96
|
+
: undefined;
|
|
97
|
+
const type = Array.isArray(schema.type) ? schema.type.join(' | ') : (string(schema.type) ?? inferred);
|
|
98
|
+
const summary = type ? [inlineCode(type)] : [];
|
|
99
|
+
for (const name of annotations) {
|
|
100
|
+
if (schema[name] !== undefined) {
|
|
101
|
+
summary.push(text(`${summary.length ? ', ' : ''}${name}: `), inlineCode(typeof schema[name] === 'string' ? schema[name] : JSON.stringify(getRaw(schema[name]))));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const blocks = [
|
|
105
|
+
...(summary.length ? [paragraph(...summary)] : []),
|
|
106
|
+
...renderSchemaDocumentation(schema),
|
|
107
|
+
];
|
|
108
|
+
const discriminator = schema.discriminator;
|
|
109
|
+
const propertyName = typeof discriminator === 'string' ? discriminator : string(object(discriminator).propertyName);
|
|
110
|
+
if (propertyName) {
|
|
111
|
+
blocks.push(paragraph(strong(text('Discriminator:')), text(' '), inlineCode(propertyName)));
|
|
112
|
+
const mappings = Object.entries(object(object(discriminator).mapping));
|
|
113
|
+
if (mappings.length) {
|
|
114
|
+
blocks.push(list(mappings.map(([name, target]) => item(paragraph(inlineCode(name), text(': '), inlineCode(String(target)))))));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const required = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
118
|
+
const unlistedRequired = [...required].filter((name) => typeof name === 'string' && !Object.hasOwn(object(schema.properties), name));
|
|
119
|
+
if (unlistedRequired.length) {
|
|
120
|
+
blocks.push(paragraph(strong(text('Required properties:')), text(' '), inlineCode(unlistedRequired.join(', '))));
|
|
121
|
+
}
|
|
122
|
+
const properties = Object.entries(object(schema.properties)).map(([name, property]) => {
|
|
123
|
+
const children = render(property, next, depth + 1);
|
|
124
|
+
const first = children[0];
|
|
125
|
+
const compact = first?.type === 'paragraph' && ['inlineCode', 'link'].includes(first.children[0]?.type ?? '');
|
|
126
|
+
return item(paragraph(strong(inlineCode(name)), ...(compact ? [text(': '), ...first.children] : []), ...(required.has(name) ? [text(' (required)')] : [])), ...(compact ? children.slice(1) : children));
|
|
127
|
+
});
|
|
128
|
+
if (properties.length) {
|
|
129
|
+
blocks.push(list(properties));
|
|
130
|
+
}
|
|
131
|
+
for (const keyword of [
|
|
132
|
+
'items',
|
|
133
|
+
'additionalItems',
|
|
134
|
+
'additionalProperties',
|
|
135
|
+
'contains',
|
|
136
|
+
'propertyNames',
|
|
137
|
+
'not',
|
|
138
|
+
'if',
|
|
139
|
+
'then',
|
|
140
|
+
'else',
|
|
141
|
+
]) {
|
|
142
|
+
if (schema[keyword] !== undefined) {
|
|
143
|
+
const values = Array.isArray(schema[keyword]) ? schema[keyword] : [schema[keyword]];
|
|
144
|
+
blocks.push(paragraph(strong(text(`${keyword}:`))));
|
|
145
|
+
for (const value of values) {
|
|
146
|
+
blocks.push(...render(value, next, depth + 1));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
for (const keyword of ['oneOf', 'anyOf', 'allOf']) {
|
|
151
|
+
if (Array.isArray(schema[keyword])) {
|
|
152
|
+
blocks.push(paragraph(strong(text(`${keyword}:`))), list(schema[keyword].map((branch) => item(...render(branch, next, depth + 1)))));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const keyword of ['patternProperties', 'dependencies']) {
|
|
156
|
+
const entries = Object.entries(object(schema[keyword]));
|
|
157
|
+
if (entries.length) {
|
|
158
|
+
blocks.push(paragraph(strong(text(`${keyword}:`))), list(entries.map(([name, value]) => item(paragraph(inlineCode(name)), ...(Array.isArray(value) ? [code(value)] : render(value, next, depth + 1))))));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
blocks.push(...renderAuthoredSchemaExamples(schema, mediaType));
|
|
162
|
+
return blocks.length ? blocks : [paragraph(text('Any value is allowed.'))];
|
|
163
|
+
};
|
|
164
|
+
return (input, expand = false, mediaType = 'application/json') => {
|
|
165
|
+
state.remaining = 5000;
|
|
166
|
+
return render(input, new Set(), 0, expand, mediaType);
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
/** Authored examples remain at every usage, including when the schema itself becomes a link. */
|
|
170
|
+
export const renderAuthoredSchemaExamples = (input, mediaType = 'application/json') => {
|
|
171
|
+
const resolved = object(input);
|
|
172
|
+
const schema = 'schema' in resolved && isJsonSchemaFormat(resolved.schemaFormat) ? object(resolved.schema) : resolved;
|
|
173
|
+
const examples = Array.isArray(schema.examples)
|
|
174
|
+
? schema.examples
|
|
175
|
+
: schema.example === undefined
|
|
176
|
+
? []
|
|
177
|
+
: [schema.example];
|
|
178
|
+
return examples.flatMap((example) => {
|
|
179
|
+
const label = paragraph(strong(text('Example:')));
|
|
180
|
+
if (!isXmlMediaType(mediaType)) {
|
|
181
|
+
return [label, code(example)];
|
|
182
|
+
}
|
|
183
|
+
if (typeof example === 'string') {
|
|
184
|
+
return [label, code(example, 'xml')];
|
|
185
|
+
}
|
|
186
|
+
const result = getXmlBodyExample(schema, { value: example });
|
|
187
|
+
return [
|
|
188
|
+
label,
|
|
189
|
+
...(result.xml !== undefined
|
|
190
|
+
? [code(result.xml, 'xml')]
|
|
191
|
+
: [paragraph(text('Unable to generate an XML example.'))]),
|
|
192
|
+
];
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
/** Preserve schema documentation at both expanded definitions and linked usages. */
|
|
196
|
+
export const renderSchemaDocumentation = (input) => {
|
|
197
|
+
const schema = object(input);
|
|
198
|
+
const external = object(schema.externalDocs);
|
|
199
|
+
return [
|
|
200
|
+
...describe(schema.description),
|
|
201
|
+
...(typeof external.url === 'string'
|
|
202
|
+
? [paragraph(link(external.url, string(external.description) || 'External documentation'))]
|
|
203
|
+
: []),
|
|
204
|
+
];
|
|
205
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-format.d.ts","sourceRoot":"","sources":["../src/schema-format.ts"],"names":[],"mappings":"AAAA,mGAAmG;AACnG,eAAO,MAAM,kBAAkB,GAAI,QAAQ,OAAO,KAAG,OAE+C,CAAA"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** Schema languages with JSON Schema keywords understood by the renderer and example generator. */
|
|
2
|
+
export const isJsonSchemaFormat = (format) => typeof format !== 'string' ||
|
|
3
|
+
/^application\/(?:schema\+(?:json|yaml)|vnd\.aai\.asyncapi(?:\+json|\+yaml)?)(?:;|$)/.test(format);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RootContent } from 'mdast';
|
|
2
|
+
import type { AsyncApiRenderOptions } from './types.js';
|
|
3
|
+
/** Index canonical model identities once, including aliases that resolve to the same schema. */
|
|
4
|
+
export declare const createSchemaRegistry: (document: Record<string, unknown>) => {
|
|
5
|
+
forPage: (options: AsyncApiRenderOptions, whole: boolean) => {
|
|
6
|
+
reference: (input: unknown, depth: number, mediaType?: string) => RootContent[] | undefined;
|
|
7
|
+
root: (input: unknown, render: () => RootContent[], mediaType?: string) => RootContent[];
|
|
8
|
+
model: (input: unknown, render: () => RootContent[], mediaType?: string) => RootContent[];
|
|
9
|
+
appendix: (render: (input: unknown) => RootContent[]) => RootContent[];
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=schema-references.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-references.d.ts","sourceRoot":"","sources":["../src/schema-references.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AAKxC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAYpD,gGAAgG;AAChG,eAAO,MAAM,oBAAoB,GAC/B,UAAU,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAChC;IACD,OAAO,EAAE,CACP,OAAO,EAAE,qBAAqB,EAC9B,KAAK,EAAE,OAAO,KACX;QACH,SAAS,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,KAAK,WAAW,EAAE,GAAG,SAAS,CAAA;QAC3F,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,EAAE,EAAE,SAAS,CAAC,EAAE,MAAM,KAAK,WAAW,EAAE,CAAA;QACxF,KAAK,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,EAAE,EAAE,SAAS,CAAC,EAAE,MAAM,KAAK,WAAW,EAAE,CAAA;QACzF,QAAQ,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,WAAW,EAAE,KAAK,WAAW,EAAE,CAAA;KACvE,CAAA;CA0GF,CAAA"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { escapeJsonPointer } from '@scalar/helpers/json/escape-json-pointer';
|
|
2
|
+
import { unescapeJsonPointerSegment } from '@scalar/helpers/json/unescape-json-pointer-segment';
|
|
3
|
+
import { getRaw } from '@scalar/json-magic/magic-proxy';
|
|
4
|
+
import { anchor, createDocumentAnchors, heading, link, paragraph, text } from '@scalar/openapi-to-markdown/markdown';
|
|
5
|
+
import { object, own, resolve } from './document.js';
|
|
6
|
+
import { renderAuthoredSchemaExamples, renderSchemaDocumentation } from './render-schema.js';
|
|
7
|
+
import { createSchemaView, isPrimitiveSchema } from './schema-view.js';
|
|
8
|
+
/** Decode only a complete component name, preserving other reference paths. */
|
|
9
|
+
const referenceName = (ref) => {
|
|
10
|
+
try {
|
|
11
|
+
const match = /^#\/components\/schemas\/([^/]+)$/.exec(decodeURIComponent(ref));
|
|
12
|
+
return match ? unescapeJsonPointerSegment(match[1]) : ref;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return ref;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
/** Index canonical model identities once, including aliases that resolve to the same schema. */
|
|
19
|
+
export const createSchemaRegistry = (document) => {
|
|
20
|
+
const view = createSchemaView();
|
|
21
|
+
const models = new Map();
|
|
22
|
+
for (const [name, value] of Object.entries(object(own(document.components, 'schemas')))) {
|
|
23
|
+
const identity = getRaw(resolve(value));
|
|
24
|
+
if (!models.has(identity)) {
|
|
25
|
+
models.set(identity, {
|
|
26
|
+
name,
|
|
27
|
+
value,
|
|
28
|
+
ref: `#/components/schemas/${escapeJsonPointer(name)}`,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
forPage: (options, whole) => {
|
|
34
|
+
const entries = new Map(models);
|
|
35
|
+
const lookup = (input) => {
|
|
36
|
+
const identity = getRaw(resolve(input));
|
|
37
|
+
const previous = entries.get(identity);
|
|
38
|
+
if (previous) {
|
|
39
|
+
return previous;
|
|
40
|
+
}
|
|
41
|
+
if (input && typeof input === 'object' && '$ref' in input && typeof input.$ref === 'string') {
|
|
42
|
+
const ref = input.$ref;
|
|
43
|
+
const name = unescapeJsonPointerSegment(ref.split('/').at(-1)) || ref;
|
|
44
|
+
const entry = { name, ref, value: input };
|
|
45
|
+
entries.set(identity, entry);
|
|
46
|
+
return entry;
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
};
|
|
50
|
+
const anchors = createDocumentAnchors();
|
|
51
|
+
const queued = new Set();
|
|
52
|
+
const shown = new Set();
|
|
53
|
+
const id = (entry) => anchors.get('model', entry.ref, entry.name);
|
|
54
|
+
const model = (input, render, mediaType = 'application/json') => {
|
|
55
|
+
const identity = getRaw(resolve(input));
|
|
56
|
+
const entry = lookup(input);
|
|
57
|
+
if (!entry) {
|
|
58
|
+
return render();
|
|
59
|
+
}
|
|
60
|
+
if (shown.has(identity)) {
|
|
61
|
+
return [
|
|
62
|
+
paragraph(link(`#${id(entry)}`, entry.name)),
|
|
63
|
+
...renderSchemaDocumentation(input),
|
|
64
|
+
...renderAuthoredSchemaExamples(input, mediaType),
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
shown.add(identity);
|
|
68
|
+
return [anchor(id(entry)), ...render()];
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
reference: (input, depth, mediaType = 'application/json') => {
|
|
72
|
+
const identity = getRaw(resolve(input));
|
|
73
|
+
const entry = lookup(input);
|
|
74
|
+
if (!entry || (!whole && depth === 0)) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
const schema = view(object(entry.value));
|
|
78
|
+
const primitive = isPrimitiveSchema(schema);
|
|
79
|
+
if (primitive && options.schemaReferences?.inlinePrimitives !== false) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
if (options.schemaReferences) {
|
|
83
|
+
const ref = input && typeof input === 'object' && '$ref' in input && typeof input.$ref === 'string'
|
|
84
|
+
? input.$ref
|
|
85
|
+
: entry.ref;
|
|
86
|
+
const name = referenceName(ref);
|
|
87
|
+
const url = options.schemaReferences.resolveUrl?.({ ref, name });
|
|
88
|
+
const destination = url ? link(url, name) : undefined;
|
|
89
|
+
return [
|
|
90
|
+
paragraph(destination?.url ? destination : text(name)),
|
|
91
|
+
...renderSchemaDocumentation(input),
|
|
92
|
+
...renderAuthoredSchemaExamples(input, mediaType),
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
queued.add(identity);
|
|
96
|
+
return [
|
|
97
|
+
paragraph(link(`#${id(entry)}`, entry.name)),
|
|
98
|
+
...renderSchemaDocumentation(input),
|
|
99
|
+
...renderAuthoredSchemaExamples(input, mediaType),
|
|
100
|
+
];
|
|
101
|
+
},
|
|
102
|
+
model,
|
|
103
|
+
root: (input, render, mediaType) => (whole ? render() : model(input, render, mediaType)),
|
|
104
|
+
appendix: (render) => {
|
|
105
|
+
const blocks = [];
|
|
106
|
+
if (options.schemaReferences) {
|
|
107
|
+
return blocks;
|
|
108
|
+
}
|
|
109
|
+
// Set iteration includes dependencies discovered while rendering earlier models.
|
|
110
|
+
for (const identity of queued) {
|
|
111
|
+
const entry = entries.get(identity);
|
|
112
|
+
if (!shown.has(identity)) {
|
|
113
|
+
blocks.push(...model(entry.value, () => [heading(2, text(`Schema: ${entry.name}`)), ...render(entry.value)]));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return blocks;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Primitive references have no structured values or child schemas to expand. */
|
|
2
|
+
export declare const isPrimitiveSchema: (schema: Record<string, unknown>) => boolean;
|
|
3
|
+
/** Cache display-only simplifications within one immutable document snapshot. */
|
|
4
|
+
export declare const createSchemaView: () => ((input: Record<string, unknown>) => Record<string, unknown>);
|
|
5
|
+
//# sourceMappingURL=schema-view.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-view.d.ts","sourceRoot":"","sources":["../src/schema-view.ts"],"names":[],"mappings":"AAIA,iFAAiF;AACjF,eAAO,MAAM,iBAAiB,GAAI,QAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,OAgBnE,CAAA;AAED,iFAAiF;AACjF,eAAO,MAAM,gBAAgB,QAAO,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAkG/F,CAAA"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { getRaw } from '@scalar/json-magic/magic-proxy';
|
|
2
|
+
import { object } from './document.js';
|
|
3
|
+
/** Primitive references have no structured values or child schemas to expand. */
|
|
4
|
+
export const isPrimitiveSchema = (schema) => {
|
|
5
|
+
const types = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
6
|
+
const primitiveValue = (value) => value === null || typeof value !== 'object';
|
|
7
|
+
return (!types.some((type) => type === 'object' || type === 'array') &&
|
|
8
|
+
!schema.properties &&
|
|
9
|
+
!schema.items &&
|
|
10
|
+
!schema.allOf &&
|
|
11
|
+
!schema.anyOf &&
|
|
12
|
+
!schema.oneOf &&
|
|
13
|
+
(types.every((type) => typeof type === 'string') ||
|
|
14
|
+
(schema.const !== undefined && primitiveValue(schema.const)) ||
|
|
15
|
+
(Array.isArray(schema.enum) && schema.enum.every(primitiveValue))) &&
|
|
16
|
+
(schema.const === undefined || primitiveValue(schema.const)) &&
|
|
17
|
+
(!Array.isArray(schema.enum) || schema.enum.every(primitiveValue)));
|
|
18
|
+
};
|
|
19
|
+
/** Cache display-only simplifications within one immutable document snapshot. */
|
|
20
|
+
export const createSchemaView = () => {
|
|
21
|
+
const cache = new WeakMap();
|
|
22
|
+
const view = (schema) => {
|
|
23
|
+
const identity = getRaw(schema);
|
|
24
|
+
const previous = cache.get(identity);
|
|
25
|
+
if (previous) {
|
|
26
|
+
return previous;
|
|
27
|
+
}
|
|
28
|
+
// Seed first so cyclic compositions retain their original shape.
|
|
29
|
+
cache.set(identity, schema);
|
|
30
|
+
const keys = Object.keys(schema);
|
|
31
|
+
if (Array.isArray(schema.allOf) &&
|
|
32
|
+
schema.allOf.length === 1 &&
|
|
33
|
+
keys.every((key) => ['allOf', 'title', 'description', 'default', 'deprecated', 'readOnly', 'writeOnly'].includes(key))) {
|
|
34
|
+
const branch = object(schema.allOf[0]);
|
|
35
|
+
if (Object.keys(branch).length) {
|
|
36
|
+
const normalized = {
|
|
37
|
+
...view(branch),
|
|
38
|
+
...Object.fromEntries(Object.entries(schema).filter(([key]) => key !== 'allOf')),
|
|
39
|
+
};
|
|
40
|
+
cache.set(identity, normalized);
|
|
41
|
+
return normalized;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
for (const keyword of ['anyOf', 'oneOf']) {
|
|
45
|
+
const branches = schema[keyword];
|
|
46
|
+
if (Array.isArray(branches) &&
|
|
47
|
+
keys.every((key) => [keyword, 'title', 'description', 'default', 'deprecated', 'readOnly', 'writeOnly'].includes(key))) {
|
|
48
|
+
const types = branches.map((branch) => object(branch));
|
|
49
|
+
const nullBranch = types.find((branch) => branch.type === 'null' &&
|
|
50
|
+
Object.keys(branch).every((key) => ['type', 'title', 'description'].includes(key)));
|
|
51
|
+
const nonNull = types.find((branch) => branch !== nullBranch);
|
|
52
|
+
const branchTypes = Array.isArray(nonNull?.type) ? nonNull.type : [nonNull?.type];
|
|
53
|
+
// Type-specific constraints do not constrain null. Global applicators may,
|
|
54
|
+
// so retain those compositions instead of changing their apparent meaning.
|
|
55
|
+
if (types.length === 2 &&
|
|
56
|
+
nullBranch &&
|
|
57
|
+
nonNull &&
|
|
58
|
+
!(nonNull.const !== undefined && Array.isArray(nonNull.enum)) &&
|
|
59
|
+
branchTypes.every((type) => typeof type === 'string' && type !== 'null') &&
|
|
60
|
+
!['not', 'if', 'then', 'else', 'allOf', 'oneOf', 'anyOf', '$ref'].some((key) => key in nonNull)) {
|
|
61
|
+
const { const: constant, ...rest } = nonNull;
|
|
62
|
+
const normalized = {
|
|
63
|
+
...rest,
|
|
64
|
+
...Object.fromEntries(Object.entries(schema).filter(([key]) => key !== keyword)),
|
|
65
|
+
type: [...branchTypes, 'null'],
|
|
66
|
+
...(Array.isArray(nonNull.enum)
|
|
67
|
+
? { enum: [...nonNull.enum, null] }
|
|
68
|
+
: constant !== undefined
|
|
69
|
+
? { enum: [constant, null] }
|
|
70
|
+
: {}),
|
|
71
|
+
};
|
|
72
|
+
cache.set(identity, normalized);
|
|
73
|
+
return normalized;
|
|
74
|
+
}
|
|
75
|
+
if (types.length &&
|
|
76
|
+
types.every((branch) => Object.keys(branch).length === 1 &&
|
|
77
|
+
typeof branch.type === 'string' &&
|
|
78
|
+
!['object', 'array'].includes(branch.type))) {
|
|
79
|
+
const names = types.map((branch) => branch.type);
|
|
80
|
+
// Overlapping oneOf branches are not equivalent to a union.
|
|
81
|
+
if (keyword === 'oneOf' &&
|
|
82
|
+
(new Set(names).size !== names.length || (names.includes('integer') && names.includes('number')))) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const normalized = {
|
|
86
|
+
...Object.fromEntries(Object.entries(schema).filter(([key]) => key !== keyword)),
|
|
87
|
+
type: [...new Set(names)],
|
|
88
|
+
};
|
|
89
|
+
cache.set(identity, normalized);
|
|
90
|
+
return normalized;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return schema;
|
|
95
|
+
};
|
|
96
|
+
return view;
|
|
97
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { AsyncApiDocument } from '@scalar/types/asyncapi/3.1';
|
|
2
|
+
/** Raw input accepted by the Node.js loader. Strings may be JSON, YAML, a file path, or a URL. */
|
|
3
|
+
export type AsyncApiInput = AsyncApiDocument | Record<string, unknown> | string;
|
|
4
|
+
/** A page is identified by its map key or an exact JSON pointer, never its display title. */
|
|
5
|
+
export type AsyncApiSelector = string | {
|
|
6
|
+
pointer: string;
|
|
7
|
+
};
|
|
8
|
+
/** Choose at most one page. Omitting all selectors renders the complete API description. */
|
|
9
|
+
type PageSelectors = {
|
|
10
|
+
introduction: true;
|
|
11
|
+
channel: AsyncApiSelector;
|
|
12
|
+
operation: AsyncApiSelector;
|
|
13
|
+
/** A string selects components.messages; a pointer can also select a channel message. */
|
|
14
|
+
message: AsyncApiSelector;
|
|
15
|
+
model: AsyncApiSelector;
|
|
16
|
+
tag: string;
|
|
17
|
+
};
|
|
18
|
+
/** Control shared model links without assuming a documentation URL layout. */
|
|
19
|
+
export type AsyncApiRenderOptions = {
|
|
20
|
+
schemaReferences?: {
|
|
21
|
+
mode: 'linked';
|
|
22
|
+
/** Return a published URL, or undefined to render the model name as text. */
|
|
23
|
+
resolveUrl?: (reference: {
|
|
24
|
+
ref: string;
|
|
25
|
+
name: string;
|
|
26
|
+
}) => string | undefined;
|
|
27
|
+
/** Keep small primitive definitions inline. Defaults to true. */
|
|
28
|
+
inlinePrimitives?: boolean;
|
|
29
|
+
};
|
|
30
|
+
} & ({
|
|
31
|
+
[Key in keyof PageSelectors]: Pick<PageSelectors, Key> & Partial<Record<Exclude<keyof PageSelectors, Key>, never>>;
|
|
32
|
+
}[keyof PageSelectors] | Partial<Record<keyof PageSelectors, never>>);
|
|
33
|
+
/** A prepared document snapshot, reusable across independent page renders. */
|
|
34
|
+
export type AsyncApiMarkdownRenderer = {
|
|
35
|
+
render: (options?: AsyncApiRenderOptions) => Promise<string>;
|
|
36
|
+
};
|
|
37
|
+
export {};
|
|
38
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAElE,kGAAkG;AAClG,MAAM,MAAM,aAAa,GAAG,gBAAgB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAA;AAE/E,6FAA6F;AAC7F,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,CAAA;AAE3D,4FAA4F;AAC5F,KAAK,aAAa,GAAG;IACnB,YAAY,EAAE,IAAI,CAAA;IAClB,OAAO,EAAE,gBAAgB,CAAA;IACzB,SAAS,EAAE,gBAAgB,CAAA;IAC3B,yFAAyF;IACzF,OAAO,EAAE,gBAAgB,CAAA;IACzB,KAAK,EAAE,gBAAgB,CAAA;IACvB,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAED,8EAA8E;AAC9E,MAAM,MAAM,qBAAqB,GAAG;IAClC,gBAAgB,CAAC,EAAE;QACjB,IAAI,EAAE,QAAQ,CAAA;QACd,6EAA6E;QAC7E,UAAU,CAAC,EAAE,CAAC,SAAS,EAAE;YAAE,GAAG,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAA;SAAE,KAAK,MAAM,GAAG,SAAS,CAAA;QAC7E,iEAAiE;QACjE,gBAAgB,CAAC,EAAE,OAAO,CAAA;KAC3B,CAAA;CACF,GAAG,CACA;KACG,GAAG,IAAI,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,EAAE,GAAG,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,aAAa,EAAE,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;CACnH,CAAC,MAAM,aAAa,CAAC,GACtB,OAAO,CAAC,MAAM,CAAC,MAAM,aAAa,EAAE,KAAK,CAAC,CAAC,CAC9C,CAAA;AAED,8EAA8E;AAC9E,MAAM,MAAM,wBAAwB,GAAG;IACrC,MAAM,EAAE,CAAC,OAAO,CAAC,EAAE,qBAAqB,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;CAC7D,CAAA"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scalar/asyncapi-to-markdown",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Create plain Markdown from AsyncAPI documents (for LLMs)",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Scalar (https://github.com/scalar)",
|
|
6
|
+
"homepage": "https://github.com/scalar/scalar",
|
|
7
|
+
"bugs": "https://github.com/scalar/scalar/issues/new/choose",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/scalar/scalar.git",
|
|
11
|
+
"directory": "packages/asyncapi-to-markdown"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"asyncapi",
|
|
15
|
+
"markdown",
|
|
16
|
+
"llm"
|
|
17
|
+
],
|
|
18
|
+
"version": "0.2.1",
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "dist/index.js",
|
|
24
|
+
"module": "dist/index.js",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./browser": {
|
|
32
|
+
"import": "./dist/browser.js",
|
|
33
|
+
"types": "./dist/browser.d.ts",
|
|
34
|
+
"default": "./dist/browser.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"CHANGELOG.md"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@scalar/asyncapi-upgrader": "0.1.18",
|
|
43
|
+
"@scalar/helpers": "0.16.4",
|
|
44
|
+
"@scalar/json-magic": "0.16.1",
|
|
45
|
+
"@scalar/openapi-to-markdown": "1.6.1",
|
|
46
|
+
"@scalar/types": "0.24.0",
|
|
47
|
+
"@scalar/workspace-store": "0.68.5",
|
|
48
|
+
"remark-gfm": "^4.0.1",
|
|
49
|
+
"remark-stringify": "^11.0.0",
|
|
50
|
+
"unified": "^11.0.5"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@scalar/components": "0.32.1",
|
|
54
|
+
"@scalar/themes": "0.19.2",
|
|
55
|
+
"@tailwindcss/vite": "^4.3.3",
|
|
56
|
+
"@types/mdast": "^4.0.4",
|
|
57
|
+
"@vitejs/plugin-vue": "^6.0.8",
|
|
58
|
+
"marked": "^14.0.0",
|
|
59
|
+
"rehype-parse": "^9.0.1",
|
|
60
|
+
"rehype-sanitize": "^6.0.0",
|
|
61
|
+
"rehype-stringify": "^10.0.1",
|
|
62
|
+
"tailwindcss": "^4.3.3",
|
|
63
|
+
"tsx": "4.19.4",
|
|
64
|
+
"vite": "8.1.5",
|
|
65
|
+
"vitest": "4.1.10",
|
|
66
|
+
"vue": "^3.5.40",
|
|
67
|
+
"vue-tsc": "^3.3.8"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"benchmark": "tsx benchmarks/run.ts",
|
|
71
|
+
"build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
|
|
72
|
+
"dev": "cd playground && vite",
|
|
73
|
+
"test": "vitest --run",
|
|
74
|
+
"types:check": "tsgo --noEmit && vue-tsc --noEmit -p playground/tsconfig.json"
|
|
75
|
+
}
|
|
6
76
|
}
|