Skip to content

Commit 75bf772

Browse files
Garth PickellCopilot
andcommitted
fix: preserve hash query params for internal docs links
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 30098bb commit 75bf772

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

plugin.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ var __init;
22
__init();
33

44
__init = function() {
5+
function argsFromHash(hash) {
6+
const index = hash.indexOf("?");
7+
if (index >= 0) {
8+
return new URLSearchParams(hash.slice(index + 1));
9+
}
10+
11+
return new URLSearchParams();
12+
}
13+
514
let timer;
615
const list = [];
716
function scrolling(_, delay = 300) {
@@ -47,8 +56,7 @@ __init = function() {
4756
}
4857

4958
function scrollToPart() {
50-
const hash = location.hash.replace(/.*\?/, "");
51-
const args = new URLSearchParams(hash);
59+
const args = argsFromHash(location.hash);
5260
const id = args.get("id") || "_top";
5361
const target = document.getElementById(id);
5462
const scroller = document.querySelector(".content");
@@ -250,18 +258,15 @@ __init = function() {
250258
}
251259

252260
function fixLinks() {
253-
const root = new URL(location.href);
254-
root.hash = "";
255-
261+
const root = "" + new URL("./", location.href);
256262
for (const a of document.querySelectorAll("a")) {
257263
const url = new URL(a.href);
258-
const hash = url.hash.replace(/.*\?/, "");
259-
const args = new URLSearchParams(hash);
260-
url.hash = "";
264+
const args = argsFromHash(url.hash);
265+
url.search = url.hash = "";
261266

262-
if (root.href === url.href) {
267+
if (url.href.startsWith(root)) {
263268
args.delete("id");
264-
269+
265270
if (args.size) {
266271
a.href = "?" + args;
267272
}

0 commit comments

Comments
 (0)