wilhelmer / mkdocs-localsearch

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

Works not with language parameters #6

Closed tanrak closed 4 years ago

tanrak commented 4 years ago

I have tested the plugin and found that it does not work with other languages. For example an excerpt from my mkdocs.yml:

theme:
    name: material
    custom_dir: theme
#    language: de

plugins:
    - search:
        lang:
#            - de
            - en
    - localsearch

Commented (with hash) it works, but commented out (without hash) it does not work.

Edit: Browser is Firefox

wilhelmer commented 4 years ago

This seems to be an issue with MkDocs Material.

With the configuration above, I get the following console output (FF, file:// protocol):

TypeError: lunr.multiLanguage is undefined index.ts:148:15
Loading failed for the <script> with source “file:///C:/Temp/mkdocs-test/test/lunr/min/lunr.de.min.js”. index.html:1:1
Loading failed for the <script> with source “file:///C:/Temp/mkdocs-test/test/lunr/min/lunr.multi.min.js”. index.html:1:1
Loading failed for the <script> with source “file:///C:/Temp/mkdocs-test/test/lunr/min/lunr.stemmer.support.min.js”. index.html:1:1

With search.lang set to de only, I get:

TypeError: e is undefined lunr.js:2678:2
Loading failed for the <script> with source “file:///C:/Temp/mkdocs-test/test/lunr/min/lunr.de.min.js”. index.html:1:1
Loading failed for the <script> with source “file:///C:/Temp/mkdocs-test/test/lunr/min/lunr.stemmer.support.min.js”. index.html:1:1

The issue seems to be related to both the theme.language and the search.lang settings.

If I use a web server, everything works as expected.

@squidfunk Could you please look into this? I have attached a sample project: localsearch-issue-6.zip

squidfunk commented 4 years ago

Might be related to iframe-worker and importScripts, but needs further investigation. Currently, tight on time. Help appreciated.

squidfunk commented 4 years ago

So, I took some minutes to investigate. It's indeed related to importScripts, as they're loaded directly before setting up the index, which means that the stemmers haven't loaded yet. This is a limitation of the polyfill which is stated in the caveats section.

What I just tested is whether we can return a Promise from importScripts, and it seems to work. The latest master of iframe-worker contains the fix. The cool thing is that we could now use async/await in Material for MkDocs to await on importScripts, as awaiting a function that does not return a Promise is a no-op. However, I don't have the time to make the necessary adjustments in the worker code and test whether the non-local (i.e. default) case still works. If somebody could pick up from here, I'd be grateful. The code in question is here.

I attached the patched iframe-worker runtime which passes all tests: polyfill.zip

wilhelmer commented 4 years ago

Not sure if that's what you wanted me to do, but I downloaded your ZIP file, added index.js to extra_javascript in the sample project, built the project and tested using Material 5.1.4:

squidfunk commented 4 years ago

No, the search worker code needs to be adjusted to await on importScripts

wilhelmer commented 4 years ago

I tried, but couldn't get it to work. Sorry 😞 I have no experience with Typescript or async/await. I'm not a programmer, just a poor script kiddie.

squidfunk commented 4 years ago

Support for awaitable importScripts was just added to Material for MkDocs Insiders ☺️ This should resolve the problems described in this issue, as the search index will now wait until the scripts have been loaded.

wilhelmer commented 4 years ago

Great, thanks. I'll check it out as soon as I have access to Insiders via my corporate account.

squidfunk commented 4 years ago

Great! We can work on this together from then on, so search works for your product after all. The funding goal for general availability is also not that far away.

squidfunk commented 4 years ago

The funding goal was hit, so let's revisit this issue when Material 6 is released, which should fix this problem. I'm pretty sure it's going to arrive this weekend. If the issue persists, we can work on it.

wilhelmer commented 4 years ago

Uuuh v6, it's gonna be a major release. Yes, let's do that.

squidfunk commented 4 years ago

https://github.com/squidfunk/mkdocs-material/commit/9fe00ec0af47f9a3ee0c405d8692611ae4138269 fixes this issue, by detecting iframe-worker inside the search worker, fixing up the base URL and loading the stemmers via awaited importScripts. Note that two bugfix releases of iframe-worker followed, i.e. 0.1.8 and 0.1.9, so make sure to use the latest version, which is 0.1.9. My testing shows that this issue is now resolved. I tested with the following configuration.

theme:
  name: material
  language: de

plugins:
  - search
  - localsearch

The fixes are not yet released, so for now you'll have to check master. A release should follow this week with some more post v6 bugfixes 😊

Just released it as 6.0.1.

wilhelmer commented 4 years ago

Also tested this, works like a charm. Thanks for your support 🙏