@@ -19,10 +19,27 @@ document.addEventListener('DOMContentLoaded', function() {
1919 // Individual error catching is removed here to be handled by Promise.all
2020 } ;
2121
22- // Determine path prefix based on nesting level
23- const path = window . location . pathname ;
24- const depth = path . split ( '/' ) . length - ( path . endsWith ( '/' ) ? 2 : 1 ) ;
25- const prefix = '../' . repeat ( depth - 1 ) ;
22+ // Determine path prefix based on the current page's location relative to the project root.
23+ function getPathPrefix ( ) {
24+ const path = window . location . pathname ;
25+ const directoryPath = path . substring ( 0 , path . lastIndexOf ( '/' ) + 1 ) ;
26+ const projectRoot = '/SiteExe/' ;
27+ const rootIndex = directoryPath . indexOf ( projectRoot ) ;
28+
29+ // Fallback for local dev or unexpected structure
30+ if ( rootIndex === - 1 ) {
31+ // Remove leading slash to correctly count directories for local file server
32+ const localPath = path . startsWith ( '/' ) ? path . substring ( 1 ) : path ;
33+ const depth = localPath . split ( '/' ) . length - 1 ;
34+ return '../' . repeat ( depth ) ;
35+ }
36+
37+ const relativePath = directoryPath . substring ( rootIndex + projectRoot . length ) ;
38+ const depth = ( relativePath . match ( / \/ / g) || [ ] ) . length ;
39+ return '../' . repeat ( depth ) ;
40+ }
41+
42+ const prefix = getPathPrefix ( ) ;
2643
2744 // Load header and footer in parallel, then perform actions.
2845 Promise . all ( [
@@ -36,36 +53,33 @@ document.addEventListener('DOMContentLoaded', function() {
3653
3754 // Highlight active navigation link
3855 const navLinks = document . querySelectorAll ( '#main-header nav a' ) ;
39- const currentPath = window . location . pathname ;
40- // Get the base href from the <base> tag to correctly resolve paths
41- const baseHref = ( document . querySelector ( 'base[href]' ) ?. getAttribute ( 'href' ) || '/' ) . replace ( / \/ $ / , '' ) ;
56+ const currentPathname = window . location . pathname ;
4257
43- navLinks . forEach ( link => {
44- const linkUrl = new URL ( link . href ) ;
45- let linkPath = linkUrl . pathname ;
58+ let bestMatchLink = null ;
59+ let longestMatchLength = 0 ;
4660
47- // If the link path starts with the base path, remove it for accurate comparison
48- if ( linkPath . startsWith ( baseHref ) ) {
49- linkPath = linkPath . substring ( baseHref . length ) ;
50- }
61+ for ( const link of navLinks ) {
62+ const linkPathname = new URL ( link . href ) . pathname ;
5163
52- // Normalize current path to match the link's structure (e.g. ensure leading slash)
53- let normalizedCurrentPath = currentPath . substring ( baseHref . length ) ;
54- if ( ! normalizedCurrentPath . startsWith ( '/' ) ) {
55- normalizedCurrentPath = '/' + normalizedCurrentPath ;
56- }
64+ // Normalize both paths to treat `page` and `page/` the same for `startsWith` comparison.
65+ const normalizedCurrent = currentPathname . endsWith ( '/' ) ? currentPathname : `${ currentPathname } /` ;
66+ const normalizedLink = linkPathname . endsWith ( '/' ) ? linkPathname : `${ linkPathname } /` ;
5767
58- // Handle the root path case
59- if ( linkPath === '/' && ( normalizedCurrentPath === '/index.html' || normalizedCurrentPath === '/' ) ) {
60- link . classList . add ( 'active' ) ;
61- return ; // Stop further processing for the root link
68+ // Check if the current page is a sub-path of the link's path.
69+ if ( normalizedCurrent . startsWith ( normalizedLink ) ) {
70+ // If this link is a more specific match than the previous best, update it.
71+ if ( normalizedLink . length > longestMatchLength ) {
72+ bestMatchLink = link ;
73+ longestMatchLength = normalizedLink . length ;
74+ }
6275 }
76+ }
6377
64- // Check for other pages, ensuring it doesn't mistakenly match the root
65- if ( linkPath !== '/' && normalizedCurrentPath . startsWith ( linkPath ) ) {
66- link . classList . add ( 'active' ) ;
67- }
68- } ) ;
78+ // If a matching link was found, add the 'active' class.
79+ if ( bestMatchLink ) {
80+ bestMatchLink . classList . add ( 'active' ) ;
81+ }
82+ } ) ;
6983 } ) . catch ( error => {
7084 // Single catch block for any error during component loading
7185 console . error ( `Error loading components:` , error ) ;
0 commit comments