From f2ae6de673bb84ef296ef8f1336d92aca38da4ba Mon Sep 17 00:00:00 2001 From: Steven Engler Date: Tue, 17 Sep 2019 13:32:41 -0400 Subject: [PATCH] Removed the "" element This was breaking links, especially images and '#elem_id' fragment URLs. --- layouts/_default/baseof.html | 9 ++++----- static/js/theme-switcher.js | 13 +++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 5d501c8..103acb8 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -5,13 +5,12 @@ - {{ .Hugo.Generator }}{{/* See: https://gohugo.io/themes/creating/#theme-description-file */}} {{ block "title" . }}{{ .Site.Title }}{{ end }} - - + + {{- if .Site.Params.ThemeStyleSwitcher }} - + {{- end }} @@ -30,7 +29,7 @@ {{- end }} diff --git a/static/js/theme-switcher.js b/static/js/theme-switcher.js index c321a55..34d26ee 100644 --- a/static/js/theme-switcher.js +++ b/static/js/theme-switcher.js @@ -1,14 +1,15 @@ // in Firefox we need to add a new css style with document.write rather than modifying the href // of the existing one, otherwise the screen will flash white while loading on dark themes var theme_css_elem = document.getElementById('theme_css'); +var js_url = document.currentScript.src; // example: http://example.com/myhugo/js/theme-switcher.js if(localStorage.getItem('theme') === 'dark'){ var clone = theme_css_elem.cloneNode(false); - clone.href = "css/themes/dark.css"; + clone.href = new URL("../css/themes/dark.css", js_url); theme_css_elem.remove(); document.write(clone.outerHTML); }else if(localStorage.getItem('theme') === 'light'){ var clone = theme_css_elem.cloneNode(false); - clone.href = "css/themes/light.css"; + clone.href = new URL("../css/themes/light.css", js_url); theme_css_elem.remove(); document.write(clone.outerHTML); } @@ -20,19 +21,19 @@ function update_toggle_button(){ var button = document.getElementById('change-theme-button'); button.style.display = ""; if(elem.href.endsWith('light.css')){ - button.getElementsByTagName('img')[0].src = 'images/theme-switcher-moon.svg'; + button.getElementsByTagName('img')[0].src = new URL('../images/theme-switcher-moon.svg', js_url); }else if(elem.href.endsWith('dark.css')){ - button.getElementsByTagName('img')[0].src = 'images/theme-switcher-sun.svg'; + button.getElementsByTagName('img')[0].src = new URL('../images/theme-switcher-sun.svg', js_url); } } function toggle_theme(){ var elem = document.getElementById('theme_css'); if(elem.href.endsWith('light.css')){ - elem.href = "css/themes/dark.css"; + elem.href = new URL("../css/themes/dark.css", js_url); localStorage.setItem('theme', 'dark'); }else if(elem.href.endsWith('dark.css')){ - elem.href = "css/themes/light.css"; + elem.href = new URL("../css/themes/light.css", js_url); localStorage.setItem('theme', 'light'); } update_toggle_button();