-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarkdown-gen.js
More file actions
137 lines (117 loc) · 4.01 KB
/
markdown-gen.js
File metadata and controls
137 lines (117 loc) · 4.01 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
/* eslint-disable @typescript-eslint/no-var-requires */
const components = require('../../docs/json/components.json');
const fs = require('fs');
const path = require('path');
function containsInternalAnnotation(description) {
return /\@internal/i.test(description);
}
function formatTypeColumn(str, propType) {
if (!str) {
return '';
}
const parts = str.split('|').map((s) => s.trim());
if (parts.length === 1) {
return `\`${str}\``;
}
if (!propType) {
return `\`${parts[0]}\``;
}
return `\`${parts[0]} / ${parts[1]}\``;
}
// Output directory for Markdown files
const outputDir = './docs/markdown';
// Overview page to list elements
let overviewContent = `
{% env enable="reactNativeSdkRef" %}
# React-Native
`;
// Loop through each component in the JSON object
Object.keys(components).forEach((key) => {
overviewContent += `\n## ${
key == 'core'
? 'Skyflow Provider'
: key == 'elements'
? 'Components'
: key.charAt(0).toUpperCase() + key.slice(1)
}\n\n`;
components[key]
.filter((component) => component)
.forEach((component) => {
// Skip generating the Markdown file if the component description contains '@internal'
if (
!component.description.trim() &&
Object.keys(component.props).every((propName) => !component.props[propName].description.trim())
) {
return;
}
// Create the Markdown file path based on the component name
const componentPath = path.join(
outputDir,
key,
`${component.displayName}.md`
);
const name = `${component.displayName}`;
overviewContent += `- [${name}](/sdks/skyflow-react-native/${key}/${name})\n`;
const sortedProps = Object.entries(component.props)
.sort(([_, propA], [__, propB]) => {
if (propA.required && !propB.required) {
return -1; // propA comes before propB
} else if (!propA.required && propB.required) {
return 1; // propB comes before propA
}
return 0; // no change in order
})
.reduce((sorted, [key, value]) => {
sorted[key] = value;
return sorted;
}, {});
// Generate the Markdown content for the component
let markdownContent = `---
id: ${component.displayName}
title: ${component.displayName}
sidebar_label: ${component.displayName}
---
{% env enable="reactNativeSdkRef" %}
# ${component.displayName}
${component.description}
## Import
\`\`\`
import {${component.displayName}} from 'skyflow-react-native';
\`\`\`
`;
const propsDetails = `
## Props
| Name | Type | Description | Required |
|-------------------------|----------------------|---------------------------------------------------------|------------------|
${Object.keys(sortedProps)
.map((propName) => {
const prop = sortedProps[propName];
const isInternal = prop.description && prop.description.includes('@internal');
const description = isInternal ? '' : prop.description;
return description ? `| ${prop.name} | ${formatTypeColumn(
prop.type.name,
prop.required
)} | ${description} | ${prop.required} |` : null;
})
.filter(Boolean)
.join('\n')}
`;
if (Object.keys(component.props).length) {
markdownContent += propsDetails;
}
if (Object.keys(component.tags).length > 0 && component.tags['returns']) {
markdownContent += `\n## Returns\n${component.tags['returns']}\n\n`;
}
markdownContent += '{% /env %}';
const folderPath = path.dirname(componentPath);
// Create the folder if it doesn't exist
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
// Write the Markdown content to the file
fs.writeFileSync(componentPath, markdownContent);
});
});
overviewContent += '\n{% /env %}';
fs.writeFileSync(path.join(outputDir, 'Overview.md'), overviewContent);
console.log('markdown files generated at docs/markdown');