From ebe2b109efe029dce0e14efb119d39041d97a895 Mon Sep 17 00:00:00 2001
From: bb1950328 <bb1950328@gmail.com>
Date: Sat, 30 Jan 2021 12:47:47 +0000
Subject: [PATCH] fixed some URL operations which don't work when hugos absolute root url is something like https://www.foo.bar (no slash at the end)

---
 assets/js/search.js    |    2 +-
 assets/js/index.js     |    2 +-
 assets/js/variables.js |    2 +-
 assets/js/functions.js |   11 ++++-------
 4 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/assets/js/functions.js b/assets/js/functions.js
index d5bba50..dc12792 100644
--- a/assets/js/functions.js
+++ b/assets/js/functions.js
@@ -91,10 +91,9 @@
 }
 
 function closestInt(goal, collection) {
-  const closest = collection.reduce(function(prev, curr) {
+  return collection.reduce(function (prev, curr) {
     return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
   });
-  return closest;
 }
 
 function hasClasses(el) {
@@ -147,14 +146,12 @@
   const hyperLinks = elems('a');
   if(hyperLinks) {
     hyperLinks.forEach(function(link){
-      const href = link.href.replaceAll(encodeURI(open), "").replaceAll(encodeURI(close), "");
-      link.href = href;
+      link.href = link.href.replaceAll(encodeURI(open), "").replaceAll(encodeURI(close), "");
     });
   }
 }
 
 function parseBoolean(string) {
-  let bool;
   string = string.trim().toLowerCase();
   switch (string) {
     case 'true':
@@ -164,10 +161,10 @@
     default:
       return undefined;
   }
-};
+}
 
 function loadSvg(file, parent, path = 'icons/') {
-  const link = `${parentURL}${path}${file}.svg`;
+  const link = new URL(`${path}${file}.svg`, rootURL).href;
   fetch(link)
   .then((response) => {
     return response.text();
diff --git a/assets/js/index.js b/assets/js/index.js
index 8614d98..65cc1a9 100644
--- a/assets/js/index.js
+++ b/assets/js/index.js
@@ -132,7 +132,7 @@
       Array.from(links).forEach(function(link, index){
         let target, rel, blank, noopener, attr1, attr2, url, isExternal;
         url = elemAttribute(link, 'href');
-        isExternal = (url && typeof url == 'string' && url.startsWith('http')) && !url.startsWith(parentURL) && link.closest(contentWrapperClass);
+        isExternal = (url && typeof url == 'string' && url.startsWith('http')) && !url.startsWith(rootURL) && link.closest(contentWrapperClass);
         if(isExternal) {
           target = 'target';
           rel = 'rel';
diff --git a/assets/js/search.js b/assets/js/search.js
index 2c4851a..fc51a0e 100644
--- a/assets/js/search.js
+++ b/assets/js/search.js
@@ -124,7 +124,7 @@
       searchField.addEventListener('search', function(){
         const searchTerm = searchField.value.trim().toLowerCase();
         if(searchTerm.length)  {
-          window.location.href = `${parentURL}search/?query=${searchTerm}`;
+          window.location.href = new URL(`search/?query=${searchTerm}`, rootURL).href;
         }
       });
     }
diff --git a/assets/js/variables.js b/assets/js/variables.js
index c351059..4b450d5 100644
--- a/assets/js/variables.js
+++ b/assets/js/variables.js
@@ -5,7 +5,7 @@
 const menu = 'menu';
 
 // defined in config.toml
-const parentURL = '{{ absURL "" }}';
+const rootURL = '{{ absURL "" }}';
 
 // defined in i18n / translation files
 const quickLinks = '{{ T "quick_links" }}';

--
Gitblit v1.10.0