@@ -27,15 +27,32 @@ function getPagePath(relative) {
2727 return `/${ path . posix . format ( parsed . name === 'index' ? { root : parsed . dir } : { dir : parsed . dir , name : parsed . name } ) } ` ;
2828}
2929
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+
3047/**
3148 * @param content {string}
3249 * @param meta {Object}
3350 * @param loaderContext {import('webpack').LoaderContext<Object> & { data: { [key: string]: any } | string }}
3451 */
3552export default function ( content , meta , loaderContext ) {
3653 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 ( ) ;
3956 const processor = remark ( )
4057 . use ( remarkBootstrapIcon )
4158 . use ( remarkRehype , { allowDangerousHtml : true } )
@@ -48,8 +65,7 @@ export default function (content, meta, loaderContext) {
4865 } )
4966 . use ( rehypeStringify , { allowDangerousCharacters : true , allowDangerousHtml : true } ) ;
5067 /** @type {VFile|undefined } */
51- const sidebar = sidebarEnt ? processor
52- . processSync ( fs . readFileSync ( path . join ( context , sidebarEnt ) ) ) : undefined ;
68+ const sidebar = sidebarContent && processor . processSync ( sidebarContent ) ;
5369 Object . assign ( data , meta , sidebar && { sidebar : sidebar . data . title || 'Pages' } ) ;
5470 return `{{#partial 'sidebar'}}${ sidebar } {{/partial}}{{#> page }}${ content } {{/page}}` ;
5571}
0 commit comments