-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Have a search bar #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
A search bar would indeed be very useful, but I don't think it's trivial to implement. Ideally there would be some priority, like this:
that would require to parse the markdown a second time, maybe we can hook into the parser from pulldown-cmark without the renderer?
A more general approach could be to specify high priority keywords in |
👍 this is something people have asked from rustbook forever see rust-lang/rust#22786 |
Is anyone else interested in working on this? If not, I might take a crack at generating a naive search index and seeing how it works on the rust book. |
I have exams until end of January, so.. be my guest! 😉 I think using lunr.js has been considered in the past, so maybe take a look. But if you have other ideas, go ahead. I haven't done a lot of research about the subject. If you have any question about the project, or you want to share advancement on this matter, feel free to post here! Good luck 👍 |
So I just noticed I can't search the Rust reference anymore, which isn't really helpful. Looking already existing search frameworks has resulted in these:
Using Tantivy would require compiling to asmjs (I guess around 10MB), or rewriting the search functionality in JS. And the search index needs to be deployed with the book. With the two JS variants the situation is a bit different: The index could be generated during compilation with node.js, or when the book is opened. I personally prefer the last option. It saves space for the Index, does not introduce additional build dependencies and the search framework can be easily updated. |
I wasn't aware there was a search functionality in the old reference? Or was it just the browsers Granted, it's not intuitive and a real search functionality would be better :) |
@azerupi you are correct on all counts. |
Just here because I also ran into rust-lang/reference#73. |
I wanted to open this same issue... |
Hi @crumblingstatue and @lilianmoraru search is certainly somewhere on the wishlist but currently we have no ETA for such a feature. But if anyone is willing to start working on it (or even start a discussion about possible implementation) they are very welcome! |
There are two paths I can see with this implementation. Use Lunr JS in the front endThe first is simply using lunr.js and doing everything in the frontend (pretty inefficient as the site gets bigger). This is something that mkdocs does already. Process would be to generate a JSON document that lunr.js can load and use to provide search capabilities. This would just be an extra output type. Use Tantivy in the back endThe second would be to support a server-based search implementation. This would mean though that mdbook wouldn't be a purely static output, rather it would require a server to serve requests. Something like tantivy would be a perfect fit. Taking it a step further maybe compiling to WASM so you could run it on the front end, although I don't know how feasible that would be. Process here would be maybe to switch it on as a flag, but have it running in the backend as a server, and have tantivy reindex on build. I think that tantivy would provide more performant results than lunr js but would no longer be a purely static output. |
Thanks for the input!
Yeah I think that loosing the static aspect is a really big drawback. I am not sure I would be willing to accept that. Gitbook uses Lunr, I think that is the way to go, at least for now. |
Unfortunately introducing dynamic backend requirement as a requirement would be a nogo for me in terms of rust-cookbook maintenance 😞. I would also go with the Lunr.js. It seams really snappy on large gitbooks (on par or larger than rust-book) that I've tested sofar (both when searching the index and with full text search). I think that we might think about optimization once we encounter any significant performance problems. |
Yep, I figured that a dynamic backend wouldn't be appropriate, but put it out there as a possible option. I still like the WASM idea though! Don't know if that is feasible at this stage, but would be awesome. Is NPM/Node already a requirement for building using mdbook? I couldn't see a package.json anywhere. If so, you can use lunr.js to prebuild an index which might speed things up a little bit. |
It is not required for building mdBook, but it is required as a dev dependency to compile the stylus files.
I would definitely build the index at build time. Is Lunr required for this or is it simple enough to do it ourselves with Rust? |
awesome! |
I did a first stab at the UI, please comment. For demonstration purposes, it indexes the current visible page with elasticlunr.js and searches that.
Todo:
|
Nice! Very cool :) Displaying the search results in the sidebar is pretty cool, but it limits the number of results we can display at any one time. I'm also not sure if it will be usable on mobile. What about something like rustdoc or having a dropdown list with the results? This would give a little more playing room to display the results in a comprehensive way.
If we do something like rustdoc (or a dropdown) we can set the limit to a high value and provide a scroll. I suppose there should be a limit to avoid really irrelevant results, but depending on the design that limit can be set to 20+ results.
I'm not sure I understand what you are proposing here. Could you elaborate? :)
Yes, that could be useful. I suppose this would rely on the search engine to provide this info along with the results? Is that a feature they provide (lunr in particular, since that is what we settled on)? Otherwise, really great job! It looks very promising 👍 |
@Phaiax go for it! I'm not very experienced with web stuff. Having each section be a separate document does seem to be the way to go. With the way fields are added to documents, you can't really support subsection headings unless each section is a document. For results on other pages, I was thinking using |
Lunr will only return you whatever you defined the 'document' reference to be, though this can be used to look up a title or summary from some other data structure.
Sounds like a url fragment, e.g. |
Sure, plus a span for highlighting |
I moved the search results to the center, added history functionality, and fixed mobile for chrome on android at least. Probably need browser cache flush (Ctrl+Shift+R): https://phaiax.github.io/mdBook/cli/init.html I also tried to integrate More Todos:
|
@Phaiax This looks darn nice. |
@Phaiax great job! That looks very slick! @lilianmoraru yes, it is just a design proposal for now using the current page as a sample. We still need to build an index during the build and hook the two up. But this is a great step forward! :) |
Quick status update about elasticulnr-rs. The json index looks correct except that the leaf nodes of the index don't actually link to any documents. Current plan is to port over tests from the JS version, which should uncover the issue. Then get it to work on stable, polish, docs, etc. Likely won't have time to work on this until the weekend. |
So, I've put together a basic implementation of a Lunr backend in Rust. It works, the included example will generate an index that can be loaded by lunr.js. There is a lot still to do, I've opened issues trying to cover the major ones. Also, as previously mentioned I'm still new to Rust so I'm positive there are a thousand ways the implementation could be improved, is anyone willing to lend a hand? |
The search is usable now . I'm not sure about the design of the results and the preview text yet. Has anyone an idea for an algorithm to select a better snippet from the unformatted paragraph string?
It would be best to decide for some snippet that contains the area of the document where the search terms occour the most. And for testing purposes, this is the new search with the second edition of the rust book. The |
This looks and works awesome! 👍
Also a slightly off topic question, would there be a way to search by some metadata added in js by hand (ie crate names in rust cookbook)? |
For the snippets, have a look at how solr handles this: https://lucene.apache.org/solr/guide/6_6/highlighting.html |
|
For the snippets, I chose my own algorithm, understanding the complex solr highlighters would take to much time. |
@Phaiax very cool!
I'd probably want the highlight as unsticky as possible (possibly removed on any kind of interaction). But someone with actual UX knowhow should probably give us some insight. For now I'd be happy to just have a js func to clear the highlight. We might consult other stakeholders. |
This doesn't need to be perfect from the beginning. |
Totally agree |
What's the status of this? Ready for a PR or is there some more work to be done? |
I am just waiting for #457 , but that would not really be a blocker. @azerupi Where are youuuuuuu? 😃 Edit: Sry for the mails, it did not show the comments after clicking on 'comment' |
Looks good! I have noticed a couple of things but not major, and are related to some other issues (#463 and mattico/elasticlunr-rs#2) |
@Phaiax & @cetra3 The repo is currently looking for new owner maintainer. We (the collaborators) will try to help some more in the meantime. I'm especially interested in this PR and hope to start nudging it forward in the upcoming days. Thanks! |
Having a search bar would be very very useful. rustbyexample has one for example. I'm not sure how it would be implemented. I'm sure this has long been wanted by the rust book.
Being able to search for programming keywords would actually make it very unusual and better than most too (outside the scope of a regular search bar though)
The text was updated successfully, but these errors were encountered: