Skip to content

Commit a16a463

Browse files
authored
Merge pull request #72 from SjnExe/fix-header-pathing-logic
fix(js): Correct header loading and nav highlighting logic
2 parents 2119f1b + 5784221 commit a16a463

File tree

2 files changed

+71
-28
lines changed

2 files changed

+71
-28
lines changed

js/main.js

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from playwright.sync_api import sync_playwright
2+
import sys
3+
4+
def run():
5+
with sync_playwright() as p:
6+
browser = p.chromium.launch()
7+
page = browser.new_page()
8+
9+
# Listen for console errors
10+
page.on("console", lambda msg: print(f"Browser console error: {msg.text}", file=sys.stderr))
11+
12+
# Navigate to the local server URL
13+
page.goto('http://localhost:8000/projects/minecraft-nbt-editor/')
14+
15+
# Wait for the header to be loaded
16+
try:
17+
page.wait_for_selector('#main-header nav', timeout=10000) # Reduced timeout
18+
# Take a screenshot of the header area
19+
header_element = page.query_selector('#main-header')
20+
header_element.screenshot(path='jules-scratch/verification/header-verification.png')
21+
print("Verification successful, screenshot taken.")
22+
except Exception as e:
23+
print(f"Verification failed: {e}", file=sys.stderr)
24+
sys.exit(1) # Exit with an error code
25+
26+
browser.close()
27+
28+
if __name__ == '__main__':
29+
run()

0 commit comments

Comments
 (0)