-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Add search to Rails Guides #39299
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
base: main
Are you sure you want to change the base?
Add search to Rails Guides #39299
Conversation
60a706c
to
3f1178e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this addition, nice job!
1b734f6
to
60c90c7
Compare
60c90c7
to
aec62a4
Compare
cc @fxn |
@rolandasb I was thinking about that as well, but thought it was a bit of a mismatch – the content of the top bar references items outside the Guides while the red navigation bar references things contained in the Guides. Since the search only works inside the Guides, but not on rubyonrails.org, I put it in the lower bar. But I don't have a very strong stance on this. |
@p8 It should work if you try again. The "Please wait..." indicator appears the first time you use the search since the index needs to be downloaded. Due to the size (3.2MB) it is lazily loaded. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
What can we do to push this along? I think search would be hugely welcome addition to the Rails guides. Better documentation and onboarding seems to be top of mind for a number of Rubyists these days and this could help. @rafaelfranca @fxn |
I don't really understand the lack of interest for adding search capabilities from the maintainers. For those of us not extremely familiar with the guides, search is a huge help, and as other have commented before, many other projects have embraced adding searching capabilities to their docs. |
/cc @zzak |
49ffdc5
to
33ef886
Compare
guides/rails_guides/search_index.rb
Outdated
link_map_js = "var linkMap = #{link_map.to_json};" | ||
File.write("#{@output_dir}/javascripts/lunr-documents.js", documents_js + link_map_js) | ||
|
||
lunr = open("https://unpkg.com/lunr@2.3.8/lunr.js").read |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be simpler to just 'vendorize' the lunr source, instead of downloading it everytime.
Bump :) seems like a really great addition |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good hunk of this leans on vanilla JS which isn't my forte so will defer to others for their opinions on how it's put together 😅
But from a product feel this seems like a big win to me. would be a nifty addition for Rails 7.1!
As I haven't generated the guides to tophat a change in a while can someone link me the quick TLDR on tophatting this locally? Thanks!
When going through the demo - I think one of the guides strengths already is to navigate via headers.
But what I could really use is searching by text - e.g. form_with
returns nothing. How hard would it be to search text body so I can search code snippets to find the right guide?
Also 10/10 for giving us a working demo! |
https://guides.rubyonrails.org/ruby_on_rails_guides_guidelines.html#html-guides-generation |
9e0bded
to
180374d
Compare
For better discoverability and to prevent users from having to leave the Guides webiste, this commit implements a client side search based on the open lunr library. Different libraries were compared: * lucaong/minisearch, where it was not possible to pre-build an index. Building it on the fly would take about 3 seconds and render the browser unresponsive. * krisk/Fuse, where it was possible to pre-build an index, but the fuzzy search was very slow and would block for about 600ms. * olivernn/lunr.js, where the index cloud be pre-built and the fuzzy search could be disabled. The new search uses Lunr with a pre-built index and by using a lookup table where the id of the documents are mapped to their respective url to keep the index size small. To build the index, the Guides HTML is parsed and split into documents that are then indexed using ExecJS. This generates an index of about 4.8 MB in size which is only loaded when the user focuses the search input. The index is then stored in the browsers localstorage for up to a week for performance.
180374d
to
4e4e025
Compare
@Schwad I added the code blocks to the index in the above demo. You need to invalidate the cache for the new index to load by typing |
@LukasSkywalker nice! 🙌 |
For consistency I think it would be nice if it used the same styles as the index dropdown. We could probably also remove the "Contribute" item from the menu as it's already present in the top menu. |
There is a version of the guide for Brazilian Portuguese where this search is already implemented, maybe it will be a good reference. |
One idea that hasn't been mentioned here yet is the way Ruby on Rails API implements search functionality: https://api.rubyonrails.org/. Right now I'll have to use the Google web search operators f.ex. site:guides.rubyonrails.org destroy. |
This is great and I would definitely implement this and make a pull request to this repository. The translation however is not forked and (thus) not licensed, so the contributors own the copyright to the search implementation. |
For better discoverability and to prevent users from having to leave
the Guides webiste, this commit implements a client side search based
on the open lunr library.
Different libraries were compared:
Building it on the fly would take about 3 seconds and render the
browser unresponsive.
fuzzy search was very slow and would block for about 600ms.
search could be disabled.
The new search uses Lunr with a pre-built index and by using a lookup
table where the id of the documents are mapped to their respective url
to keep the index size small.
To build the index, the Guides HTML is parsed and split into documents
that are then indexed using ExecJS. This generates an index of about
3.3 MB in size which is only loaded when the user focuses the search
input.
The index is then stored in the browsers localstorage for up to a week
for performance.
Demo here
Summary
Other Information
References: