@@ -3,150 +3,151 @@ const path = require('path');
3
3
const remarkAbstract = require ( 'remark' ) ;
4
4
const extractor = require ( '@alifd/api-extractor' ) ;
5
5
const { logger } = require ( '../utils' ) ;
6
+
6
7
const remark = remarkAbstract ( ) ;
7
8
const cwd = process . cwd ( ) ;
8
9
9
10
const blackPropList = [ 'prefix' , 'className' , 'style' , 'locale' ] ;
10
11
11
12
const defaultOrderMap = {
12
- size : 0 ,
13
- type : 1 ,
14
- shape : 2
13
+ size : 0 ,
14
+ type : 1 ,
15
+ shape : 2
15
16
} ;
16
17
17
18
module . exports = function generateApis ( componentName = 'all' ) {
18
- if ( componentName === 'all' ) {
19
+ if ( componentName === 'all' ) {
19
20
20
- const components = fs . readdirSync ( path . join ( cwd , 'docs' ) ) ;
21
+ const components = fs . readdirSync ( path . join ( cwd , 'docs' ) ) ;
21
22
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
+ }
31
32
32
33
} ;
33
34
34
35
function 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
+ }
49
39
50
- if ( mainApiInfo ) {
51
- let apiInfos = mainApiInfo . subComponents ;
40
+ const readmePath = path . join ( cwd , 'docs' , componentName , 'index.md' ) ;
52
41
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
+ }
68
69
}
69
70
70
71
function 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
+ } ) ;
84
85
85
- return apiAST ;
86
+ return apiAST ;
86
87
}
87
88
88
89
function 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
+ } , { } ) ;
95
96
}
96
97
97
98
function 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 ] ) ;
106
107
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
+ } , { } ) ;
111
112
}
112
113
113
114
function 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
+ }
122
124
}
123
- }
124
125
125
- return [ ] ;
126
+ return [ ] ;
126
127
}
127
128
128
129
function 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' &&
131
132
child . depth === 2 &&
132
133
child . children &&
133
134
child . children [ 0 ] &&
134
135
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' &&
138
139
child . depth === 2
139
- ) + 1 ;
140
+ ) + 1 ;
140
141
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
+ }
143
147
} 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 ) ;
145
149
}
146
- } else {
147
- ast . children = ast . children . concat ( apiAST . children ) ;
148
- }
149
150
150
- return ast ;
151
+ return ast ;
151
152
}
152
153
0 commit comments