diff options
Diffstat (limited to 'assets')
| -rw-r--r-- | assets/js/main_new.js | 13 | ||||
| -rw-r--r-- | assets/js/pjax.js | 49 |
2 files changed, 28 insertions, 34 deletions
diff --git a/assets/js/main_new.js b/assets/js/main_new.js index 22f2552..fc4b6ce 100644 --- a/assets/js/main_new.js +++ b/assets/js/main_new.js @@ -1,4 +1,8 @@ -$(function () { +/** + * 根据 URL ?kw= 参数高亮页面内匹配的关键词。 + * 提取为全局函数,供 pjax.js 在页面切换后复用,避免重复实现。 + */ +function highlightKeyword() { const urlParams = new URLSearchParams(window.location.search); const keyword = urlParams.get('kw')?.trim(); @@ -10,7 +14,7 @@ $(function () { const regex = new RegExp(`(${escapedKeyword})`, 'gi'); // 递归遍历并高亮文本节点 - const escapeHTML = str => str.replace(/[&<>"']/g, + const escapeHTML = str => str.replace(/[&<>"']/g, tag => ({ '&': '&', '<': '<', @@ -41,7 +45,7 @@ $(function () { $('section').each(function () { highlightTextNodes(this); }); -}); +}; function initCopyButtons() { $('.copy').remove(); @@ -58,6 +62,7 @@ function initCopyButtons() { }); } -$(function() { +$(function () { + highlightKeyword(); initCopyButtons(); });
\ No newline at end of file diff --git a/assets/js/pjax.js b/assets/js/pjax.js index 324087a..8f84322 100644 --- a/assets/js/pjax.js +++ b/assets/js/pjax.js @@ -23,34 +23,6 @@ } } - /** 关键词高亮 */ - function reinitHighlight() { - var keyword = new URLSearchParams(window.location.search).get('kw'); - if (!keyword) return; - keyword = keyword.trim(); - if (!keyword) return; - - var escaped = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); - var regex = new RegExp('(' + escaped + ')', 'gi'); - var escapeHTML = function (str) { - return str.replace(/[&<>"']/g, function (t) { - return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[t] || t; - }); - }; - function walk(node) { - $(node).contents().each(function () { - if (this.nodeType === Node.TEXT_NODE) { - var $t = $(this); - var text = escapeHTML($t.text()); - if (regex.test(text)) $t.replaceWith(text.replace(regex, '<mark>$1</mark>')); - } else if (this.nodeType === Node.ELEMENT_NODE && !$(this).is('script, style, noscript, textarea')) { - walk(this); - } - }); - } - $('section').each(function () { walk(this); }); - } - /** Google Analytics 页面浏览事件 */ function trackPageView() { if (typeof gtag === 'function') { @@ -120,15 +92,32 @@ // ========== 初始化 ========== + /** pjax 完成后滚动到目标位置:有锚点则定位锚点,否则回到顶部 */ + function scrollToAnchor() { + var hash = window.location.hash; + if (hash) { + // 中文等非 ASCII 字符在 URL 中会被编码,需先解码再匹配元素 id + var id = hash.slice(1); + try { id = decodeURIComponent(id); } catch (e) { /* 保持原值 */ } + var target = document.getElementById(id) || + document.querySelector('a[name="' + id + '"]'); + if (target) { + target.scrollIntoView({ behavior: 'smooth', block: 'start' }); + return; + } + } + window.scrollTo(0, 0); + } + /** 每次 pjax 完成后执行所有重初始化 */ function onPjaxComplete() { initVisitors(); initCopyButtons(); - reinitHighlight(); + highlightKeyword(); reinitAISummary(); reinitLive2d(); trackPageView(); - window.scrollTo(0, 0); + scrollToAnchor(); } $(document).ready(function () { |
