From 95c3f7d84f2f758721ad74bee1be4d0274e53537 Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Fri, 6 Nov 2026 05:29:45 +0800 Subject: [PATCH] Update site.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 按 GitHub 的写法打开数字开头的小节(例如 ?id=0-1-背包)时,换成网站上的标题 id 再跳转。 --- website/templates/site.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/website/templates/site.js b/website/templates/site.js index f8898511..1b031e44 100644 --- a/website/templates/site.js +++ b/website/templates/site.js @@ -123,14 +123,21 @@ if (box.scrollWidth > box.clientWidth) box.tabIndex = 0; }); } + // Docsify 给数字开头的标题 id 前面加下划线;GitHub 的写法(0-1-背包)直接交给 Docsify 会让它查找时抛出异常 + function docsifyId(id) { return /^\d/.test(id) ? '_' + id : id; } function preserveLegacyLinks() { var hash = location.hash; var legacy = (window.CS_NOTES_LEGACY_ROUTES || {})[decode(location.pathname)]; if (legacy) { - var heading = hash && !hash.startsWith('#/') ? '?id=' + encodeURIComponent(decode(hash.slice(1))) : ''; + var heading = hash && !hash.startsWith('#/') ? '?id=' + encodeURIComponent(docsifyId(decode(hash.slice(1)))) : ''; location.replace('/#' + encodedRoute(legacy) + heading); return; } + var digit = hash.match(/^(#\/[^?]*\?(?:.*&)?id=)(\d[^&]*)(.*)$/); + if (digit) { + location.replace(digit[1] + '_' + digit[2] + digit[3]); + return; + } // 只剩一个链接的旧页面(例如“算法”)直接换成它指向的笔记,不留在中间页 var stub = (window.CS_NOTES_STUBS || {})[decode(hash.split('?')[0].slice(1)).replace(/\.md$/, '')]; if (stub) {