Skip to content

Commit 4673b61

Browse files
committed
Update docs build script to have better components hierarchy
1 parent 602b775 commit 4673b61

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

scripts/build-docs.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,28 @@ if (!fs.existsSync(COMPONENTS_DOCS_DIR)) {
1717
fs.mkdirSync(COMPONENTS_DOCS_DIR);
1818
}
1919

20+
const compoundComponents = components.filter(c => c.name.includes('.'));
21+
const parentComponents = _.chain(compoundComponents)
22+
.map(c => c.name.split('.')[0])
23+
.uniq()
24+
.value();
25+
2026
components.forEach(component => {
27+
const [componentName, componentParentName] = getComponentNameParts(component.name);
28+
const isParentComponent = parentComponents.includes(componentName);
29+
30+
let content = '';
31+
/* Markdown Front Matter */
32+
content += `---\n`;
33+
if (isParentComponent) {
34+
content += `sidebar_position: 1\n`;
35+
}
36+
content += `sidebar_label: ${componentName}\n`;
37+
content += `title: ${component.name}\n`;
38+
content += `---\n`;
39+
2140
/* General */
22-
let content = `${component.description} \n`;
41+
content += `${component.description} \n`;
2342
content += `[(code example)](${component.example})\n`;
2443

2544
if (component.extends) {
@@ -51,9 +70,21 @@ components.forEach(component => {
5170
content += `<code>${_.escape(prop.type)}</code>\n\n`;
5271
});
5372

54-
if (!fs.existsSync(`${COMPONENTS_DOCS_DIR}/${component.category}`)) {
55-
fs.mkdirSync(`${COMPONENTS_DOCS_DIR}/${component.category}`);
73+
const componentParentDir = (componentParentName || isParentComponent) ? `/${componentParentName || componentName}` : '';
74+
const dirPath = `${COMPONENTS_DOCS_DIR}/${component.category}${componentParentDir}`;
75+
76+
if (!fs.existsSync(dirPath)) {
77+
fs.mkdirSync(dirPath, {recursive: true});
5678
}
5779

58-
fs.writeFileSync(`${COMPONENTS_DOCS_DIR}/${component.category}/${component.name}.md`, content, {encoding: 'utf8'});
80+
fs.writeFileSync(`${dirPath}/${component.name}.md`, content, {encoding: 'utf8'});
5981
});
82+
83+
function getComponentNameParts(componentName) {
84+
const parts = componentName.split('.');
85+
if (parts.length === 1) {
86+
return [parts[0], undefined];
87+
} else {
88+
return [parts[1], parts[0]];
89+
}
90+
}

0 commit comments

Comments
 (0)