Skip to content

Commit

Permalink
feat(plugin-slimsearch): improve default querySplitter, close #299
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Dec 16, 2024
1 parent a11237c commit 715dde1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useRouteLocale } from 'vuepress/client'

import { isFunction } from 'vuepress/shared'
import type { SearchResult, WorkerSearchOptions } from '../typings/index.js'
import { defaultQuerySplitter } from '../utils/index.js'

declare const __VUEPRESS_DEV__: boolean

Expand Down Expand Up @@ -33,7 +34,9 @@ export interface SearchOptions extends SearchLocaleOptions {
locales?: Record<string, SearchLocaleOptions>
}

const searchOptions: Ref<SearchOptions> = ref({})
const searchOptions: Ref<SearchOptions> = ref({
querySplitter: (query) => Promise.resolve(defaultQuerySplitter(query)),
})

const slimsearchSymbol: InjectionKey<Ref<SearchOptions>> = Symbol(
__VUEPRESS_DEV__ ? 'slimsearch' : '',
Expand Down
1 change: 1 addition & 0 deletions plugins/search/plugin-slimsearch/src/client/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './createSearchWorker.js'
export * from './getResultPath.js'
export * from './isFocusingTextControl.js'
export * from './isKeyMatched.js'
export * from './querySplitter.js'
16 changes: 16 additions & 0 deletions plugins/search/plugin-slimsearch/src/client/utils/querySplitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const CJK_REGEXP =
/[\u4e00-\u9fff\u3400-\u4dbf\u3040-\u309f\u30a0-\u30ff\uac00-\ud7af]/

export const defaultQuerySplitter = (query: string): string[] =>
query
.split(/\s+/)
.map((word) => {
if (word.length > 3) {
const chars = word.split('')

if (chars.every((char) => CJK_REGEXP.test(char))) return chars
}

return word
})
.flat()
18 changes: 18 additions & 0 deletions plugins/search/plugin-slimsearch/tests/querySliptter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, it } from 'vitest'
import { defaultQuerySplitter } from '../src/client/utils/querySplitter.js'

it('defaultQuerySplitter', () => {
const testCases: [string, string[]][] = [
['你好世界', ['你', '好', '世', '界']],
['你好 世界', ['你好', '世界']],
['hello world', ['hello', 'world']],
['hello 你好 world 世界', ['hello', '你好', 'world', '世界']],
['hi hello 你好 世界', ['hi', 'hello', '你好', '世界']],
['', ['']],
]

for (const [query, expected] of testCases) {
const result = defaultQuerySplitter(query)
expect(result).toEqual(expected)
}
})

0 comments on commit 715dde1

Please sign in to comment.