From c5382e0bcc6d2ca9d3fcbf6a1102ddda1afed626 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6ren=20Weber?= <mail@soeren-weber.de>
Date: Thu, 9 Sep 2021 21:03:37 +0200
Subject: [PATCH] theme: fix showVisitedLinks in case Hugo is configured to
 modify relative URLs #86

---
 layouts/partials/menu.html | 2 +-
 static/js/relearn.js       | 7 ++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/layouts/partials/menu.html b/layouts/partials/menu.html
index a1f9828266..e0fdd153d3 100644
--- a/layouts/partials/menu.html
+++ b/layouts/partials/menu.html
@@ -135,7 +135,7 @@
       </li>
     {{else}}
       <li data-nav-id="{{.RelPermalink}}" title="{{.Title}}" class="dd-item{{if eq .File.UniqueID $currentFileUniqueID}} active{{end}}">
-        <a href="{{ .RelPermalink}}">
+        <a href="{{.RelPermalink}}">
           {{ partial "menu-pre.html" . }}{{or .Params.menuTitle .LinkTitle .Title}}{{ partial "menu-post.html" . }}
           {{ if $showvisitedlinks}}<i class="fas fa-check read-icon"></i>{{end}}
         </a>
diff --git a/static/js/relearn.js b/static/js/relearn.js
index 55b2ea46cc..2b725491df 100644
--- a/static/js/relearn.js
+++ b/static/js/relearn.js
@@ -527,7 +527,12 @@ jQuery(function() {
 
     // loop through the sessionStorage and see if something should be marked as visited
     for (var url in sessionStorage) {
-        if (sessionStorage.getItem(url) == 1) jQuery('[data-nav-id="' + url + '"]').addClass('visited');
+        if (sessionStorage.getItem(url) == 1){
+            // in case we have `relativeURLs=true` we have to strip the
+            // relative path to root
+            url = url.replace( /\.\.\//g, '/' ).replace( /^\/+\//, '/' );
+            jQuery('[data-nav-id="' + url + '"]').addClass('visited');
+        }
     }
 });