11import type { Root } from 'mdast' ;
2+ import type { Nodes } from 'hast' ;
23import type { Transformer } from 'unified' ;
34import type {
45 ExpressionStatement ,
@@ -7,7 +8,7 @@ import type {
78 Property ,
89} from 'estree' ;
910import { createGenerator , type DocEntry , type Generator } from '@/lib/base' ;
10- import { renderMarkdownToHast } from '@/markdown' ;
11+ import { renderMarkdownToHast , renderTypeToHast } from '@/markdown' ;
1112import { valueToEstree } from 'estree-util-value-to-estree' ;
1213import { visit } from 'unist-util-visit' ;
1314import {
@@ -19,16 +20,18 @@ import { dirname } from 'node:path';
1920
2021async function mapProperty (
2122 entry : DocEntry ,
22- renderMarkdown : typeof renderMarkdownToHast ,
23+ {
24+ renderMarkdown = renderMarkdownToHast ,
25+ renderType = renderTypeToHast ,
26+ } : RemarkAutoTypeTableOptions ,
2327) : Promise < Property > {
2428 const value = valueToEstree ( {
25- type : entry . type ,
2629 default : entry . tags . default || entry . tags . defaultValue ,
2730 required : entry . required ,
2831 } ) as ObjectExpression ;
2932
30- if ( entry . description ) {
31- const hast = toEstree ( await renderMarkdown ( entry . description ) , {
33+ function addJsxProperty ( key : string , hast : Nodes ) {
34+ const estree = toEstree ( hast , {
3235 elementAttributeNameCase : 'react' ,
3336 } ) . body [ 0 ] as ExpressionStatement ;
3437
@@ -39,13 +42,20 @@ async function mapProperty(
3942 computed : false ,
4043 key : {
4144 type : 'Identifier' ,
42- name : 'description' ,
45+ name : key ,
4346 } ,
4447 kind : 'init' ,
45- value : hast . expression ,
48+ value : estree . expression ,
4649 } ) ;
4750 }
4851
52+ addJsxProperty ( 'type' , await renderType ( entry . simplifiedType ) ) ;
53+ addJsxProperty ( 'typeDescription' , await renderType ( entry . type ) ) ;
54+
55+ if ( entry . description ) {
56+ addJsxProperty ( 'description' , await renderMarkdown ( entry . description ) ) ;
57+ }
58+
4959 return {
5060 type : 'Property' ,
5161 method : false ,
@@ -72,6 +82,7 @@ export interface RemarkAutoTypeTableOptions {
7282 outputName ?: string ;
7383
7484 renderMarkdown ?: typeof renderMarkdownToHast ;
85+ renderType ?: typeof renderTypeToHast ;
7586
7687 /**
7788 * Customise type table generation
@@ -91,18 +102,20 @@ export interface RemarkAutoTypeTableOptions {
91102 *
92103 * MDX is required to use this plugin.
93104 */
94- export function remarkAutoTypeTable ( {
95- name = 'auto-type-table' ,
96- outputName = 'TypeTable' ,
97- renderMarkdown = renderMarkdownToHast ,
98- options = { } ,
99- remarkStringify = true ,
100- generator = createGenerator ( ) ,
101- } : RemarkAutoTypeTableOptions = { } ) : Transformer < Root , Root > {
105+ export function remarkAutoTypeTable (
106+ config : RemarkAutoTypeTableOptions = { } ,
107+ ) : Transformer < Root , Root > {
108+ const {
109+ name = 'auto-type-table' ,
110+ outputName = 'TypeTable' ,
111+ options : generateOptions = { } ,
112+ remarkStringify = true ,
113+ generator = createGenerator ( ) ,
114+ } = config ;
115+
102116 return async ( tree , file ) => {
103117 const queue : Promise < void > [ ] = [ ] ;
104- let basePath = options ?. basePath ;
105- if ( ! basePath && file . path ) basePath = dirname ( file . path ) ;
118+ const defaultBasePath = file . path ? dirname ( file . path ) : undefined ;
106119
107120 visit ( tree , 'mdxJsxFlowElement' , ( node ) => {
108121 if ( node . name !== name ) return ;
@@ -121,14 +134,14 @@ export function remarkAutoTypeTable({
121134 const output = await generator . generateTypeTable (
122135 props as BaseTypeTableProps ,
123136 {
124- ...options ,
125- basePath,
137+ ...generateOptions ,
138+ basePath : generateOptions . basePath ?? defaultBasePath ,
126139 } ,
127140 ) ;
128141
129142 const rendered = output . map ( async ( doc ) => {
130143 const properties = await Promise . all (
131- doc . entries . map ( ( entry ) => mapProperty ( entry , renderMarkdown ) ) ,
144+ doc . entries . map ( ( entry ) => mapProperty ( entry , config ) ) ,
132145 ) ;
133146
134147 return {
0 commit comments