From c767bad38694333a41687737392f7ec6c50a2779 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Mon, 26 Jul 2021 22:29:24 +0200 Subject: [PATCH] Make anchor links base uri independent Signed-off-by: Tilman Vatteroth --- public/js/extra.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/public/js/extra.js b/public/js/extra.js index e15b255a5..6e3b0ed0e 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -575,6 +575,14 @@ export function postProcess (code) { // also add noopener to prevent clickjacking // See details: https://mathiasbynens.github.io/rel-noopener/ result.find('a:not([href^="#"]):not([target])').attr('target', '_blank').attr('rel', 'noopener') + + // If it's hashtag link then make it base uri independent + result.find('a[href^="#"]').each((index, linkTag) => { + const currentLocation = new URL(window.location) + currentLocation.hash = linkTag.hash + linkTag.href = currentLocation.toString() + }) + // update continue line numbers const linenumberdivs = result.find('.gutter.linenumber').toArray() for (let i = 0; i < linenumberdivs.length; i++) {