@@ -3,150 +3,151 @@ const path = require('path');
33const remarkAbstract = require ( 'remark' ) ;
44const extractor = require ( '@alifd/api-extractor' ) ;
55const { logger } = require ( '../utils' ) ;
6+
67const remark = remarkAbstract ( ) ;
78const cwd = process . cwd ( ) ;
89
910const blackPropList = [ 'prefix' , 'className' , 'style' , 'locale' ] ;
1011
1112const defaultOrderMap = {
12- size : 0 ,
13- type : 1 ,
14- shape : 2
13+ size : 0 ,
14+ type : 1 ,
15+ shape : 2
1516} ;
1617
1718module . exports = function generateApis ( componentName = 'all' ) {
18- if ( componentName === 'all' ) {
19+ if ( componentName === 'all' ) {
1920
20- const components = fs . readdirSync ( path . join ( cwd , 'docs' ) ) ;
21+ const components = fs . readdirSync ( path . join ( cwd , 'docs' ) ) ;
2122
22- components . forEach ( componentName => {
23- if ( / ^ \. / . test ( componentName ) ) {
24- return ;
25- }
26- generateApi ( componentName ) ;
27- } ) ;
28- } else {
29- generateApi ( componentName ) ;
30- }
23+ components . forEach ( componentName => {
24+ if ( / ^ \. / . test ( componentName ) ) {
25+ return ;
26+ }
27+ generateApi ( componentName ) ;
28+ } ) ;
29+ } else {
30+ generateApi ( componentName ) ;
31+ }
3132
3233} ;
3334
3435function generateApi ( componentName ) {
35- if ( [ 'core' ] . indexOf ( componentName ) > - 1 ) {
36- return false ;
37- }
38-
39- const readmePath = path . join ( cwd , 'docs' , componentName , 'index.md' ) ;
40-
41- if ( ! fs . existsSync ( readmePath ) ) {
42- return logger . error ( `Can not find index.md: ${ readmePath } ` ) ;
43- }
44- const readme = fs . readFileSync ( readmePath , 'utf8' ) ;
45- const ast = remark . parse ( readme ) ;
46- const currentComponentPath = path . join ( cwd , 'src' , componentName ) ;
47-
48- let mainApiInfo = extractor . extract ( currentComponentPath ) ;
36+ if ( [ 'core' ] . indexOf ( componentName ) > - 1 ) {
37+ return false ;
38+ }
4939
50- if ( mainApiInfo ) {
51- let apiInfos = mainApiInfo . subComponents ;
40+ const readmePath = path . join ( cwd , 'docs' , componentName , 'index.md' ) ;
5241
53- apiInfos . forEach ( apiInfo => {
54- apiInfo . name = `${ mainApiInfo . name } .${ apiInfo . name } ` ;
55- } ) ;
56- delete mainApiInfo . subComponents ;
57- apiInfos . unshift ( mainApiInfo ) ;
58-
59- let apiAST = generateAPIAST ( apiInfos , cwd ) ;
60- let apiExtraAST = getAPIExtraAST ( ast ) ;
61- apiAST . children = apiAST . children . concat ( apiExtraAST ) ;
62- updateAST ( ast , apiAST ) ;
63- let readme = remark . stringify ( ast ) . replace ( / \* \s \* \s \* / g, '---' ) ;
64- fs . writeFileSync ( readmePath , readme ) ;
65- } else {
66- logger . warn ( `Can not get API information of ${ currentComponentPath } ` ) ;
67- }
42+ if ( ! fs . existsSync ( readmePath ) ) {
43+ return logger . error ( `Can not find index.md: ${ readmePath } ` ) ;
44+ }
45+ const readme = fs . readFileSync ( readmePath , 'utf8' ) ;
46+ const ast = remark . parse ( readme ) ;
47+ const currentComponentPath = path . join ( cwd , 'src' , componentName ) ;
48+
49+ const mainApiInfo = extractor . extract ( currentComponentPath ) ;
50+
51+ if ( mainApiInfo ) {
52+ const apiInfos = mainApiInfo . subComponents ;
53+
54+ apiInfos . forEach ( apiInfo => {
55+ apiInfo . name = `${ mainApiInfo . name } .${ apiInfo . name } ` ;
56+ } ) ;
57+ delete mainApiInfo . subComponents ;
58+ apiInfos . unshift ( mainApiInfo ) ;
59+
60+ const apiAST = generateAPIAST ( apiInfos , cwd ) ;
61+ const apiExtraAST = getAPIExtraAST ( ast ) ;
62+ apiAST . children = apiAST . children . concat ( apiExtraAST ) ;
63+ updateAST ( ast , apiAST ) ;
64+ const readme = remark . stringify ( ast ) . replace ( / \* \s \* \s \* / g, '---' ) ;
65+ fs . writeFileSync ( readmePath , readme ) ;
66+ } else {
67+ logger . warn ( `Can not get API information of ${ currentComponentPath } ` ) ;
68+ }
6869}
6970
7071function generateAPIAST ( apiInfos ) {
71- const apiDocs = ' ## API\n' + apiInfos . reduce ( ( ret , apiInfo ) => {
72- if ( apiInfo . props ) {
73- let props = filterProps ( apiInfo . props ) ;
74- props = orderProps ( props ) ;
75- ret = `${ ret } \n### ${ apiInfo . name } \n${ apiInfo . description ? '>' + apiInfo . description + '\n' : '' } ${ extractor . generatePropsMD ( props ) } \n` ;
76- }
77- return ret ;
78- } , '' ) ;
79-
80- const apiAST = remark . parse ( apiDocs ) ;
81- apiAST . children = apiAST . children . filter ( child => {
82- return ! ( child . children && child . children [ 0 ] && child . children [ 0 ] . type === 'linkReference' ) ;
83- } ) ;
72+ const apiDocs = ` ## API\n${ apiInfos . reduce ( ( ret , apiInfo ) => {
73+ if ( apiInfo . props ) {
74+ let props = filterProps ( apiInfo . props ) ;
75+ props = orderProps ( props ) ;
76+ ret = `${ ret } \n### ${ apiInfo . name } \n${ apiInfo . description ? `> ${ apiInfo . description } \n` : '' } ${ extractor . generatePropsMD ( props ) } \n` ;
77+ }
78+ return ret ;
79+ } , '' ) } ` ;
80+
81+ const apiAST = remark . parse ( apiDocs ) ;
82+ apiAST . children = apiAST . children . filter ( child => {
83+ return ! ( child . children && child . children [ 0 ] && child . children [ 0 ] . type === 'linkReference' ) ;
84+ } ) ;
8485
85- return apiAST ;
86+ return apiAST ;
8687}
8788
8889function filterProps ( props ) {
89- return Object . keys ( props ) . reduce ( ( ret , name ) => {
90- if ( blackPropList . indexOf ( name ) === - 1 ) {
91- ret [ name ] = props [ name ] ;
92- }
93- return ret ;
94- } , { } ) ;
90+ return Object . keys ( props ) . reduce ( ( ret , name ) => {
91+ if ( blackPropList . indexOf ( name ) === - 1 ) {
92+ ret [ name ] = props [ name ] ;
93+ }
94+ return ret ;
95+ } , { } ) ;
9596}
9697
9798function orderProps ( props ) {
98- const orderMap = Object . assign ( { } , defaultOrderMap ) ;
99- const names = Object . keys ( props ) ;
100- names . forEach ( ( name , index ) => {
101- if ( ! ( name in orderMap ) ) {
102- orderMap [ name ] = index * 10 ;
103- }
104- } ) ;
105- const orderedNames = names . sort ( ( prev , next ) => orderMap [ prev ] - orderMap [ next ] ) ;
99+ const orderMap = Object . assign ( { } , defaultOrderMap ) ;
100+ const names = Object . keys ( props ) ;
101+ names . forEach ( ( name , index ) => {
102+ if ( ! ( name in orderMap ) ) {
103+ orderMap [ name ] = index * 10 ;
104+ }
105+ } ) ;
106+ const orderedNames = names . sort ( ( prev , next ) => orderMap [ prev ] - orderMap [ next ] ) ;
106107
107- return orderedNames . reduce ( ( ret , name ) => {
108- ret [ name ] = props [ name ] ;
109- return ret ;
110- } , { } ) ;
108+ return orderedNames . reduce ( ( ret , name ) => {
109+ ret [ name ] = props [ name ] ;
110+ return ret ;
111+ } , { } ) ;
111112}
112113
113114function getAPIExtraAST ( ast ) {
114- const generateReg = key => new RegExp ( `^<!--\\s*api-extra-${ key } \\s*-->$` , 'i' ) ;
115- const startIndex = ast . children . findIndex ( child =>
116- child . type === 'html' && generateReg ( 'start' ) . test ( child . value ) ) ;
117- if ( startIndex > - 1 ) {
118- const endIndex = ast . children . findIndex ( child =>
119- child . type === 'html' && generateReg ( 'end' ) . test ( child . value ) ) ;
120- if ( endIndex > - 1 && startIndex < endIndex ) {
121- return ast . children . slice ( startIndex , endIndex + 1 ) ;
115+ const generateReg = key => new RegExp ( `^<!--\\s*api-extra-${ key } \\s*-->$` , 'i' ) ;
116+ const startIndex = ast . children . findIndex ( child =>
117+ child . type === 'html' && generateReg ( 'start' ) . test ( child . value ) ) ;
118+ if ( startIndex > - 1 ) {
119+ const endIndex = ast . children . findIndex ( child =>
120+ child . type === 'html' && generateReg ( 'end' ) . test ( child . value ) ) ;
121+ if ( endIndex > - 1 && startIndex < endIndex ) {
122+ return ast . children . slice ( startIndex , endIndex + 1 ) ;
123+ }
122124 }
123- }
124125
125- return [ ] ;
126+ return [ ] ;
126127}
127128
128129function updateAST ( ast , apiAST ) {
129- const apiIndex = ast . children . findIndex ( child =>
130- child . type === 'heading' &&
130+ const apiIndex = ast . children . findIndex ( child =>
131+ child . type === 'heading' &&
131132 child . depth === 2 &&
132133 child . children &&
133134 child . children [ 0 ] &&
134135 child . children [ 0 ] . value === 'API' ) ;
135- if ( apiIndex > - 1 ) {
136- const toNextHeading2 = ast . children . slice ( apiIndex + 1 ) . findIndex ( child =>
137- child . type === 'heading' &&
136+ if ( apiIndex > - 1 ) {
137+ const toNextHeading2 = ast . children . slice ( apiIndex + 1 ) . findIndex ( child =>
138+ child . type === 'heading' &&
138139 child . depth === 2
139- ) + 1 ;
140+ ) + 1 ;
140141
141- if ( toNextHeading2 === 0 ) {
142- ast . children = ast . children . slice ( 0 , apiIndex ) . concat ( apiAST . children ) ;
142+ if ( toNextHeading2 === 0 ) {
143+ ast . children = ast . children . slice ( 0 , apiIndex ) . concat ( apiAST . children ) ;
144+ } else {
145+ ast . children = ast . children . slice ( 0 , apiIndex ) . concat ( apiAST . children , ast . children . slice ( apiIndex + toNextHeading2 ) ) ;
146+ }
143147 } else {
144- ast . children = ast . children . slice ( 0 , apiIndex ) . concat ( apiAST . children , ast . children . slice ( apiIndex + toNextHeading2 ) ) ;
148+ ast . children = ast . children . concat ( apiAST . children ) ;
145149 }
146- } else {
147- ast . children = ast . children . concat ( apiAST . children ) ;
148- }
149150
150- return ast ;
151+ return ast ;
151152}
152153
0 commit comments