Skip to content
This repository was archived by the owner on Aug 17, 2023. It is now read-only.

Commit a286ae9

Browse files
committed
docs: Improve hash links workaround script
1 parent 0bd9f12 commit a286ae9

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

docs/static/adrestia.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
// Keep anchor #links on the same page, when they would otherwise redirect to
22
// the page's <base href>. Ref: https://stackoverflow.com/q/8108836
33
(function() {
4-
function fixLink(el) {
4+
function anchorHrefHash(el) {
55
if (el.tagName.toLowerCase() === "a") {
66
var href = el.getAttribute("href");
77
if (href && href.indexOf("#") === 0) {
8-
el.href = location.pathname + el.getAttribute("href");
8+
return el.getAttribute("href");
99
}
1010
}
1111
}
12+
13+
function fixLink(el) {
14+
var hash = anchorHrefHash(el);
15+
if (hash) {
16+
el.href = location.pathname + hash;
17+
}
18+
return el;
19+
}
20+
1221
// Adjust href for all existing links.
1322
document.addEventListener("DOMContentLoaded", function() {
1423
const es = document.getElementsByTagName("a");
1524
for (var i = 0; i < es.length; i++) {
1625
fixLink(es[i]);
1726
}
1827
});
19-
// Adjust href for dynamically added links - when they are clicked.
20-
document.addEventListener("click", function(ev) { fixLink(ev.target); });
28+
29+
document.addEventListener("click", function(ev) {
30+
// Adjust href for dynamically added links - when they are clicked.
31+
var el = fixLink(ev.target);
32+
// Special case for ema live server: handle the click
33+
if (el && !!window.connected) {
34+
if (el.hash && el.pathname === window.location.pathname) {
35+
window.location.hash = el.hash;
36+
ev.stopPropagation();
37+
ev.preventDefault();
38+
}
39+
}
40+
});
2141
})();
2242

2343
function mermaidClick(nodeId) {

0 commit comments

Comments
 (0)