@@ -26,6 +26,7 @@ const internalSubstitutions = {
26
26
} ,
27
27
} ;
28
28
let codeList = [ ] ;
29
+ let openNavTreeScript = '' ;
29
30
30
31
export const callouts = {
31
32
note : {
@@ -777,17 +778,62 @@ async function getDirectoryListing(req) {
777
778
} )
778
779
) ;
779
780
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
+
781
789
if ( filteredFiles [ filteredFiles . length - 1 ] . folders . length > 0 ) {
782
790
r += `</div></div>` ;
783
791
}
784
792
return r ;
785
793
}
786
794
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 ) {
788
832
let html = "" ;
789
833
let lastProcessedFileIndex = - 1 ;
790
834
let lastFile = null ;
835
+ let currentLevel = 0 ;
836
+
791
837
for ( let i = 0 ; i < files . length ; i ++ ) {
792
838
const file = files [ i ] ;
793
839
lastFile = file ;
@@ -800,10 +846,14 @@ async function getDirectoryListingInternal(req, files, folders) {
800
846
html += `</div></div>` ;
801
847
}
802
848
}
803
- // Open as many folders as needed to reach the new folder
849
+ // Insert as many folders as needed to reach the new folder
804
850
for ( let j = diffIndex ; j < file . folderArray . length ; j ++ ) {
805
851
const folder = file . folderArray [ j ] ;
806
852
html += insertDirFolder ( folder , j ) ;
853
+ if ( lookupAndMarkPath ( folder , currentLevel , path . path ) ) {
854
+ openNavTreeScript += `toggleDirList('ff-${ folder } -${ j } ');\n` ;
855
+ currentLevel ++ ;
856
+ }
807
857
}
808
858
folders = file . folderArray ;
809
859
@@ -814,7 +864,7 @@ async function getDirectoryListingInternal(req, files, folders) {
814
864
const subfolderFiles = files . filter (
815
865
( f , index ) => f . folders . startsWith ( folders ) && index > i
816
866
) ;
817
- html += await getDirectoryListingInternal ( req , subfolderFiles , folders ) ;
867
+ html += await getDirectoryListingInternal ( path , req , subfolderFiles , folders ) ;
818
868
819
869
// Update the last processed file index
820
870
lastProcessedFileIndex = i + subfolderFiles . length ;
@@ -851,9 +901,14 @@ function insertDirFolder(folder, j) {
851
901
852
902
function insertDirLink ( file , req , indent , i , files ) {
853
903
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 ) } ${
857
912
file . fileNameWithoutExtension
858
913
} </a>`;
859
914
// Only add <br> if it isn't the last file in the folder
@@ -1099,6 +1154,7 @@ export async function wrapInPage(html, startPage, req) {
1099
1154
) } ');
1100
1155
init();
1101
1156
</script>
1157
+ ${ openNavTreeScript }
1102
1158
</body>
1103
1159
</html>
1104
1160
` ;
0 commit comments