|
16 | 16 |
|
17 | 17 | // Resolve the MID / UID to the correct page / anchor using the projectMap. |
18 | 18 | function resolveStableUriRedirectUsingProjectMap(anchor) { |
19 | | - for (const [page, nodes] of Object.entries(projectMap)) { |
20 | | - for (const node of nodes) { |
21 | | - if (node['_LINK'].toLowerCase() === anchor.toLowerCase()) { |
22 | | - window.location.replace(page + "#" + node['_LINK']); |
23 | | - return; |
24 | | - } |
| 19 | + for (const [page, nodes] of Object.entries(projectMap)) { |
| 20 | + for (const node of nodes) { |
| 21 | + if (node['_LINK'].toLowerCase() === anchor.toLowerCase()) { |
| 22 | + window.location.replace(page + "#" + node['_LINK']); |
| 23 | + return; |
| 24 | + } |
25 | 25 |
|
26 | | - const nodeHasUID = 'UID' in node && typeof node['UID'] === 'string'; |
27 | | - if (nodeHasUID && node['UID'].toLowerCase() === anchor.toLowerCase()) { |
28 | | - window.location.replace(page + "#" + node['_LINK']); |
29 | | - return; |
30 | | - } |
| 26 | + const nodeHasUID = 'UID' in node && typeof node['UID'] === 'string'; |
| 27 | + if (nodeHasUID && node['UID'].toLowerCase() === anchor.toLowerCase()) { |
| 28 | + window.location.replace(page + "#" + node['_LINK']); |
| 29 | + return; |
| 30 | + } |
31 | 31 |
|
32 | | - const nodeHasMID = 'MID' in node && typeof node['MID'] === 'string'; |
33 | | - if (nodeHasMID && node['MID'].toLowerCase() === anchor.toLowerCase()) { |
34 | | - window.location.replace(page + "#" + node['_LINK']); |
35 | | - return; |
36 | | - } |
37 | | - } |
| 32 | + const nodeHasMID = 'MID' in node && typeof node['MID'] === 'string'; |
| 33 | + if (nodeHasMID && node['MID'].toLowerCase() === anchor.toLowerCase()) { |
| 34 | + window.location.replace(page + "#" + node['_LINK']); |
| 35 | + return; |
| 36 | + } |
38 | 37 | } |
| 38 | + } |
39 | 39 | } |
40 | 40 |
|
41 | 41 | // Dynamically load the projectMap an resolve MID / UID. |
42 | 42 | function loadProjectMapAndResolveStableUriRedirect(anchor) { |
43 | | - |
44 | | - // ProjectMap is loaded, no need to load it again. |
45 | | - if (typeof projectMap !== 'undefined') { |
46 | | - resolveStableUriRedirectUsingProjectMap(anchor); |
47 | | - return; |
48 | | - } |
49 | 43 |
|
50 | | - // Get script URL and derive from it the url of project_map.js |
51 | | - const scriptUrl = new URL(document.getElementById("stable_uri_forwarder").src, window.location.href) |
52 | | - const projectMapUrl = new URL('project_map.js', scriptUrl).href; |
| 44 | + // ProjectMap is loaded, no need to load it again. |
| 45 | + if (typeof projectMap !== 'undefined') { |
| 46 | + resolveStableUriRedirectUsingProjectMap(anchor); |
| 47 | + return; |
| 48 | + } |
53 | 49 |
|
54 | | - // Dynamically load project map and resolve |
55 | | - const script = document.createElement("script"); |
56 | | - script.src = projectMapUrl; |
57 | | - script.onload = () => resolveStableUriRedirectUsingProjectMap(anchor); |
58 | | - script.onerror = () => { |
59 | | - console.error(`Failed to load project map from ${projectMapUrl}`); |
60 | | - }; |
61 | | - document.head.appendChild(script); |
| 50 | + // Get script URL and derive from it the url of project_map.js |
| 51 | + const scriptUrl = new URL(document.getElementById("stable_uri_forwarder").src, window.location |
| 52 | + .href) |
| 53 | + const projectMapUrl = new URL('project_map.js', scriptUrl).href; |
| 54 | + |
| 55 | + // Dynamically load project map and resolve |
| 56 | + const script = document.createElement("script"); |
| 57 | + script.src = projectMapUrl; |
| 58 | + script.onload = () => resolveStableUriRedirectUsingProjectMap(anchor); |
| 59 | + script.onerror = () => { |
| 60 | + console.error(`Failed to load project map from ${projectMapUrl}`); |
| 61 | + }; |
| 62 | + document.head.appendChild(script); |
62 | 63 | } |
63 | 64 |
|
64 | | -function processStableUriRedirect(anchor) |
65 | | -{ |
66 | | - const exportType = document.querySelector('meta[name="strictdoc-export-type"]')?.content; |
| 65 | +function processStableUriRedirect(anchor) { |
| 66 | + const exportType = document.querySelector('meta[name="strictdoc-export-type"]')?.content; |
67 | 67 |
|
68 | | - if (exportType === 'webserver') { |
69 | | - // For the web server, we let the main_router.py dynamically forward UID to node. |
70 | | - window.location.replace("/UID/" + anchor); |
71 | | - } else if (exportType === 'static') { |
72 | | - // For static exports, we use the project_map.js. |
73 | | - loadProjectMapAndResolveStableUriRedirect(anchor) |
74 | | - } |
| 68 | + if (exportType === 'webserver') { |
| 69 | + // For the web server, we let the main_router.py dynamically forward UID to node. |
| 70 | + window.location.replace("/UID/" + anchor); |
| 71 | + } else if (exportType === 'static') { |
| 72 | + // For static exports, we use the project_map.js. |
| 73 | + loadProjectMapAndResolveStableUriRedirect(anchor) |
| 74 | + } |
75 | 75 | } |
76 | 76 |
|
77 | 77 |
|
78 | 78 | // In case an anchor is present at content load time. |
79 | 79 | document.addEventListener("DOMContentLoaded", () => { |
80 | | - const anchorParam = new URLSearchParams(window.location.search).get("a"); |
81 | | - if (anchorParam) { |
82 | | - processStableUriRedirect(anchorParam) |
83 | | - return |
84 | | - } |
| 80 | + const anchorParam = new URLSearchParams(window.location.search).get("a"); |
| 81 | + if (anchorParam) { |
| 82 | + processStableUriRedirect(anchorParam) |
| 83 | + return |
| 84 | + } |
85 | 85 |
|
86 | | - // for legacy link support |
87 | | - const anchorHash = window.location.hash.substring(1); |
88 | | - if (anchorHash) { |
89 | | - processStableUriRedirect(anchorHash) |
90 | | - } |
| 86 | + // for legacy link support |
| 87 | + const anchorHash = window.location.hash.substring(1); |
| 88 | + if (anchorHash) { |
| 89 | + processStableUriRedirect(anchorHash) |
| 90 | + } |
91 | 91 | }); |
92 | 92 |
|
93 | 93 | // In case an anchor is added manually afterwards (eases testing). |
94 | 94 | window.addEventListener("hashchange", () => { |
95 | | - const anchor = window.location.hash.substring(1); |
96 | | - if (anchor) { |
97 | | - processStableUriRedirect(anchor) |
98 | | - } |
| 95 | + const anchor = window.location.hash.substring(1); |
| 96 | + if (anchor) { |
| 97 | + processStableUriRedirect(anchor) |
| 98 | + } |
99 | 99 | }); |
0 commit comments