|
1 | 1 | // Keep anchor #links on the same page, when they would otherwise redirect to |
2 | 2 | // the page's <base href>. Ref: https://stackoverflow.com/q/8108836 |
3 | 3 | (function() { |
4 | | - function fixLink(el) { |
| 4 | + function anchorHrefHash(el) { |
5 | 5 | if (el.tagName.toLowerCase() === "a") { |
6 | 6 | var href = el.getAttribute("href"); |
7 | 7 | if (href && href.indexOf("#") === 0) { |
8 | | - el.href = location.pathname + el.getAttribute("href"); |
| 8 | + return el.getAttribute("href"); |
9 | 9 | } |
10 | 10 | } |
11 | 11 | } |
| 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 | + |
12 | 21 | // Adjust href for all existing links. |
13 | 22 | document.addEventListener("DOMContentLoaded", function() { |
14 | 23 | const es = document.getElementsByTagName("a"); |
15 | 24 | for (var i = 0; i < es.length; i++) { |
16 | 25 | fixLink(es[i]); |
17 | 26 | } |
18 | 27 | }); |
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 | + }); |
21 | 41 | })(); |
22 | 42 |
|
23 | 43 | function mermaidClick(nodeId) { |
|
0 commit comments