@@ -27,15 +27,32 @@ function getPagePath(relative) {
27
27
return `/${ path . posix . format ( parsed . name === 'index' ? { root : parsed . dir } : { dir : parsed . dir , name : parsed . name } ) } ` ;
28
28
}
29
29
30
+ /**
31
+ * @param fs {import('webpack').LoaderContext<Object>['fs']}
32
+ * @param dir {string}
33
+ * @param root {string}
34
+ * @return {string|null }
35
+ */
36
+ function findSidebar ( fs , dir , root ) {
37
+ while ( dir !== root ) {
38
+ const sidebar = fs . readdirSync ( dir ) . find ( ent => ent . startsWith ( '_sidebar.' ) ) ;
39
+ if ( sidebar ) {
40
+ return path . join ( dir , sidebar ) ;
41
+ }
42
+ dir = path . dirname ( dir ) ;
43
+ }
44
+ return null ;
45
+ }
46
+
30
47
/**
31
48
* @param content {string}
32
49
* @param meta {Object}
33
50
* @param loaderContext {import('webpack').LoaderContext<Object> & { data: { [key: string]: any } | string }}
34
51
*/
35
52
export default function ( content , meta , loaderContext ) {
36
53
const { resourcePath, data, context, rootContext, fs} = loaderContext ;
37
- const sidebarEnt = fs . readdirSync ( path . dirname ( resourcePath ) )
38
- . find ( ent => ent . startsWith ( '_sidebar.' ) ) ;
54
+ const sidebarPath = findSidebar ( fs , path . dirname ( resourcePath ) , rootContext ) ;
55
+ const sidebarContent = sidebarPath && fs . readFileSync ( sidebarPath , 'utf-8' ) . trim ( ) ;
39
56
const processor = remark ( )
40
57
. use ( remarkBootstrapIcon )
41
58
. use ( remarkRehype , { allowDangerousHtml : true } )
@@ -48,8 +65,7 @@ export default function (content, meta, loaderContext) {
48
65
} )
49
66
. use ( rehypeStringify , { allowDangerousCharacters : true , allowDangerousHtml : true } ) ;
50
67
/** @type {VFile|undefined } */
51
- const sidebar = sidebarEnt ? processor
52
- . processSync ( fs . readFileSync ( path . join ( context , sidebarEnt ) ) ) : undefined ;
68
+ const sidebar = sidebarContent && processor . processSync ( sidebarContent ) ;
53
69
Object . assign ( data , meta , sidebar && { sidebar : sidebar . data . title || 'Pages' } ) ;
54
70
return `{{#partial 'sidebar'}}${ sidebar } {{/partial}}{{#> page }}${ content } {{/page}}` ;
55
71
}
0 commit comments