diff --git a/plugins/search/plugin-slimsearch/src/client/worker/index.ts b/plugins/search/plugin-slimsearch/src/client/worker/index.ts index feb09fa3b..8f4bd9ab4 100644 --- a/plugins/search/plugin-slimsearch/src/client/worker/index.ts +++ b/plugins/search/plugin-slimsearch/src/client/worker/index.ts @@ -8,9 +8,9 @@ import { getResults } from './result.js' import { getSuggestions } from './suggestion.js' self.onmessage = async ({ - data: { type = 'all', query, locale, options, id }, + data: { type = 'all', query, locale = '/', options, id }, }: MessageEvent): Promise => { - const { default: localeIndex } = await database[locale ?? '/']() + const { default: localeIndex } = await database[locale]() const searchLocaleIndex = loadJSONIndex( localeIndex, diff --git a/plugins/search/plugin-slimsearch/src/client/worker/matchContent.ts b/plugins/search/plugin-slimsearch/src/client/worker/matchContent.ts index 0dbfdd73e..f89f73c72 100644 --- a/plugins/search/plugin-slimsearch/src/client/worker/matchContent.ts +++ b/plugins/search/plugin-slimsearch/src/client/worker/matchContent.ts @@ -14,33 +14,29 @@ export const getMatchedContent = ( let startIndex = 0 let contentLength = 0 - const addResult = (content: string, isEnd = false): void => { - let text = '' + const addResult = (text: string, isEnd = false): void => { + let display: string // A beginning of a long string if (contentLength === 0) - text = - content.length > SUFFIX_LENGTH - ? `… ${content.slice(-SUFFIX_LENGTH)}` - : content + display = + text.length > SUFFIX_LENGTH ? `… ${text.slice(-SUFFIX_LENGTH)}` : text // Already the last text else if (isEnd) - text = + display = // If the string will be longer than maxLength - content.length + contentLength > MAX_LENGTH - ? `${content.slice(0, MAX_LENGTH - contentLength)}… ` - : content + text.length + contentLength > MAX_LENGTH + ? `${text.slice(0, MAX_LENGTH - contentLength)}… ` + : text // Text is at the middle else - text = - content.length > SUFFIX_LENGTH - ? `${content.slice(0, SUFFIX_LENGTH)} … ${content.slice( - -SUFFIX_LENGTH, - )}` - : content - - if (text) result.push(text) - contentLength += text.length + display = + text.length > SUFFIX_LENGTH + ? `${text.slice(0, SUFFIX_LENGTH)} … ${text.slice(-SUFFIX_LENGTH)}` + : text + + if (display) result.push(display) + contentLength += display.length if (!isEnd) { result.push(['mark', queryString]) diff --git a/plugins/search/plugin-slimsearch/tests/__fixtures__/src/example.md b/plugins/search/plugin-slimsearch/tests/__fixtures__/src/example.md index 086224faf..53dbe3058 100644 --- a/plugins/search/plugin-slimsearch/tests/__fixtures__/src/example.md +++ b/plugins/search/plugin-slimsearch/tests/__fixtures__/src/example.md @@ -106,8 +106,8 @@ Sample text here... Syntax highlighting: ```js -const foo = function (bar) { - return bar++ +function foo(bar) { + return `foo${bar}` } console.log(foo(5)) diff --git a/plugins/search/plugin-slimsearch/tests/__snapshots__/generateIndex.spec.ts.snap b/plugins/search/plugin-slimsearch/tests/__snapshots__/generateIndex.spec.ts.snap index 1ade70bc2..d4a344564 100644 --- a/plugins/search/plugin-slimsearch/tests/__snapshots__/generateIndex.spec.ts.snap +++ b/plugins/search/plugin-slimsearch/tests/__snapshots__/generateIndex.spec.ts.snap @@ -168,7 +168,7 @@ exports[`generateIndex > Should generate full index 2`] = ` "Block code:", "Sample text here... ", "Syntax highlighting:", - "const foo = function (bar) { return bar++ } console.log(foo(5)) ", + "function foo(bar) { return \`foo\${bar}\` } console.log(foo(5)) ", ], }, ] @@ -755,7 +755,7 @@ exports[`generateIndex > Should support customFields with full index 2`] = ` "Block code:", "Sample text here... ", "Syntax highlighting:", - "const foo = function (bar) { return bar++ } console.log(foo(5)) ", + "function foo(bar) { return \`foo\${bar}\` } console.log(foo(5)) ", ], }, ]