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

Commit 12ae330

Browse files
committed
Fix: url rewrites with #anchors work now
1 parent 400346a commit 12ae330

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

runestone/common/js/bookfuncs.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,27 @@ window.addEventListener("load", function () {
284284
e.stopPropagation();
285285
});
286286
});
287-
287+
288+
// re-write some urls
289+
// This is tricker than it looks and you have to obey the rules for # anchors
290+
// The #anchors must come after the query string as the server basically ignores any part
291+
// of a url that comes after # - like a comment...
288292
if (location.href.includes("mode=browsing")) {
289293
let queryString = "?mode=browsing";
290294
document.querySelectorAll("a").forEach((link) => {
291-
if (link.href.includes("books/published")) {
295+
console.log(`Starting: ${link.href}`)
296+
let anchorText = "";
297+
if (link.href.includes("books/published") && ! link.href.includes("?mode=browsing")) {
298+
if (link.href.includes("#")) {
299+
let aPoint = link.href.indexOf("#");
300+
anchorText = link.href.substring(aPoint);
301+
link.href = link.href.substring(0,aPoint);
302+
}
292303
link.href = link.href.includes("?")
293-
? link.href + queryString.replace("?", "&")
294-
: link.href + queryString;
304+
? link.href + queryString.replace("?", "&") + anchorText
305+
: link.href + queryString + anchorText;
295306
}
307+
console.log(`Ending: ${link.href}`)
296308
});
297309
}
298310
});

0 commit comments

Comments
 (0)