From 306d606b9bd169abb5228bbd50029f33d48e3cec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?WangLiang/=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Sat, 23 Jul 2022 23:13:38 +0800 Subject: [PATCH 1/6] fix search.js 1. url is wrong if multiple contents are on the same site, example: `https://xxxxx/docs/` and `https://xxxxx/blog/` 2. when the `token.text` is undefined, `handlePostContent` is undefined too at line 216 --- src/plugins/search/search.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index f889a08e5..84958f86e 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -94,6 +94,7 @@ export function genIndex(path, content = '', router, depth) { } else { slug = router.toURL(path, { id: slugify(escapeHtml(token.text)) }); } + slug = location.pathname + slug; if (str) { title = str @@ -130,9 +131,7 @@ export function genIndex(path, content = '', router, depth) { token.text = getTableData(token); token.text = getListData(token); - index[slug].body = index[slug].body - ? index[slug].body + token.text - : token.text; + index[slug].body = token.text || ''; } } }); From 30fa174694b5e5e0ee9285d34933b3b3ed70cba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Tue, 6 Dec 2022 09:47:51 +0800 Subject: [PATCH 2/6] fix 404 --- src/plugins/search/search.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 84958f86e..a1d77a1f1 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -94,7 +94,18 @@ export function genIndex(path, content = '', router, depth) { } else { slug = router.toURL(path, { id: slugify(escapeHtml(token.text)) }); } - slug = location.pathname + slug; + + // fix 404 + let pathname = location.pathname; + while (slug.startsWith("#/../") && pathname.lastIndexOf("/") > 1) { + slug = "#/" + slug.substring(5); + + if (pathname.endsWith("/")) { + pathname = pathname.substring(0, pathname.length - 1); + } + pathname = pathname.substring(0, pathname.lastIndexOf("/")); + } + slug = pathname + slug; if (str) { title = str From 7b974ef082a77837afd1c4b4bb8dbbff5c3c3ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Tue, 6 Dec 2022 09:55:24 +0800 Subject: [PATCH 3/6] When manually delete docsify.search.index and keep docsify.search.expires, this plugin can still operate normally --- src/plugins/search/search.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 5f7409b21..a4ae32575 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -100,13 +100,13 @@ export function genIndex(path, content = '', router, depth) { // fix 404 let pathname = location.pathname; - while (slug.startsWith("#/../") && pathname.lastIndexOf("/") > 1) { - slug = "#/" + slug.substring(5); + while (slug.startsWith('#/../') && pathname.lastIndexOf('/') > 1) { + slug = '#/' + slug.substring(5); - if (pathname.endsWith("/")) { + if (pathname.endsWith('/')) { pathname = pathname.substring(0, pathname.length - 1); } - pathname = pathname.substring(0, pathname.lastIndexOf("/")); + pathname = pathname.substring(0, pathname.lastIndexOf('/')); } slug = pathname + slug; @@ -287,9 +287,9 @@ export function init(config, vm) { const isExpired = localStorage.getItem(expireKey) < Date.now(); - INDEXS = JSON.parse(localStorage.getItem(indexKey)); + INDEXS = isExpired ? null : JSON.parse(localStorage.getItem(indexKey)); - if (isExpired) { + if (!INDEXS) { INDEXS = {}; } else if (!isAuto) { return; From fa1246103d19b13d751b40ec741bb88d1b12a87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Tue, 6 Dec 2022 10:02:30 +0800 Subject: [PATCH 4/6] optimize fix 404 --- src/plugins/search/search.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index a4ae32575..1b68e0f16 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -100,15 +100,17 @@ export function genIndex(path, content = '', router, depth) { // fix 404 let pathname = location.pathname; + let flag = false; while (slug.startsWith('#/../') && pathname.lastIndexOf('/') > 1) { - slug = '#/' + slug.substring(5); - + flag = true; if (pathname.endsWith('/')) { pathname = pathname.substring(0, pathname.length - 1); } pathname = pathname.substring(0, pathname.lastIndexOf('/')); + + slug = '#/' + slug.substring(5); } - slug = pathname + slug; + slug = pathname + (flag ? '/' : '') + slug; if (str) { title = removeDocsifyIgnoreTag(str); From b1df79d7e4d2a76adb68223bd3eb048fcca71e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Tue, 3 Jan 2023 14:51:06 +0800 Subject: [PATCH 5/6] revert --- src/plugins/search/search.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 1b68e0f16..85bfdb4e9 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -289,9 +289,9 @@ export function init(config, vm) { const isExpired = localStorage.getItem(expireKey) < Date.now(); - INDEXS = isExpired ? null : JSON.parse(localStorage.getItem(indexKey)); + INDEXS = JSON.parse(localStorage.getItem(indexKey)); - if (!INDEXS) { + if (isExpired) { INDEXS = {}; } else if (!isAuto) { return; From f41ee5a629a43ab7e3a897579fd13448f194b1fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Tue, 3 Jan 2023 14:53:11 +0800 Subject: [PATCH 6/6] revert fix 404 --- src/plugins/search/search.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 85bfdb4e9..f8d7cd3ab 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -98,20 +98,6 @@ export function genIndex(path, content = '', router, depth) { slug = router.toURL(path, { id: slugify(escapeHtml(text)) }); } - // fix 404 - let pathname = location.pathname; - let flag = false; - while (slug.startsWith('#/../') && pathname.lastIndexOf('/') > 1) { - flag = true; - if (pathname.endsWith('/')) { - pathname = pathname.substring(0, pathname.length - 1); - } - pathname = pathname.substring(0, pathname.lastIndexOf('/')); - - slug = '#/' + slug.substring(5); - } - slug = pathname + (flag ? '/' : '') + slug; - if (str) { title = removeDocsifyIgnoreTag(str); }