@@ -26,7 +26,7 @@ async function loadChildren(path: string, recursive?: boolean) {
2626 const fileTree = document . querySelector ( '#view-file-tree' ) ;
2727 const apiBaseUrl = fileTree . getAttribute ( 'data-api-base-url' ) ;
2828 const refTypeNameSubURL = fileTree . getAttribute ( 'data-current-ref-type-name-sub-url' ) ;
29- const response = await GET ( `${ apiBaseUrl } /tree/${ refTypeNameSubURL } /${ path ?? '' } ?recursive=${ recursive ?? false } ` ) ;
29+ const response = await GET ( `${ apiBaseUrl } /tree/${ refTypeNameSubURL } /${ encodeURIComponent ( path ?? '' ) } ?recursive=${ recursive ?? false } ` ) ;
3030 const json = await response . json ( ) ;
3131 if ( json instanceof Array ) {
3232 return json . map ( ( i ) => ( {
@@ -49,7 +49,7 @@ async function loadContent(sidebarEl: HTMLElement) {
4949}
5050
5151function reloadContentScript ( sidebarEl : HTMLElement , contentEl : Element ) {
52- contentEl . querySelector ( '.show-tree-sidebar-button' ) . addEventListener ( 'click' , ( ) => {
52+ contentEl . querySelector ( '.show-tree-sidebar-button' ) ? .addEventListener ( 'click' , ( ) => {
5353 toggleSidebar ( sidebarEl , true ) ;
5454 } ) ;
5555}
@@ -70,41 +70,26 @@ export function initViewFileTreeSidebar() {
7070 const refName = fileTree . getAttribute ( 'data-current-ref-short-name' ) ;
7171 const refString = ( refType ? ( `/${ refType } ` ) : '' ) + ( refName ? ( `/${ refName } ` ) : '' ) ;
7272
73- const selectedItem = ref ( treePath ) ;
73+ const selectedItem = ref ( getSelectedPath ( refString ) ) ;
7474
7575 const files = await loadChildren ( treePath , true ) ;
7676
7777 fileTree . classList . remove ( 'is-loading' ) ;
7878 const fileTreeView = createApp ( ViewFileTree , { files, selectedItem, loadChildren, loadContent : ( path : string ) => {
79- window . history . pushState ( null , null , `${ baseUrl } /src${ refString } /${ path } ` ) ;
80- selectedItem . value = path ;
79+ selectedItem . value = getSelectedPath ( refString , `${ baseUrl } /src${ refString } /${ path } ` ) ;
80+ window . history . pushState ( null , null , ` ${ baseUrl } /src ${ refString } / ${ encodeURIComponent ( path ) } ` ) ;
8181 loadContent ( el ) ;
8282 } } ) ;
8383 fileTreeView . mount ( fileTree ) ;
8484
8585 window . addEventListener ( 'popstate' , ( ) => {
86- selectedItem . value = extractPath ( window . location . href ) ;
86+ selectedItem . value = getSelectedPath ( refString ) ;
8787 loadContent ( el ) ;
8888 } ) ;
8989 } ) ;
9090}
9191
92- function extractPath ( url : string ) {
93- // Create a URL object
94- const urlObj = new URL ( url ) ;
95-
96- // Get the pathname part
97- const path = urlObj . pathname ;
98-
99- // Define a regular expression to match "/{param1}/{param2}/src/{branch}/{main}/"
100- const regex = / ^ \/ [ ^ / ] + \/ [ ^ / ] + \/ s r c \/ [ ^ / ] + \/ [ ^ / ] + \/ / ;
101-
102- // Use RegExp#exec() method to match the path
103- const match = regex . exec ( path ) ;
104- if ( match ) {
105- return path . substring ( match [ 0 ] . length ) ;
106- }
107-
108- // If the path does not match, return the original path
109- return path ;
92+ function getSelectedPath ( ref : string , url ?: string ) {
93+ const path = url ?? ( new URL ( window . location . href ) . pathname ) ;
94+ return path . substring ( path . indexOf ( ref ) + ref . length + 1 ) ;
11095}
0 commit comments