-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdom-reference.js
More file actions
165 lines (152 loc) · 5.82 KB
/
Copy pathdom-reference.js
File metadata and controls
165 lines (152 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright (c) 2026 Gennaro Prota (gennaro.prota@gmail.com)
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Official repository: https://github.com/cppalliance/mrdocs
Antora extension that renders the DOM Reference section of the
docs site from mrdocs-dom-schema.json. Drop-in counterpart to
config-options-reference.js for the JSON-Schema describing the
Handlebars DOM.
*/
function escapeHtml(str)
{
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// Convert a CamelCase / PascalCase type name into a kebab-case
// anchor id. Adjacent uppercase letters that look like an acronym
// (e.g. "TParam", "TArg") stay glued together - the existing
// manually-maintained docs use `tparam-fields` and `targ-fields`,
// not `t-param-fields` / `t-arg-fields`, and we preserve that.
function kebabAnchor(name)
{
return name
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
.toLowerCase();
}
function anchorFor(typeName)
{
return kebabAnchor(typeName) + '-fields';
}
// Strip "#/$defs/X" -> "X".
function refTypeName(ref)
{
const prefix = '#/$defs/';
return ref.startsWith(prefix) ? ref.substring(prefix.length) : ref;
}
// Render the type cell of a property. Returns HTML.
function describeType(prop)
{
if (prop.$ref) {
const t = refTypeName(prop.$ref);
return `<a href="#${anchorFor(t)}"><code>${t}</code></a>`;
}
if (prop.const !== undefined) {
return `<code>"${escapeHtml(String(prop.const))}"</code>`;
}
if (prop.type === 'array') {
return `array of ${describeType(prop.items)}`;
}
if (prop.type === 'string' && Array.isArray(prop.enum)) {
const values = prop.enum.map(v =>
`<code>"${escapeHtml(v)}"</code>`).join(' | ');
return `string (${values})`;
}
if (prop.type === 'object') {
return 'object';
}
return prop.type || 'any';
}
// Render one `$defs` entry: heading + description + members table
// (or a `oneOf` list, for polymorphic unions).
function renderTypeSection(typeName, schema, level, block)
{
block.lines.push(
`<div class="sect${level - 1}">`);
block.lines.push(
`<h${level} id="${anchorFor(typeName)}">`);
block.lines.push(
`<a class="anchor" href="#${anchorFor(typeName)}"></a>`);
block.lines.push(escapeHtml(typeName));
block.lines.push(`</h${level}>`);
block.lines.push(`<div class="sectionbody">`);
if (schema.description) {
block.lines.push(
`<div class="paragraph"><p>${escapeHtml(schema.description)}</p></div>`);
}
if (Array.isArray(schema.oneOf)) {
// Polymorphic union: list each variant as a link.
block.lines.push(`<div class="paragraph"><p>One of:</p></div>`);
block.lines.push(`<div class="ulist"><ul>`);
for (const variant of schema.oneOf) {
const name = refTypeName(variant.$ref);
block.lines.push(
`<li><a href="#${anchorFor(name)}"><code>${escapeHtml(name)}</code></a></li>`);
}
block.lines.push(`</ul></div>`);
} else if (schema.properties) {
// Object type: render the property table.
const required = new Set(schema.required || []);
block.lines.push(
`<table class="tableblock frame-all grid-all stretch">`);
block.lines.push(`<colgroup>`);
block.lines.push(`<col style="width: 25%;">`);
block.lines.push(`<col style="width: 25%;">`);
block.lines.push(`<col style="width: 50%;">`);
block.lines.push(`</colgroup>`);
block.lines.push(`<thead><tr>`);
block.lines.push(
`<th class="tableblock halign-left valign-top">Property</th>`);
block.lines.push(
`<th class="tableblock halign-left valign-top">Type</th>`);
block.lines.push(
`<th class="tableblock halign-left valign-top">Description</th>`);
block.lines.push(`</tr></thead>`);
block.lines.push(`<tbody>`);
for (const [name, prop] of Object.entries(schema.properties)) {
block.lines.push(`<tr>`);
block.lines.push(
`<td class="tableblock halign-left valign-top">`
+ `<code>${escapeHtml(name)}</code>`
+ (required.has(name)
? ` <span style="color: orangered;">(required)</span>`
: '')
+ `</td>`);
block.lines.push(
`<td class="tableblock halign-left valign-top">${describeType(prop)}</td>`);
block.lines.push(
`<td class="tableblock halign-left valign-top">`
+ (prop.description ? escapeHtml(prop.description) : '')
+ `</td>`);
block.lines.push(`</tr>`);
}
block.lines.push(`</tbody>`);
block.lines.push(`</table>`);
}
block.lines.push(`</div>`); // sectionbody
block.lines.push(`</div>`); // sect
}
module.exports = function (registry) {
if (!registry) {
throw new Error('registry must be defined');
}
registry.block('dom-reference', function () {
const self = this;
self.onContext('example');
self.process((parent, reader, attrs) => {
const level = parseInt(attrs.level || 3, 10);
const code = reader.getLines().join('\n');
const schema = JSON.parse(code);
const block = self.$create_pass_block(parent, '', Opal.hash(attrs));
const defs = schema['$defs'] || {};
for (const [typeName, def] of Object.entries(defs)) {
renderTypeSection(typeName, def, level, block);
}
return block;
});
});
};