-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathcomponent-docs.cjs
110 lines (88 loc) · 2.39 KB
/
component-docs.cjs
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
#!/usr/bin/env node
const fs = require('fs');
const { writeMDXFile } = require('./utils.cjs');
const { pascalCase } = require('pascal-case');
const {
components,
} = require('@dytesdk/ui-kit/dist/docs/docs-components.json');
/** @typedef {import('@dytesdk/ui-kit/dist/docs/docs-components').JsonDocsComponent} Component */
const basePaths = {
core: 'docs/ui-kit/components',
react: 'docs/react-ui-kit/components',
angular: 'docs/angular-ui-kit/components',
};
/**
*
* @param {Component} component
* @param {'core' | 'react' | 'angular' | 'vue'} framework
* @returns
*/
function generateFile(component, framework = 'core') {
const { tag, docs, props, usage } = component;
function getFrontmatter() {
const path = `/static/ui-kit/1.x.x/components/${tag}.svg`;
if (fs.existsSync(`static${path}`)) {
return ['---', `image: ${path}`, '---'];
}
return [];
}
function getTitle() {
let title = tag;
if (framework === 'react') {
title = pascalCase(tag);
}
return title;
}
function getExample() {
let docName = 'html-example';
if (framework === 'react') {
docName = 'react-example';
} else if (framework === 'angular') {
docName = 'angular-example';
} else if (framework === 'vue') {
docName = 'vue-example';
}
const exampleCode = usage[docName];
return exampleCode ? [exampleCode, ''] : [];
}
const text = [
...getFrontmatter(),
'',
`# ${getTitle()}`,
'',
docs,
'\n',
...getExample(),
...(props.length > 0 ? [`## Props`, `<PropsTable of="${tag}" />`] : []),
'',
]
.filter((line) => line)
.join('\n');
return text;
}
for (const component of components) {
const { tag } = component;
if (tag.startsWith('dyte-breakout') || tag.startsWith('dyte-ai')) {
continue;
}
/** If there is no readme, skip component */
if (
(!component || component.docs.trim() === '') &&
component.tag !== 'dyte-clock'
) {
continue;
}
/** If there is no usage documentation for any framework, skip component */
if (component?.usage && Object.keys(component.usage) === 0) {
continue;
}
writeMDXFile(`${basePaths.core}/${tag}.mdx`, generateFile(component));
writeMDXFile(
`${basePaths.react}/${tag}.mdx`,
generateFile(component, 'react'),
);
writeMDXFile(
`${basePaths.angular}/${tag}.mdx`,
generateFile(component, 'angular'),
);
}