wilhelmer / mkdocs-localsearch

A MkDocs plugin to make the native "search" plugin work locally (file:// protocol)
MIT License
32 stars 8 forks source link

add useCustomSearch option to prevent from overwriting __search metho… #16

Closed MtShan closed 3 years ago

MtShan commented 3 years ago

I customized search query transformation because it is needed for Japanese full-text search. Japanese does not use any spaces between words, so splitting sentence to words (segmentation) is not an easy job. Segmentation result strongly depends on segmentation library. MkDocs Material use Lunr for searching, and Lunr use TinySegmenter for Japanese segmentation. So, we need to use TinySegmenter for search query segmentation. That is why I made issue and I made pull request. Please review my pull request and I hope you accept my proposal. Thank you for your useful plug-in. The previous my pull request was on my wrong branch, so I created new pull request again on my feature/#13 branch. And I am sorry for bothering you.

squidfunk commented 3 years ago

You could just use __search.transform to override the transform when using localsearch. AFAICT this PR is unnecessary.

Other than that, the search pipeline should already use the segmenter for query transformation. If you can prove that it works with a mint lunr.js setup, we can see what we got wrong in the Material theme.

MtShan commented 3 years ago

I tried to use __search.transform to override the transform, but it does not work well. I wrote overrides/main.html as follows:

{% extends "base.html" %}
{% block config %}
{{ super() }}
{% if "localsearch" in config["plugins"] %}
<script src="{{ 'assets/javascripts/iframe-worker.js' | url }}"></script>
<script src="{{ 'search/search_index.js' | url }}"></script>
{% endif %}
<script src="https://cdn.jsdelivr.net/npm/tiny-segmenter@0.2.0/dist/tiny-segmenter-0.2.0.min.js"></script>
<script>
var __search = {
    index: Promise.resolve(local_index),
    transform: function(query) {
        var segmenter = new TinySegmenter();
        var segs = segmenter.segment(query);
        var newQuery = segs.join(" ");
        console.log(newQuery);
        return newQuery;
    }
}
</script>
{% endblock %}

At first the localsearch plugin puts index: Promise.resolve(local_index), in site/search/search_index.js, and I wrote overrides/main.html without index: Promise.resolve(local_index), then it does not work.

The segmenter tiny-segmenter-0.2.0.min.js is used in tinyseg.js in lunr-languages. function TinySegmenter is the segmenter.

wilhelmer commented 3 years ago

Please try the code snippet posted here.

MtShan commented 3 years ago

Dear Mr. wilhelmer, Thank you so much about your advice. Your advice is exactly correct. My issue is instantaneously solved by your advice. I was amateur and I was wrong. I am so sorry to bother you.