Skip to content

utf-8 search support #826

Closed
Closed

Description

@imehdihosseini

Hey, we are working on a Persian version of a mkdocs theme, everything works fine but we have a problem on search contents with Persian contents, when i search the expression nothing found, i also test it with Arabic, Chinese and Hebrew and all of theme have problem with this case. any solution?

Activity

  1. changed the title [-]Search utf-8 support[/-] [+]utf-8 search support[/+] on Feb 16, 2016
  2. d0ugal commented on Feb 16, 2016

    @d0ugal
    Contributor

    Unfortunately search wont work well in non-latin character sets at the moment. We use lunr.js to provide the search functionality, I believe it can work with them but I am not sure what is involved in making that happen.

    It is possible for themes to overwrite and completely replace the search implementation. To do this, themes would need to at least replace this file (or modify the theme so that file isn't loaded) by including a javascript file in the theme with the path mkdocs/js/search.js. However, as I said at the top, I have no experience in making search work for different character sets, so I don't know how difficult it will be.

    We could also consider adding a setting to disable search. While this isn't ideal, it would be better to turn search off than have it in a broken state.

    I don't think there is a good, easy solution for this at the moment. 😞

  3. waylan commented on Mar 31, 2016

    @waylan
    Contributor

    There is also some related discussion in #859. Apparently, non-English search indexes are extremely large as lunr.js only "excludes" insignificant English words from the index by default. MihaiValentin/lunr-languages appears to be an add-on to lunr.js which adds support for various other languages including Japanese and Russian (both use non-latin alphabets). Unfortunately, at this time, Persian, Arabic, Chinese and Hebrew are not on the list of supported languages. Although there are currently outstanding requests for Arabic and Chinese to be added. If you would like to have support added for a certain language, I would suggest starting there. If/when, mkdocs adds language support, then your language will work if its supported by lunr.js.

  4. added this to the 1.0.0 milestone on Dec 2, 2016
  5. self-assigned this
    on Dec 2, 2016
  6. waylan commented on Sep 29, 2017

    @waylan
    Contributor

    @netroby, that is some old legacy (now deprecated) code which will be removed in the next major release. It has nothing to do with the current search implementation or any future updates to search.

  7. waylan commented on Feb 1, 2018

    @waylan
    Contributor

    In a brief discussion about how to pass config settings to lunr.js, @squidfunk pointed out that the Material (third party) theme passes the values in via meta tags in the template (<meta name="lang:search.language" content="en">). However, it occurred to me that the document still has <html lang="en"> at the top. Obviously, if the documents contain something other than English content (requiring a search lang other than en), then perhaps the html tag should indicate that. In fact, Declaring language in HTML seems to indicate as much.

    Therefore, I'm thinking that we approach this in two ways:

    1. Create a global lang config setting which is used as <html lang="{{ config.lang }}"> in the template. Search could then use this as a default language to use if one is not otherwise specified.
    2. Create a search plugin specific lang setting which can contain multiple language codes. Lunr supports indexing multiple languages and multiple languages can exist on one page. However, only one language gets defined in the html tag (see Declaring language in HTML). The plugin specific setting should include all possible languages for that site that will be indexed (see Multi-Language Content in Lunr docs).

    Finally, rather than using the meta tags to pass the plugin specific settings, I'm inclined to include them in the JSON index file as so:

    { 'docs': [...], 'config': { 'lang': ['en, 'es'], 'otherkey': 'value'} }
    

    Of course, any third party theme can ignore those. For example, Material would continue to work without changes (except to use config.plugins.search.lang rather than config.extra.search.lang as the source of its data to populate the meta tag).

  8. waylan commented on Feb 1, 2018

    @waylan
    Contributor

    I just realized that having a global lang config setting (which sets <html lang="en">) means we also need to have Internationalization support for hardcoded text in templates (see #211). And I'm unsure how that will work with pages in different languages (see #774). I think at this time it makes sense to implement the search config setting (part 2 above) and leave the global setting to be implemented as part of #211 and/or #774.

    In the meantime, the search config setting plugins.search.lang should probably default to ['en'], which is consistent with the current behavior.

    Or perhaps all multi-language support matters should be addressed together, in which case this issue would be delayed to be addressed with #211 and #774 rather than as part of the search refactor.

  9. waylan commented on Feb 2, 2018

    @waylan
    Contributor

    I almost have language support working. I was able to get it working within a worker in 6034dc2. I tried to adapt that to also run in the main thread in db1a84a as a fallback for browsers which don't support workers, Unsurprisingly, it fails as the scripts are not finished loading before the code attempts to run. I understand how to use a callback to avoid that sort of problem, but it is not obvious to me here how to make that work when we have multiple potential scripts which could be loaded and those calls are all nested within various separate conditionals. JavaScript's async nature is my biggest weakness.

    @yeraydiazdiaz I started with your code. Any suggests from you or anyone?

  10. waylan commented on Feb 2, 2018

    @waylan
    Contributor

    For the record, you can see all of my recent search related work (including the code this builds on) at master...waylan:new-search.

  11. yeraydiazdiaz commented on Feb 3, 2018

    @yeraydiazdiaz
    Contributor

    Nice job adding language support.

    I tweaked your version slightly to have a separate process where we first load the JSON, then resolve the Lunr files to fetch, grab them asynchronously and finally activate the search. Seems to work well with and without Web Worker support.

    Give it a go and let me know what you think.

    Edit: Of course, as I posted this I found an error, seems the multilanguage support requires Lunr to be loaded before executing them, I'll see if I can work around that

  12. yeraydiazdiaz commented on Feb 3, 2018

    @yeraydiazdiaz
    Contributor

    I managed to solve the issue by abusing $.getScript callbacks, pretty awful but I guess it beats using require.js just for old browser support.

    Here are the changes the full changes against your branch waylan:new-search...yeraydiazdiaz:new-search.

  13. waylan commented on Feb 3, 2018

    @waylan
    Contributor

    I considered recursively loading each script but it wasn't obvious to me how to make it work. Nicely done.

  14. added a commit that references this issue on Mar 6, 2018
    dd7e2d9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions