1
1
import fg from "fast-glob" ;
2
2
3
- const javaScriptIntroduction = { text : "Introduction" , link : "/javascript/introduction" } ;
4
- const javaScriptOptions : Record < string , string > = {
5
- type : "类型和语法" ,
6
- "array-string" : "Array & String" ,
7
- object : "Object" ,
8
- "type-conversion" : "类型转换" ,
9
- prototype : "原型和原型链" ,
10
- this : "this" ,
11
- closure : "闭包" ,
12
- "throttle-debounce" : "节流和防抖" ,
13
- async : "同步和异步" ,
14
- regexp : "正则" ,
15
- "ES5+" : "ES6+" ,
16
- "proxy-reflect" : "代理和反射" ,
17
- "broadcast-channel" : "Broadcast Channel API" ,
18
- "web-speech-api" : "Web Speech API" ,
19
- observer : "Observer API" ,
20
- other : "其他" ,
21
- eventloop : "Event Loop" ,
22
- } ;
23
-
24
- const blogIntroduction = { text : "Introduction" , link : "/blog/introduction" } ;
25
- const cssIntroduction = { text : "Introduction" , link : "/basic/css/introduction" } ;
3
+ const javaScriptIntroduction = { text : "Introduction" , link : "/javascript/introduction" , order : 0 } ;
4
+ const blogIntroduction = { text : "Introduction" , link : "/blog/introduction" , order : 0 } ;
5
+ const cssIntroduction = { text : "Introduction" , link : "/css/introduction" , order : 0 } ;
26
6
27
- const CSSOptions = {
28
- "@font-face" : "@font-face" ,
29
- center : "居中" ,
30
- flex : "flex布局" ,
31
- layout : "常见布局" ,
32
- grid : "grid布局" ,
33
- "text-hidden-overflow" : "文本溢出" ,
34
- bfc : "BFC" ,
35
- position : "层叠与定位" ,
36
- "pseudo-classes-element" : "伪类与伪元素" ,
37
- selectors : "CSS选择器" ,
7
+ const javaScriptOptions : Record < string , { text : string ; order : number } > = {
8
+ type : { text : "类型和语法" , order : 1 } ,
9
+ "array-string" : { text : "Array & String" , order : 2 } ,
10
+ object : { text : "Object" , order : 3 } ,
11
+ "type-conversion" : { text : "类型转换" , order : 4 } ,
12
+ prototype : { text : "原型和原型链" , order : 5 } ,
13
+ this : { text : "this" , order : 6 } ,
14
+ closure : { text : "闭包" , order : 7 } ,
15
+ "throttle-debounce" : { text : "节流和防抖" , order : 8 } ,
16
+ async : { text : "同步和异步" , order : 9 } ,
17
+ regexp : { text : "正则" , order : 10 } ,
18
+ "ES5+" : { text : "ES6+" , order : 11 } ,
19
+ "proxy-reflect" : { text : "代理和反射" , order : 12 } ,
20
+ "broadcast-channel" : { text : "Broadcast Channel API" , order : 13 } ,
21
+ "web-speech-api" : { text : "Web Speech API" , order : 14 } ,
22
+ observer : { text : "Observer API" , order : 15 } ,
23
+ other : { text : "其他" , order : 16 } ,
24
+ eventloop : { text : "Event Loop" , order : 17 } ,
38
25
} ;
39
26
27
+ const CSSOptions : Record < string , { text : string ; order : number } > = {
28
+ center : { text : "居中" , order : 1 } ,
29
+ flex : { text : "flex布局" , order : 2 } ,
30
+ layout : { text : "常见布局" , order : 3 } ,
31
+ grid : { text : "grid布局" , order : 4 } ,
32
+ "@font-face" : { text : "@font-face" , order : 5 } ,
33
+ "text-hidden-overflow" : { text : "文本溢出" , order : 6 } ,
34
+ bfc : { text : "BFC" , order : 7 } ,
35
+ position : { text : "层叠与定位" , order : 8 } ,
36
+ "pseudo-classes-element" : { text : "伪类与伪元素" , order : 9 } ,
37
+ selectors : { text : "CSS选择器" , order : 10 } ,
38
+ } ;
40
39
/**
41
40
* @description : 生成侧边栏
42
41
*/
43
42
const generateSidebar = (
44
43
cwd : string ,
45
- introduction : Record < string , string > ,
46
- options : Record < string , string > ,
44
+ introduction : { text : string ; link : string ; order : number } ,
45
+ options : Record < string , { text : string ; order : number } > ,
47
46
prefix : string
48
47
) => {
49
48
const files = fg . sync ( [ "./*.md" ] , { cwd : cwd , ignore : [ "./introduction.md" ] } ) ;
50
49
const result = files . map ( ( file ) => {
51
50
const path = file . replace ( / ( \/ i n d e x ) ? \. m d $ / , "" ) ;
52
- const text = path . replace ( / \/ $ / , "" ) . split ( "/" ) . pop ( ) ;
53
- return { text : options [ text ] || text , link : `/${ prefix } /${ path } ` } ;
51
+ const key = path . replace ( / \/ $ / , "" ) . split ( "/" ) . pop ( ) ;
52
+ if ( options [ key ] ) {
53
+ return {
54
+ text : options [ key ] . text ,
55
+ link : `/${ prefix } /${ path } ` ,
56
+ order : options [ key ] . order ,
57
+ } ;
58
+ } else {
59
+ console . warn ( `warning: [sidebar.ts/${ prefix } ] ${ key } not in options` ) ;
60
+ }
54
61
} ) ;
55
- return [ introduction , ...result ] ;
62
+ return [ introduction , ...result ] . sort ( ( a , b ) => a . order - b . order ) ;
56
63
} ;
57
64
58
65
export const generateJavaScriptSidebar = ( ) => {
@@ -72,5 +79,5 @@ export const generateBlogSidebar = () => {
72
79
73
80
export const generateCSSSidebar = ( ) => {
74
81
console . log ( "css sidebar generate" ) ;
75
- return generateSidebar ( "./docs/basic/ css" , cssIntroduction , CSSOptions , "css" ) ;
82
+ return generateSidebar ( "./docs/css" , cssIntroduction , CSSOptions , "css" ) ;
76
83
} ;
0 commit comments