forked from ionic-team/ionic-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-playground-init.js
More file actions
283 lines (223 loc) · 6.89 KB
/
Copy pathapi-playground-init.js
File metadata and controls
283 lines (223 loc) · 6.89 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
const { api: apiOverrides } = require('./data/meta-override.json');
const utils = require('./utils');
const DEMOS_PATH = path.resolve('static/demos');
let COMPONENT_LINK_REGEXP;
/**
* This script will mass generate all the docs for the Ionic Framework components.
* It will parse the contents of the core.json file and generate a markdown file for each component.
*
* Contents that continue to be autogenerated (props, events, methods, parts, custom props, slots) will
* be written as separate markdown files that that main component file will import.
*
* The component file will not be gitignored and will become the source of truth for README documentation
* and playground usage examples.
*
* The auto generated files will be gitignored and continued to be generated from the published core.json.
* The script to generate those files will be run from the `generate-markdown` command and will be a separate
* script from this script.
*/
(async function () {
const response = await fetch('https://unpkg.com/@ionic/docs/core.json');
const { components } = await response.json();
const names = components.map((component) => component.tag.slice(4));
// matches all relative markdown links to a component, e.g. (../button)
COMPONENT_LINK_REGEXP = new RegExp(`\\(../(${names.join('|')})/?(#[^)]+)?\\)`, 'g');
components.forEach((comp) => {
const compTag = comp.tag.slice(4);
writePage(comp);
writeAutoGeneratedPage(compTag, 'props', renderProperties(comp));
writeAutoGeneratedPage(compTag, 'events', renderEvents(comp));
writeAutoGeneratedPage(compTag, 'methods', renderMethods(comp));
writeAutoGeneratedPage(compTag, 'parts', renderParts(comp));
writeAutoGeneratedPage(compTag, 'custom-props', renderCustomProps(comp));
writeAutoGeneratedPage(compTag, 'slots', renderSlots(comp));
});
console.log('Finished one-time migration of component docs');
})();
function writePage(page) {
let data = [renderFrontmatter(page), renderReadme(page), renderUsage(page), renderComponentApi()].join('');
// fix relative links, e.g. (../button) -> (button.md)
data = data.replace(COMPONENT_LINK_REGEXP, '($1.md$2)');
const dir = './docs/api';
const path = `${dir}/${page.tag.slice(4)}.md`;
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path, data, {});
}
function writeAutoGeneratedPage(componentTag, fileName, data) {
const dir = `./static/auto-generated/${componentTag}`;
const path = `${dir}/${fileName}.md`;
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path, data);
}
function renderFrontmatter({ tag }) {
const frontmatter = {
title: tag,
hide_table_of_contents: true,
};
const demoPath = `api/${tag.slice(4)}/index.html`;
if (fs.existsSync(path.join(DEMOS_PATH, demoPath))) {
frontmatter.demoUrl = `/docs/demos/${demoPath}`;
frontmatter.demoSourceUrl = `https://github.com/ionic-team/ionic-docs/tree/main/static/demos/${demoPath}`;
}
return `---
${Object.entries(frontmatter)
.map(([key, value]) => `${key}: ${typeof value === 'string' ? `"${value.replace('"', '\\"')}"` : value}`)
.join('\n')}
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import TOCInline from '@theme/TOCInline';
import Props from '@site/static/auto-generated/${tag.slice(4)}/props.md';
import Events from '@site/static/auto-generated/${tag.slice(4)}/events.md';
import Methods from '@site/static/auto-generated/${tag.slice(4)}/methods.md';
import Parts from '@site/static/auto-generated/${tag.slice(4)}/parts.md';
import CustomProps from '@site/static/auto-generated/${tag.slice(4)}/custom-props.md';
import Slots from '@site/static/auto-generated/${tag.slice(4)}/slots.md';
${utils.getHeadTag(apiOverrides[tag])}
`;
}
function renderReadme({ readme, encapsulation }) {
const endIndex = readme.indexOf('\n');
const rest = readme.substring(endIndex);
const addAdmonitions = (text) =>
text.replace(/\n\n>/gm, '\n\n:::note').replace(/:::note(.*?)\n(#|\n|^)/gm, ':::note\n$1\n:::\n\n$2');
return `
import EncapsulationPill from '@components/page/api/EncapsulationPill';
${encapsulation !== 'none' ? `<EncapsulationPill type="${encapsulation}" />` : ''}
<h2 className="table-of-contents__title">Contents</h2>
<TOCInline
toc={toc}
maxHeadingLevel={2}
/>
${addAdmonitions(rest)}
`;
}
function renderComponentApi() {
return `
<Props />
<Events />
<Methods />
<Parts />
<CustomProps />
<Slots />`;
}
function renderUsage({ usage }) {
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
const keys = Object.keys(usage);
if (keys.length === 0) {
return '';
}
if (keys.length === 1) {
return `
## Usage
${usage[keys[0]]}
`;
}
return `
## Usage
<Tabs groupId="framework" defaultValue="${keys[0]}" values={[${keys
.map((key) => `{ value: '${key}', label: '${capitalizeFirstLetter(key)}' }`)
.join(', ')}]}>
${Object.entries(usage)
.map(
([key, value]) => `
<TabItem value="${key}">
${value}
</TabItem>
`
)
.join('\n')}
</Tabs>
`;
}
function renderProperties({ props: properties }) {
if (properties.length === 0) {
return '';
}
// NOTE: replaces | with U+FF5C since MDX renders \| in tables incorrectly
return `
## Properties
${properties
.map(
(prop) => `
### ${prop.name}
| | |
| --- | --- |
| **Description** | ${prop.docs.split('\n').join('<br />')} |
| **Attribute** | \`${prop.attr}\` |
| **Type** | \`${prop.type.replace(/\|/g, '\uff5c')}\` |
| **Default** | \`${prop.default}\` |
`
)
.join('\n')}
`;
}
function renderEvents({ events }) {
if (events.length === 0) {
return '';
}
return `
## Events
| Name | Description |
| --- | --- |
${events.map((event) => `| \`${event.event}\` | ${event.docs} |`).join('\n')}
`;
}
function renderMethods({ methods }) {
if (methods.length === 0) {
return '';
}
// NOTE: replaces | with U+FF5C since MDX renders \| in tables incorrectly
return `
## Methods
${methods
.map(
(method) => `
### ${method.name}
| | |
| --- | --- |
| **Description** | ${method.docs.split('\n').join('<br />')} |
| **Signature** | \`${method.signature.replace(/\|/g, '\uff5c')}\` |
`
)
.join('\n')}
`;
}
function renderParts({ parts }) {
if (parts.length === 0) {
return '';
}
return `
## CSS Shadow Parts
| Name | Description |
| --- | --- |
${parts.map((prop) => `| \`${prop.name}\` | ${prop.docs} |`).join('\n')}
`;
}
function renderCustomProps({ styles: customProps }) {
if (customProps.length === 0) {
return '';
}
return `
## CSS Custom Properties
| Name | Description |
| --- | --- |
${customProps.map((prop) => `| \`${prop.name}\` | ${prop.docs} |`).join('\n')}
`;
}
function renderSlots({ slots }) {
if (slots.length === 0) {
return '';
}
return `
## Slots
| Name | Description |
| --- | --- |
${slots.map((slot) => `| \`${slot.name}\` | ${slot.docs} |`).join('\n')}
`;
}