From 425cbb57811c81a0871013bb66e5b6e92f6a89f4 Mon Sep 17 00:00:00 2001 From: zzossig Date: Thu, 6 Feb 2020 03:52:41 +0900 Subject: [PATCH] search index rollback --- layouts/partials/head/scripts.html | 51 +++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/layouts/partials/head/scripts.html b/layouts/partials/head/scripts.html index e2dea4f..6b9c326 100644 --- a/layouts/partials/head/scripts.html +++ b/layouts/partials/head/scripts.html @@ -78,9 +78,6 @@ } // =============================================================== - {{ $permalink := .Permalink }} - var permalink = JSON.parse({{ $permalink | jsonify }}); - document.addEventListener('DOMContentLoaded', function () { // ============================ tab ============================ document.querySelectorAll('.tab') ? @@ -554,6 +551,44 @@ // ========================== search ========================== + var baseurl = null; + {{ $siteBaseURL:= .Site.BaseURL }} + var siteBaseURL = JSON.parse({{ $siteBaseURL | jsonify }}); + var siteBaseChecker = /\/\w+\//i; + var isSlug = siteBaseChecker.test(siteBaseURL); + var isThemeSite = location.origin.includes('themes.gohugo.io'); + + {{ if .Site.IsMultiLingual }} + if (isThemeSite) { + baseurl = "{{.Site.BaseURL}}{{.Site.LanguagePrefix}}"; + } else { + var hasLangPostfix = location.pathname.includes("/{{.Site.Language.Lang}}"); + if (hasLangPostfix) { + if (isSlug) { + baseurl = location.origin + siteBaseURL.match(siteBaseChecker)[0] + "{{.Site.Language.Lang}}"; + } else { + baseurl = location.origin + "/{{.Site.Language.Lang}}"; + } + } else { + if (isSlug) { + baseurl = location.origin + siteBaseURL.match(siteBaseChecker)[0]; + } else { + baseurl = location.origin; + } + } + } + {{ else }} + if (isThemeSite) { + baseurl = "{{.Site.BaseURL}}"; + } else { + if (isSlug) { + baseurl = location.origin + siteBaseURL.match(siteBaseChecker)[0]; + } else { + baseurl = location.origin; + } + } + {{ end }} + var searchResults = null; var searchMenu = null; var searchText = null; @@ -563,13 +598,13 @@ var fuse = null; - function endsWith(str, suffix) { - return str.indexOf(suffix, str.length - suffix.length) !== -1; - } - function initFuse() { + if (!endsWith(baseurl, "/")) { + baseurl = baseurl + '/'; + }; + var xhr = new XMLHttpRequest(); - xhr.open('GET', permalink + "index.json"); + xhr.open('GET', baseurl + "index.json"); xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); xhr.onload = function () { if (xhr.status === 200) {