@@ -26,6 +26,7 @@ const internalSubstitutions = {
2626 } ,
2727} ;
2828let codeList = [ ] ;
29+ let openNavTreeScript = '' ;
2930
3031export const callouts = {
3132 note : {
@@ -777,17 +778,62 @@ async function getDirectoryListing(req) {
777778 } )
778779 ) ;
779780 const filteredFiles = files . filter ( ( f ) => f !== null ) ;
780- let r = await getDirectoryListingInternal ( req , filteredFiles , [ ] ) ;
781+ const p = await markPathForSelectedPage ( req , files ) ;
782+
783+ openNavTreeScript = "<script lang=\"javascript\">\n"
784+ openNavTreeScript += `toggleDirList('sidebar-dirlist');\n` ;
785+ let r = await getDirectoryListingInternal ( p , req , filteredFiles , [ ] ) ;
786+ openNavTreeScript += "</script>" ;
787+ // console.log("openNavTreeScript", openNavTreeScript);
788+
781789 if ( filteredFiles [ filteredFiles . length - 1 ] . folders . length > 0 ) {
782790 r += `</div></div>` ;
783791 }
784792 return r ;
785793}
786794
787- async function getDirectoryListingInternal ( req , files , folders ) {
795+ async function markPathForSelectedPage ( req , files ) {
796+ // console.log("getSelectedPage", req);
797+ // console.log("getSelectedPage", files);
798+ let path = req . _parsedUrl . path ;
799+ if ( path . startsWith ( "/md/" ) ) {
800+ path = path . slice ( 4 ) ;
801+ }
802+ while ( path . startsWith ( "/" ) ) {
803+ path = path . slice ( 1 ) ;
804+ }
805+ const r = {
806+ path : [ ] ,
807+ file : null
808+ }
809+ const splitPath = path . split ( "/" ) ;
810+ r . file = splitPath . pop ( ) ;
811+ for ( let i = 0 ; i < splitPath . length ; i ++ ) {
812+ const f = {
813+ dir : splitPath [ i ] ,
814+ level : i
815+ }
816+ r . path . push ( f ) ;
817+ }
818+ // console.log("markPathForSelectedPage", r);
819+ return r ;
820+ }
821+
822+ function lookupAndMarkPath ( folder , level , path ) {
823+ for ( const p of path ) {
824+ if ( p . level === level && p . dir === folder ) {
825+ return true ;
826+ }
827+ }
828+ return false ;
829+ }
830+
831+ async function getDirectoryListingInternal ( path , req , files , folders ) {
788832 let html = "" ;
789833 let lastProcessedFileIndex = - 1 ;
790834 let lastFile = null ;
835+ let currentLevel = 0 ;
836+
791837 for ( let i = 0 ; i < files . length ; i ++ ) {
792838 const file = files [ i ] ;
793839 lastFile = file ;
@@ -800,10 +846,14 @@ async function getDirectoryListingInternal(req, files, folders) {
800846 html += `</div></div>` ;
801847 }
802848 }
803- // Open as many folders as needed to reach the new folder
849+ // Insert as many folders as needed to reach the new folder
804850 for ( let j = diffIndex ; j < file . folderArray . length ; j ++ ) {
805851 const folder = file . folderArray [ j ] ;
806852 html += insertDirFolder ( folder , j ) ;
853+ if ( lookupAndMarkPath ( folder , currentLevel , path . path ) ) {
854+ openNavTreeScript += `toggleDirList('ff-${ folder } -${ j } ');\n` ;
855+ currentLevel ++ ;
856+ }
807857 }
808858 folders = file . folderArray ;
809859
@@ -814,7 +864,7 @@ async function getDirectoryListingInternal(req, files, folders) {
814864 const subfolderFiles = files . filter (
815865 ( f , index ) => f . folders . startsWith ( folders ) && index > i
816866 ) ;
817- html += await getDirectoryListingInternal ( req , subfolderFiles , folders ) ;
867+ html += await getDirectoryListingInternal ( path , req , subfolderFiles , folders ) ;
818868
819869 // Update the last processed file index
820870 lastProcessedFileIndex = i + subfolderFiles . length ;
@@ -851,9 +901,14 @@ function insertDirFolder(folder, j) {
851901
852902function insertDirLink ( file , req , indent , i , files ) {
853903 let r = "" ;
854- r += `<a href="/${ dirPrefix + file . path } " class="${
855- "/" + file . path === decodeURIComponent ( req . path ) ? "highlight" : ""
856- } ">${ indentStringFor ( file . lastFolder === "" ? 0 : indent ) } ${
904+ let correctedPath = decodeURIComponent ( req . path ) ;
905+ if ( correctedPath . startsWith ( "/md/" ) ) {
906+ correctedPath = correctedPath . slice ( 4 ) ;
907+ }
908+ let highlight = "" ;
909+ if ( file . path === correctedPath )
910+ highlight = "highlight" ;
911+ r += `<a href="/${ dirPrefix + file . path } " class="${ highlight } ">${ indentStringFor ( file . lastFolder === "" ? 0 : indent ) } ${
857912 file . fileNameWithoutExtension
858913 } </a>`;
859914 // Only add <br> if it isn't the last file in the folder
@@ -1099,6 +1154,7 @@ export async function wrapInPage(html, startPage, req) {
10991154 ) } ');
11001155 init();
11011156 </script>
1157+ ${ openNavTreeScript }
11021158 </body>
11031159 </html>
11041160 ` ;
0 commit comments