Skip to content

Commit 1875f6a

Browse files
committed
docs: update api generator
1 parent f5efc48 commit 1875f6a

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

build/api.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env node
2+
/* eslint-disable @typescript-eslint/no-var-requires */
23
'use strict'
34

45
const docgen = require('react-docgen-typescript')
56
const fs = require('fs').promises
67
const path = require('path')
78
const globby = require('globby')
9+
const pkg = require('../package.json')
810

9-
// These are the filetypes we only care about replacing the version
1011
const GLOB = ['src/components/**/*.tsx']
1112
const GLOBBY_OPTIONS = {
1213
cwd: path.join(__dirname, '..'),
@@ -22,20 +23,35 @@ const options = {
2223
async function createMdx(filename, name, props) {
2324
if (typeof props === 'undefined') return
2425

25-
// let content = `### ${name}\n\n| Prop name | Description | Type |\n| --- | --- | --- |\n`
26-
let content = `| Prop name | Description | Type |\n| --- | --- | --- |\n`
26+
let content = `| Property | Description | Type | Default |\n`
27+
content += `| --- | --- | --- | --- |\n`
2728

2829
for (const [key, value] of Object.entries(props).sort()) {
29-
if (value.parent.fileName !== 'react/node_modules/@types/react/index.d.ts') {
30-
content += `| ${value['name']} | ${value['description']} | ${value['type']['name']} |\n`
31-
console.log(`${key}: ${value}`)
30+
if (
31+
value.parent.fileName !== 'react/node_modules/@types/react/index.d.ts' &&
32+
!value['description'].includes('@ignore')
33+
) {
34+
const name = value.name || ''
35+
const description =
36+
value.description.replaceAll('\n', '<br/>').replaceAll(' [docs]', '') || '-'
37+
const type =
38+
value.type ? value.type.name.includes('ReactElement')
39+
? 'ReactElement'
40+
: value.type.name : ''
41+
const defaultValue = value.defaultValue ? value.defaultValue.value : '-'
42+
43+
content += `| **${name}** | ${description} | \`${type}\` | ${defaultValue} |\n`
44+
console.log(`${filename} - ${key}`)
3245
}
3346
}
3447

35-
await fs.writeFile(`api/${filename}.api.mdx`, content, { encoding: 'utf8' }).then(() => {
36-
// Do whatever you want to do.
37-
console.log('Done')
38-
})
48+
await fs
49+
.writeFile(`docs/${pkg.config.version_short}/api/${filename}.api.mdx`, content, {
50+
encoding: 'utf8',
51+
})
52+
.then(() => {
53+
console.log(`File created: ${filename}.api.mdx`)
54+
})
3955
}
4056

4157
async function main() {
@@ -49,8 +65,6 @@ async function main() {
4965
const filename = path.basename(file, '.tsx')
5066
createMdx(filename, props[0].displayName, props[0].props)
5167
}
52-
// const props = docgen.parse(file, options)
53-
// if (props) console.log(props)
5468
}),
5569
)
5670
} catch (error) {

0 commit comments

Comments
 (0)