Skip to content

Commit 1f8a708

Browse files
committed
#4
1 parent c6157a7 commit 1f8a708

2 files changed

Lines changed: 42 additions & 28 deletions

File tree

book/html/theme/rnasir.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@
128128
document.title.includes("Page not found") ||
129129
document.body.textContent.includes("could not be found");
130130

131-
// If not 404, clear redirect history
132131
if (!is404) {
133132
sessionStorage.removeItem("alreadyRedirected");
134133
return;
@@ -145,35 +144,43 @@
145144
const fullPath = window.location.pathname;
146145
const segments = fullPath.split("/").filter(Boolean);
147146

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+
}
151159
const base = "/" + baseSegments.join("/");
152160

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("/");
160166
const newUrl = `${base}/${x}/${z}`;
161-
console.log(`🔁 Redirecting to: ${newUrl}`);
167+
console.log(`🔁 Redirecting to (case 1): ${newUrl}`);
162168
sessionStorage.setItem("alreadyRedirected", "true");
163169
window.location.replace(newUrl + window.location.search + window.location.hash);
164170
return;
165171
}
166172

167-
// Case 2: /Y/X/z.html → /X/z.html
173+
// ✳️ Case 2: /base/Y/X/z.html → /base/X/z.html
168174
if (segments.length >= 3) {
169175
const x = segments[segments.length - 2];
170176
const z = segments[segments.length - 1];
171177
const newUrl = `${base}/${x}/${z}`;
172-
console.log(`🔁 Redirecting to: ${newUrl}`);
178+
console.log(`🔁 Redirecting to (case 2): ${newUrl}`);
173179
sessionStorage.setItem("alreadyRedirected", "true");
174180
window.location.replace(newUrl + window.location.search + window.location.hash);
175181
return;
176182
}
177183

178184
}, 62.5);
179185
})();
186+

theme/rnasir.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@
128128
document.title.includes("Page not found") ||
129129
document.body.textContent.includes("could not be found");
130130

131-
// If not 404, clear redirect history
132131
if (!is404) {
133132
sessionStorage.removeItem("alreadyRedirected");
134133
return;
@@ -145,35 +144,43 @@
145144
const fullPath = window.location.pathname;
146145
const segments = fullPath.split("/").filter(Boolean);
147146

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+
}
151159
const base = "/" + baseSegments.join("/");
152160

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("/");
160166
const newUrl = `${base}/${x}/${z}`;
161-
console.log(`🔁 Redirecting to: ${newUrl}`);
167+
console.log(`🔁 Redirecting to (case 1): ${newUrl}`);
162168
sessionStorage.setItem("alreadyRedirected", "true");
163169
window.location.replace(newUrl + window.location.search + window.location.hash);
164170
return;
165171
}
166172

167-
// Case 2: /Y/X/z.html → /X/z.html
173+
// ✳️ Case 2: /base/Y/X/z.html → /base/X/z.html
168174
if (segments.length >= 3) {
169175
const x = segments[segments.length - 2];
170176
const z = segments[segments.length - 1];
171177
const newUrl = `${base}/${x}/${z}`;
172-
console.log(`🔁 Redirecting to: ${newUrl}`);
178+
console.log(`🔁 Redirecting to (case 2): ${newUrl}`);
173179
sessionStorage.setItem("alreadyRedirected", "true");
174180
window.location.replace(newUrl + window.location.search + window.location.hash);
175181
return;
176182
}
177183

178184
}, 62.5);
179185
})();
186+

0 commit comments

Comments
 (0)