From 9432412c22d1d231880e9157515dfc6ff599fbbc Mon Sep 17 00:00:00 2001 From: HaoXuan40404 <444649358@qq.com> Date: Thu, 21 Nov 2024 10:51:24 +0800 Subject: [PATCH] fix chinese words search --- .gitignore | 1 + 3.x/zh_CN/conf.py | 24 +++++++++++++++++++++++- requirements.txt | 4 +++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 984adaab8..cc1cd1147 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ _build .DS_Store .vscode/ *.bak +3.x/zh_CN/venv/ diff --git a/3.x/zh_CN/conf.py b/3.x/zh_CN/conf.py index d8020d241..fd4edea85 100644 --- a/3.x/zh_CN/conf.py +++ b/3.x/zh_CN/conf.py @@ -14,11 +14,12 @@ import sys import os +import jieba #sys.path.insert(0, os.path.abspath('..')) import sphinx_rtd_theme from recommonmark.transform import AutoStructify - +from sphinx.search import SearchEnglish DOC_SOURCES_DIR = os.path.dirname(os.path.abspath(__file__)) PROJECT_ROOT_DIR = os.path.dirname(os.path.dirname(DOC_SOURCES_DIR)) @@ -64,6 +65,15 @@ 'myst_parser' ] +myst_enable_extensions = [ + "linkify", # 自动转换 URL 为链接 + # "replacements", # 支持文本替换 + # "smartquotes", # 美化引号 + # "strikethrough", # 支持删除线 + # "substitution", # 支持替换变量 + # "attrs_block", # 支持 Markdown 块级属性 +] + # Add any paths that contain templates here, relative to this directory. templates_path = ['../_templates'] @@ -234,6 +244,11 @@ #html_search_language = 'en' html_search_language = 'zh' +html_search_options = { + 'search_language': 'zh', # 指定中文 + 'search_mode': 'full_text', # 全文搜索 +} + # A dictionary with options for the search language support, empty by default. # Now only 'ja' uses this config value #html_search_options = {'type': 'default'} @@ -347,6 +362,12 @@ # Markdown support +class SearchChinese(SearchEnglish): + def split(self, input): + # 使用 jieba 对输入文本分词 + return list(jieba.cut(input)) + + github_doc_root = 'https://github.com/rtfd/recommonmark/tree/master/doc/' def setup(app): app.add_config_value('recommonmark_config', { @@ -360,6 +381,7 @@ def setup(app): app.add_stylesheet('css/custom.css') app.add_javascript('js/readthedocs-analytics.js') app.connect('build-finished', replace_source) + app.add_config_value('additional_keywords', additional_keywords, 'env') def replace_source(app, exception): if exception is None: diff --git a/requirements.txt b/requirements.txt index 367c31524..b4ca064fb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,4 +13,6 @@ git-lfs Jinja2<3.1 myst-parser == 0.13.0 pyyaml - +jieba +linkify-it-py +# markdown-it-py \ No newline at end of file