Skip to content

Offline search with Lunr.js#142

Merged
LisaFC merged 8 commits intogoogle:masterfrom
theletterf:offline-search-lunr
Nov 13, 2019
Merged

Offline search with Lunr.js#142
LisaFC merged 8 commits intogoogle:masterfrom
theletterf:offline-search-lunr

Conversation

@theletterf
Copy link
Copy Markdown
Contributor

@theletterf theletterf commented Oct 23, 2019

Hi there!

As mentioned in #45, I added offline search with Lunr.js, borrowing heavily from existing tutorials.

How to enable offline search

  1. Add offlineSearch = true to the main config.toml file.
  2. Add JSON to the outputs for home in the main config.toml file:
[outputs]
	home = [ "HTML", "JSON"]

That's it. Upon build, pages will be indexed into an index.json file by Lunr.js. The whole process is invoked by static/js/offline-search.js, which also handles instant results (thanks @mwalters!).

Styling for the offline search field lives in /static/css/offline-search.css.

Current limitation: I didn't implement a search results page, but it can be done.

@theletterf
Copy link
Copy Markdown
Contributor Author

@LisaFC: Not sure why the checks are failing. :-/

@LisaFC
Copy link
Copy Markdown
Collaborator

LisaFC commented Oct 24, 2019

Ooh, thanks for that. I'll see if I can figure out why the checks are failing.

@LisaFC
Copy link
Copy Markdown
Collaborator

LisaFC commented Oct 24, 2019

Ok, looks like it was some Netlify weirdosity, I manually reran the deploy and it now works fine! Will take a proper look at the change now.

Copy link
Copy Markdown
Collaborator

@LisaFC LisaFC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Once this is in I can add a search-index.md to the example site so it'll actually work if users enable it.

@theletterf
Copy link
Copy Markdown
Contributor Author

@LisaFC: all resolved. Only two issues remain:

@LisaFC
Copy link
Copy Markdown
Collaborator

LisaFC commented Nov 13, 2019

@LisaFC: all resolved. Only two issues remain:

Fantastic!

Answered there, but just to reiterate:

"Aaaaaah, right, I see what's going on. So as you've written it here, it means that the top search field only displays if the sidebar search is disabled (which is what you want for local search and Algolia), but they actually both work fine displayed at the same time if you're using a Google custom search (as you can see on docsy.dev) because it doesn't have an instant results feature.

At the moment for Algolia search we just tell people to disable the sidebar search as it doesn't currently work with Algolia, I think for now we can just do the same for local search. I'll have a think for a future iteration whether we can make the sidebar search automatically disappear if you're using anything except Google custom search."

  • Should we add search-index.md to the docsy repo?

We should add it to the user guide and example sites (so for the user guide in this repo, add userguide/content/en/search-index.md)

@theletterf
Copy link
Copy Markdown
Contributor Author

@LisaFC Done - and PR opened for search-index.md in the demo site repository: google/docsy-example#33

@LisaFC
Copy link
Copy Markdown
Collaborator

LisaFC commented Nov 13, 2019

Brilliant, thanks so much for doing this, I think the Docsy community will find this super useful!

@LisaFC LisaFC merged commit 5fc6fb9 into google:master Nov 13, 2019
@LisaFC
Copy link
Copy Markdown
Collaborator

LisaFC commented Nov 15, 2019

Ta da! https://www.docsy.dev/docs/adding-content/navigation/#configure-local-search-with-lunr

@theletterf
Copy link
Copy Markdown
Contributor Author

Sweet! Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants