@@ -31,43 +31,99 @@ ${content}`)
31
31
console . log ( `Writed: ${ indexFilepath } ` )
32
32
}
33
33
34
- const debugHandle = _debug ( 'update:meta:handle' )
34
+ const debugHandle = _debug ( 'update:meta:handle' ) ;
35
+
36
+ const symlinkMap = { } ;
37
+
38
+ async function handleSymlinks ( nodes ) {
39
+ await pMap ( nodes , async ( node ) => {
40
+ if ( node . isDir ) {
41
+ return handleSymlinks ( node . children ) ;
42
+ }
43
+ if ( ! node . isSymbolicLink ) { return ; }
44
+
45
+ symlinkMap [ node . path ] = true ;
46
+
47
+ // https://just-the-docs.com/docs/navigation-structure/
48
+ const ymlFrontRaw = [
49
+ '---' ,
50
+ 'layout: default' ,
51
+ 'nav_exclude: true' , // symlink document should hide in nav_bar
52
+ ]
53
+
54
+ if ( node . parent ) {
55
+ ymlFrontRaw . push ( `parent: ${ node . parent . title } ` ) ;
56
+ if ( node . parent . parent ) {
57
+ ymlFrontRaw . push ( `grand_parent: ${ node . parent . parent . title } ` ) ;
58
+ }
59
+ }
60
+
61
+ ymlFrontRaw . push ( '---' , '' , '' ) ;
62
+
63
+ let fh = await FSP . open ( node . path , 'r+' ) ; // why readfile with "w+" return empty?
64
+ const buffer = await fh . readFile ( ) ;
65
+ await fh . close ( )
66
+ await FSP . unlink ( node . path ) ;
67
+ fh = await FSP . open ( node . path , 'w+' ) ;
68
+
69
+ try {
70
+ const stream = fh . createWriteStream ( { start : 0 } ) ;
71
+ await new Promise ( ( resolve ) => {
72
+ stream . write ( ymlFrontRaw . join ( '\n' ) , 'utf8' , resolve ) ;
73
+ } )
74
+ await new Promise ( ( resolve ) => {
75
+ stream . write ( buffer , 'utf8' , resolve ) ;
76
+ } )
77
+ } finally {
78
+ await fh . close ( ) ;
79
+ }
80
+
81
+ console . log ( `Added YAML front: ${ node . path } ` )
82
+ } )
83
+ }
35
84
36
85
async function handle ( nodes ) {
37
86
debugHandle ( 'nodes=%O' , nodes )
38
87
39
88
await pMap ( nodes , async ( node ) => {
40
89
if ( node . isDir ) {
41
90
await createIndexFileForJTD ( node )
42
- } else {
43
- const fh = await FSP . open ( node . path , 'r+' ) ; // why readfile with "w+" return empty?
91
+ return handle ( node . children ) ;
92
+ }
44
93
45
- // https://just-the-docs.com/docs/navigation-structure/
46
- const ymlFront = `---
47
- layout: default
48
- ${ node . parent ? `parent: ${ node . parent . title } ` : '' }
49
- ${ node ?. parent . parent ? `grand_parent: ${ node . parent . parent . title } ` : '' }
50
- ---
94
+ if ( symlinkMap [ node . path ] ) { return ; }
51
95
52
- ` ;
96
+ // https://just-the-docs.com/docs/navigation-structure/
97
+ const ymlFrontRaw = [
98
+ '---' ,
99
+ 'layout: default' ,
100
+ ]
101
+
102
+ if ( node . parent ) {
103
+ ymlFrontRaw . push ( `parent: ${ node . parent . title } ` ) ;
104
+ if ( node . parent . parent ) {
105
+ ymlFrontRaw . push ( `grand_parent: ${ node . parent . parent . title } ` ) ;
106
+ }
107
+ }
53
108
54
- try {
55
- const buffer = await fh . readFile ( ) ;
56
- const stream = fh . createWriteStream ( { start : 0 } ) ;
57
- await new Promise ( ( resolve ) => {
58
- stream . write ( ymlFront , 'utf8' , resolve ) ;
59
- } )
60
- await new Promise ( ( resolve ) => {
61
- stream . write ( buffer , 'utf8' , resolve ) ;
62
- } )
63
- } finally {
64
- await fh . close ( ) ;
65
- }
66
-
67
- console . log ( `Added YAML front: ${ node . path } ` )
109
+ ymlFrontRaw . push ( '---' , '' , '' ) ;
110
+
111
+ const fh = await FSP . open ( node . path , 'r+' ) ; // why readfile with "w+" return empty?
112
+
113
+ try {
114
+ const buffer = await fh . readFile ( ) ;
115
+ const stream = fh . createWriteStream ( { start : 0 } ) ;
116
+ await new Promise ( ( resolve ) => {
117
+ stream . write ( ymlFrontRaw . join ( '\n' ) , 'utf8' , resolve ) ;
118
+ } )
119
+ await new Promise ( ( resolve ) => {
120
+ stream . write ( buffer , 'utf8' , resolve ) ;
121
+ } )
122
+ } finally {
123
+ await fh . close ( ) ;
68
124
}
69
125
70
- return handle ( node . children ) ;
126
+ console . log ( `Added YAML front: ${ node . path } ` )
71
127
} )
72
128
}
73
129
@@ -107,6 +163,7 @@ run(async () => {
107
163
debug ( 'nodeMap=%O' , nodeMap ) ;
108
164
debug ( 'rootNodes=%O' , rootNodes ) ;
109
165
166
+ await handleSymlinks ( rootNodes ) ;
110
167
await handle ( rootNodes ) ;
111
168
await updateREADME ( ) ;
112
169
0 commit comments