From 14d05267b723b38520cc054f5abf60bda7581a4f Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Sun, 1 Nov 2026 18:53:01 +0800 Subject: [PATCH] Update search.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 打开另一篇文章的搜索结果时,等新文章渲染出来后再在目标小节中标出搜索词。 --- website/templates/search.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/website/templates/search.js b/website/templates/search.js index 49cce3cd..26b25246 100644 --- a/website/templates/search.js +++ b/website/templates/search.js @@ -211,6 +211,7 @@ } var HIT_SKIP = /^(PRE|CODE|SCRIPT|STYLE|MARK|BUTTON|SVG)$/; + var shownPath = null; function clearHits() { document.querySelectorAll('mark.search-hit').forEach(function (mark) { mark.replaceWith(document.createTextNode(mark.textContent)); @@ -221,7 +222,8 @@ try { saved = JSON.parse(sessionStorage.getItem('cs-notes-search-hit')); } catch (error) { return; } if (!saved || Date.now() - saved.time > 30000) return; var hash = decodeURIComponent(location.hash), href = decodeURIComponent(saved.href.replace(/^\//, '')); - if (hash !== href) return; + // 换了文章时,新文章渲染出来之前页面上还是旧文章,等 doneEach 再标 + if (hash !== href || hash.split('?')[0] !== shownPath) return; sessionStorage.removeItem('cs-notes-search-hit'); clearHits(); var id = new URLSearchParams(href.split('?').slice(1).join('?')).get('id'); @@ -267,6 +269,9 @@ window.$docsify.plugins.push(function (hook) { hook.mounted(mountSearch); hook.doneEach(mountSearch); - hook.doneEach(function () { setTimeout(markHits, 0); }); + hook.doneEach(function () { + shownPath = decodeURIComponent(location.hash.split('?')[0]); + setTimeout(markHits, 0); + }); }); })();