|
128 | 128 | document.title.includes("Page not found") || |
129 | 129 | document.body.textContent.includes("could not be found"); |
130 | 130 |
|
131 | | - // If not 404, clear redirect history |
132 | 131 | if (!is404) { |
133 | 132 | sessionStorage.removeItem("alreadyRedirected"); |
134 | 133 | return; |
|
145 | 144 | const fullPath = window.location.pathname; |
146 | 145 | const segments = fullPath.split("/").filter(Boolean); |
147 | 146 |
|
148 | | - // Determine base path (everything before first known folder like 'Java-Fundamentals') |
149 | | - // Assumes base ends before the last 3 segments at most |
150 | | - const baseSegments = segments.slice(0, segments.length - 3); |
| 147 | + // 🔍 Detect and preserve base path (e.g. FRC-Programming-Guide) |
| 148 | + // Assume everything before 'course' or other content is base |
| 149 | + let baseSegments = []; |
| 150 | + for (let i = 0; i < segments.length; i++) { |
| 151 | + if ( |
| 152 | + segments[i] === "course" || |
| 153 | + (segments[i + 1] && segments[i + 1].endsWith(".html")) |
| 154 | + ) { |
| 155 | + break; |
| 156 | + } |
| 157 | + baseSegments.push(segments[i]); |
| 158 | + } |
151 | 159 | const base = "/" + baseSegments.join("/"); |
152 | 160 |
|
153 | | - // Case 1: /Y/course/X/z.html → /X/z.html |
154 | | - if ( |
155 | | - segments.length >= 4 && |
156 | | - segments[segments.length - 4] === "course" |
157 | | - ) { |
158 | | - const x = segments[segments.length - 3]; |
159 | | - const z = segments.slice(-2).join("/"); // e.g. [folder, file.html] |
| 161 | + // ✳️ Case 1: /base/Y/course/X/z.html → /base/X/z.html |
| 162 | + const idx = segments.lastIndexOf("course"); |
| 163 | + if (idx !== -1 && segments.length > idx + 2) { |
| 164 | + const x = segments[idx + 1]; |
| 165 | + const z = segments.slice(idx + 2).join("/"); |
160 | 166 | const newUrl = `${base}/${x}/${z}`; |
161 | | - console.log(`🔁 Redirecting to: ${newUrl}`); |
| 167 | + console.log(`🔁 Redirecting to (case 1): ${newUrl}`); |
162 | 168 | sessionStorage.setItem("alreadyRedirected", "true"); |
163 | 169 | window.location.replace(newUrl + window.location.search + window.location.hash); |
164 | 170 | return; |
165 | 171 | } |
166 | 172 |
|
167 | | - // Case 2: /Y/X/z.html → /X/z.html |
| 173 | + // ✳️ Case 2: /base/Y/X/z.html → /base/X/z.html |
168 | 174 | if (segments.length >= 3) { |
169 | 175 | const x = segments[segments.length - 2]; |
170 | 176 | const z = segments[segments.length - 1]; |
171 | 177 | const newUrl = `${base}/${x}/${z}`; |
172 | | - console.log(`🔁 Redirecting to: ${newUrl}`); |
| 178 | + console.log(`🔁 Redirecting to (case 2): ${newUrl}`); |
173 | 179 | sessionStorage.setItem("alreadyRedirected", "true"); |
174 | 180 | window.location.replace(newUrl + window.location.search + window.location.hash); |
175 | 181 | return; |
176 | 182 | } |
177 | 183 |
|
178 | 184 | }, 62.5); |
179 | 185 | })(); |
| 186 | + |
0 commit comments