Fix/video sidebar hash links#1054
Conversation
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new partial, video-scripts.html, which generates a mapping of video category URLs to hash fragments and uses JavaScript to append these hashes to matching links. Feedback suggests that the current implementation should also handle direct navigation by checking the current window location on load and applying the appropriate hash if it is missing.
| document.addEventListener('DOMContentLoaded', function () { | ||
| const mapEl = document.getElementById('video-hash-map'); | ||
| const videoHashMap = mapEl ? JSON.parse(mapEl.dataset.map || '{}') : {}; | ||
|
|
||
| const normalize = (href) => { | ||
| try { | ||
| const u = new URL(href, location.origin); | ||
| let p = u.pathname; | ||
| if (!p.endsWith('/')) p += '/'; | ||
| return p; | ||
| } catch (_) { return href || ''; } | ||
| }; | ||
|
|
||
| document.querySelectorAll("a[href^='/videos/'], a[href*='/videos/']").forEach(link => { | ||
| const key = normalize(link.getAttribute('href') || ''); | ||
| const hash = videoHashMap[key]; | ||
| if (hash) link.setAttribute('href', key + '#' + hash); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
The current implementation rewrites links on the page, but it does not address the core issue of direct navigation (e.g., when a user enters a category URL directly into the browser). To fully fix the issue, the script should check the current window.location.pathname on load and update the browser's hash if it matches a known category path. The implementation has been updated to use logical short-circuiting for string validation and defaults to ensure conciseness.
document.addEventListener('DOMContentLoaded', function () {
const mapEl = document.getElementById('video-hash-map');
const videoHashMap = mapEl ? JSON.parse(mapEl.dataset.map || '{}') : {};
const normalize = (href) => {
try {
const u = new URL(href, location.origin);
const p = u.pathname;
return p.endsWith('/') ? p : p + '/';
} catch (_) { return href || ''; }
};
const currentPath = normalize(window.location.pathname);
if (!window.location.hash && videoHashMap[currentPath]) {
window.location.hash = videoHashMap[currentPath];
}
document.querySelectorAll("a[href^='/videos/'], a[href*='/videos/']").forEach(link => {
const key = normalize(link.getAttribute('href') || '');
const hash = videoHashMap[key];
if (hash) link.setAttribute('href', key + '#' + hash);
});
});References
- Use logical short-circuiting to simplify return statements for string validation and trimming to make code more concise and avoid redundant method calls.
Signed-off-by: Sbragul26 <sbragul26@gmail.com>
Signed-off-by: l5io <ci@layer5.io>
Signed-off-by: l5io <ci@layer5.io>
Signed-off-by: l5io <ci@layer5.io>
Signed-off-by: l5io <ci@layer5.io>
Signed-off-by: Discuss bot <discussions@meshery.io>
Signed-off-by: l5io <ci@layer5.io>
Signed-off-by: Sangram Rath <sangram.rath@gmail.com>
Signed-off-by: Sbragul26 <sbragul26@gmail.com>
Signed-off-by: Sbragul26 <sbragul26@gmail.com>
ef22099 to
a1477bd
Compare
|
Closing this PR for now because unrelated commits were included after rebasing. I’ll clean the branch and open a fresh PR with only the required video sidebar hash link fix. |
Notes for Reviewers
This PR fixes #1051 where direct navigation to video category pages always showed the default "Getting Started" tab instead of the correct category.
Signed commits