From 4a4ae8e598319dcc7b7087ecf5128e99d402a9bd Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Sat, 24 Jun 2023 20:17:53 +0800 Subject: [PATCH] =?UTF-8?q?gitbook=20=E6=90=9C=E7=B4=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/gitbook/components/SearchInput.js | 40 +++++++++--------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/themes/gitbook/components/SearchInput.js b/themes/gitbook/components/SearchInput.js index 6828d111..f88615bf 100644 --- a/themes/gitbook/components/SearchInput.js +++ b/themes/gitbook/components/SearchInput.js @@ -1,5 +1,6 @@ import { useImperativeHandle, useRef, useState } from 'react' import { useMediumGlobal } from '../LayoutBase' +import { deepClone } from '@/lib/utils' let lock = false const SearchInput = ({ currentSearch, cRef, className }) => { @@ -22,35 +23,23 @@ const SearchInput = ({ currentSearch, cRef, className }) => { } else { setFilterPosts(allNavPages) } - for (const post of allNavPages) { - const tagContent = post.tags && Array.isArray(post.tags) ? post.tags.join(' ') : '' - const categoryContent = post.category && Array.isArray(post.category) ? post.category.join(' ') : '' - const articleInfo = post.title + post.summary + tagContent + categoryContent - let hit = articleInfo.toLowerCase().indexOf(keyword) > -1 - const indexContent = [post.summary] - // console.log('全文搜索缓存', cacheKey, page != null) - post.results = [] - let hitCount = 0 - for (const i in indexContent) { - const c = indexContent[i] - if (!c) { - continue - } - const index = c.toLowerCase().indexOf(keyword.toLowerCase()) - if (index > -1) { - hit = true - hitCount += 1 - post.results.push(c) - } else { - if ((post.results.length - 1) / hitCount < 3 || i === 0) { - post.results.push(c) - } + const filterAllNavPages = deepClone(allNavPages) + for (const filterGroup of filterAllNavPages) { + for (let i = filterGroup.items.length - 1; i >= 0; i--) { + const post = filterGroup.items[i] + const articleInfo = post.title + '' + const hit = articleInfo.toLowerCase().indexOf(keyword.toLowerCase()) > -1 + if (!hit) { + // 删除 + filterGroup.items.splice(i, 1) } } - if (hit) { - filterPosts.push(post) + if (filterGroup.items && filterGroup.items.length > 0) { + filterPosts.push(filterGroup) } } + + // 更新完 setFilterPosts(filterPosts) } const handleKeyUp = (e) => { @@ -62,6 +51,7 @@ const SearchInput = ({ currentSearch, cRef, className }) => { } const cleanSearch = () => { searchInputRef.current.value = '' + handleSearch() } const [showClean, setShowClean] = useState(false)