@@ -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,31 +778,62 @@ async function getDirectoryListing(req) {
777
778
} )
778
779
) ;
779
780
const filteredFiles = files . filter ( ( f ) => f !== null ) ;
780
- await getSelectedPage ( req , files ) ;
781
- 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
+
782
789
if ( filteredFiles [ filteredFiles . length - 1 ] . folders . length > 0 ) {
783
790
r += `</div></div>` ;
784
791
}
785
792
return r ;
786
793
}
787
794
788
- async function getSelectedPage ( req , files ) {
789
- // console.log("getSelectedPage", req);
790
- console . log ( "getSelectedPage" , files ) ;
795
+ async function markPathForSelectedPage ( req , files ) {
796
+ // console.log("getSelectedPage", req);
797
+ // console.log("getSelectedPage", files);
791
798
let path = req . _parsedUrl . path ;
792
799
if ( path . startsWith ( "/md/" ) ) {
793
800
path = path . slice ( 4 ) ;
794
801
}
795
802
while ( path . startsWith ( "/" ) ) {
796
803
path = path . slice ( 1 ) ;
797
804
}
798
- console . log ( 'parsedOriginalUrl' , path ) ;
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 ;
799
820
}
800
821
801
- async function getDirectoryListingInternal ( req , files , folders ) {
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 ) {
802
832
let html = "" ;
803
833
let lastProcessedFileIndex = - 1 ;
804
834
let lastFile = null ;
835
+ let currentLevel = 0 ;
836
+
805
837
for ( let i = 0 ; i < files . length ; i ++ ) {
806
838
const file = files [ i ] ;
807
839
lastFile = file ;
@@ -818,6 +850,10 @@ async function getDirectoryListingInternal(req, files, folders) {
818
850
for ( let j = diffIndex ; j < file . folderArray . length ; j ++ ) {
819
851
const folder = file . folderArray [ j ] ;
820
852
html += insertDirFolder ( folder , j ) ;
853
+ if ( lookupAndMarkPath ( folder , currentLevel , path . path ) ) {
854
+ openNavTreeScript += `toggleDirList('ff-${ folder } -${ j } ');\n` ;
855
+ currentLevel ++ ;
856
+ }
821
857
}
822
858
folders = file . folderArray ;
823
859
@@ -828,7 +864,7 @@ async function getDirectoryListingInternal(req, files, folders) {
828
864
const subfolderFiles = files . filter (
829
865
( f , index ) => f . folders . startsWith ( folders ) && index > i
830
866
) ;
831
- html += await getDirectoryListingInternal ( req , subfolderFiles , folders ) ;
867
+ html += await getDirectoryListingInternal ( path , req , subfolderFiles , folders ) ;
832
868
833
869
// Update the last processed file index
834
870
lastProcessedFileIndex = i + subfolderFiles . length ;
@@ -865,9 +901,14 @@ function insertDirFolder(folder, j) {
865
901
866
902
function insertDirLink ( file , req , indent , i , files ) {
867
903
let r = "" ;
868
- r += `<a href="/${ dirPrefix + file . path } " class="${
869
- "/" + file . path === decodeURIComponent ( req . path ) ? "highlight" : ""
870
- } ">${ 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 ) } ${
871
912
file . fileNameWithoutExtension
872
913
} </a>`;
873
914
// Only add <br> if it isn't the last file in the folder
@@ -1113,6 +1154,7 @@ export async function wrapInPage(html, startPage, req) {
1113
1154
) } ');
1114
1155
init();
1115
1156
</script>
1157
+ ${ openNavTreeScript }
1116
1158
</body>
1117
1159
</html>
1118
1160
` ;
0 commit comments