feat: Add preference to make citation links go straight to cited docs (#4886)

* feat: Add preference to make citation links go straight to cited documents

* Use localStorage
This commit is contained in:
Lars Eggert 2022-12-19 21:30:08 +02:00 committed by GitHub
parent 30f852179c
commit 4b1c10f6ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 1 deletions

View file

@ -48,7 +48,7 @@ document.addEventListener("DOMContentLoaded", function (event) {
["py-0"]);
// activate pref buttons selected by pref cookies or localStorage
const in_localStorage = ["deftab"];
const in_localStorage = ["deftab", "reflinks"];
document.querySelectorAll("#pref-tab-pane .btn-check")
.forEach(btn => {
const id = btn.id.replace("-radio", "");
@ -80,4 +80,36 @@ document.addEventListener("DOMContentLoaded", function (event) {
};
defpane.show();
document.activeElement.blur();
if (localStorage.getItem("reflinks") != "refsection") {
// make links to references go directly to the referenced doc
document.querySelectorAll("a[href^='#'].xref")
.forEach(ref => {
const loc = document
.getElementById(ref.hash.substring(1))
.nextElementSibling;
if (!loc ||
loc.tagName != "DD" ||
!loc.closest(".references")) {
return;
}
const url = loc.querySelector(
"a:not([href='']:last-of-type)");
if (url) {
const rfc = url.href.match(/(rfc\d+)$/i);
if (rfc) {
// keep RFC links within the datatracker
const base = ref.href.match(
/^(.*\/)rfc\d+.*$/i);
if (base) {
ref.href = base[1] + rfc[1];
return;
}
}
ref.href = url.href;
}
});
}
});

View file

@ -265,6 +265,17 @@
<i class="bi bi-link-45deg me-1"></i>Reference
</label>
</div>
<label class="form-label fw-bold mt-4 mb-2">Citation links</label>
<div class="btn-group-vertical btn-group-sm d-flex" role="group">
<input type="radio" class="btn-check" name="reflinks" id="refsection-radio">
<label class="btn btn-outline-primary" for="refsection-radio" title="Citation links go to the reference section.">
<i class="bi bi-arrow-clockwise"></i> Go to reference section
</label>
<input type="radio" class="btn-check" name="reflinks" id="citation-radio">
<label class="btn btn-outline-primary" for="citation-radio" title="Citation links go directly to the cited document.">
<i class="bi bi-link-45deg me-1"></i>Go to linked document
</label>
</div>
</div>
</div>
</div>