Repository navigation
Find Chinese and Japanese words with the built-in search - #172
Merged
Merged
Conversation
Chinese and Japanese are written without spaces, so lunr saw a whole sentence as one word. With `lang: zh` search didn't work at all: the bundled lunr.zh.js calls jieba, which only exists in Node.js, and the index build stopped with an error. Japanese only found whole sentences and two-character words were below min_search_length. - New cjk-segment.js splits Chinese and Japanese text into words with the browser's Intl.Segmenter (one word per character without it). It wraps the index tokenizer and splits the query the same way, also for prebuilt indexes and several languages together. Loaded only when `lang` has zh or ja. - lunr.zh.js falls back to the default tokenizer outside Node.js. - lunr.zh.js and lunr.ja.js stem search words like indexed words, so English words on such pages are found too. - A Chinese or Japanese character counts as two for min_search_length. Also show titles like `Tom & Jerry` instead of `Tom & Jerry` in search results: titles in the index are HTML, and main.js escaped them again.
Contributor
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The built-in
searchplugin couldn't find Chinese or Japanese words. Chinese and Japanese are written without spaces, so lunr saw a whole sentence as one word:lang: zh(also what azhlanguage of alanguages:site gets), search didn't work at all: the bundledlunr.zh.jscalls jieba, which only exists in Node.js, so building the index stopped withCannot read properties of undefined (reading 'cut').lang: [en, zh],lang: jaor a prebuilt index, a word inside a sentence found nothing, and two-character words such as 搜索 were below the defaultmin_search_length.What changes, with no new dependency:
cjk-segment.jssplits Chinese and Japanese text into words with the browser's built-inIntl.Segmenter(one word per character in browsers without it, such as Firefox before 125). It wraps the index tokenizer and splits the search query the same way, so both always agree. It also runs inprebuild-index.js. It is only copied and loaded whenlanghaszhorja; other sites get the same files as before.lunr.zh.jsuses the default tokenizer outside Node.js instead of failing.lunr.zh.jsandlunr.ja.jsstem search words with the same stemmer as indexed words. Before, English words on such pages could not be found when English stemming changed them (Jerry→jerri).main.js: a Chinese or Japanese character counts as two towardsmin_search_length.main.jsalso shows titles such asTom & Jerrycorrectly. Titles in the index are HTML, soescapeHtmlturned&into&. The index is unchanged, so other themes that read it see no difference.Checked in Chromium with the mkdocs and readthedocs themes (search in a Web Worker, in the page, and in the page without
Intl.Segmenter):theme.locale: zh_CNlang: [en, zh]lang: jalang: ja,prebuild_index: truelanguages: {en, zh},/zh/lang: zh/jaTom & JerryRelated Issue
Roadmap item 50 in #59.
Checklist
zh,jaand[en, zh]search.langin the configuration docs, Fixed Upstream Issues pagedocs/about/release-notes.mdupdated (if applicable): under 1.11.2 (unreleased)