Repository navigation
Expand file tree
/
Copy pathPropsTable.astro
More file actions
97 lines (88 loc) · 2.51 KB
/
Copy pathPropsTable.astro
File metadata and controls
97 lines (88 loc) · 2.51 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
---
import { resolve } from 'node:path'
import {
type ComponentDoc,
withCustomConfig
} from 'react-docgen-typescript'
interface Props {
component: string
}
// The build runs in the website folder: the sources of the package are one level up
const root = resolve(process.cwd(), '..')
let docs: ComponentDoc[] | undefined
/**
* Props of the components, read from the sources of the package once per build.
* @returns Docs of every exported component.
*/
function getDocs() {
docs ??= withCustomConfig(resolve(root, 'tsconfig.json'), {
savePropValueAsString: true,
// Standard <canvas> props are mentioned once on the page instead of being listed
propFilter: prop => !prop.parent || !prop.parent.fileName.includes('node_modules')
}).parse([
resolve(root, 'src/chart.ts'),
resolve(root, 'src/typedCharts.ts')
])
return docs
}
const { component } = Astro.props
const doc = getDocs().find(({ displayName }) => displayName === component)
if (!doc) {
throw new Error(`No props found for the ${component} component`)
}
/**
* The type as the docs show it: an optional prop takes `undefined` anyway,
* and Chart.js options are named as in Chart.js instead of being expanded.
* @param type - The type as TypeScript prints it.
* @returns The readable type.
*/
function formatType(type: string) {
return type
.replace(/ \| undefined$/, '')
.replace(/ChartJSOrUndefined<([^>]+)>/, 'Chart<$1>')
.replace(/^_DeepPartialObject<CoreChartOptions<([^>]+)>[\s\S]*$/, 'ChartOptions<$1>')
}
const props = Object.values(doc.props).map(({
name,
type,
defaultValue,
required,
description
}) => ({
name,
type: formatType(type.name),
defaultValue: defaultValue?.value as string | undefined,
required,
description: description.replace(/\n@[\s\S]*/, ''),
see: /@see (\S+)/.exec(description)?.[1]
}))
---
<dl class='rc2-props'>
{props.map(prop => (
<div class='rc2-prop'>
<dt>
<code>{prop.name}</code>
{prop.required && <span class='rc2-prop-required'>required</span>}
</dt>
<dd>
<p>
{prop.description}
{prop.see && (
<>
{' '}
<a href={prop.see} target='_blank' rel='noopener noreferrer'>See more</a>
</>
)}
</p>
<p class='rc2-prop-type'>
Type: <code>{prop.type}</code>
{prop.defaultValue && (
<>
{' · '}Default: <code>{prop.defaultValue}</code>
</>
)}
</p>
</dd>
</div>
))}
</dl>