How I built search-as-you-type UX with Sphinx's built-in client-side JS search tools #13222
kaycebasques
started this conversation in
Show and tell
Replies: 1 comment
-
It's so cool and creative! If you don't mind, I've added your built-in search customization as an outstanding example to my older post about implementing search to Sphinx themes at https://documatt.com/blog/25/sphinx-builtin-search-theme/. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
On pigweed.dev I managed to hack together a search-as-you-type UX using only Sphinx's built-in client-side JS search logic. As you type stuff into the search query textbox, you see inline results under your query:
366233420-5dcd1b9f-2ad6-4457-aca6-acb4286fe262.webm
I have heard that other theme maintainers have wanted something similar but couldn't figure out how to get it working. So I thought it might be helpful to share the general outline of the implementation here. I also want to make the upstream Sphinx maintainers aware of this work because it requires extending
searchtools.js
a bit beyond its assumed usage.I'm in the process of upstreaming the pigweed.dev implementation into pydata-sphinx-theme. Here's the PR: pydata/pydata-sphinx-theme#2093
Implementation
Extend
basic
themeIt seems like
searchtools.js
(the file containing upstream Sphinx's client-side JS search logic) is only available if your theme extends basic.Update layout to always load searchtools.js
Update your main HTML layout template to (almost) always load
searchtools.js
and the other search-related files.Listen for searches, kick off the search, make sure the results container exists
The actual search logic is completely offloaded to
searchtools.js
. You just need to kick off the search and make sure thatsearchtools.js
has somewhere to render the results. There is some complexity around making sure that the search result URLs are correct. The full WIP impl for pydata-sphinx-theme is below.Suggestions for upstream Sphinx
A lot of the implementation complexity revolves around making sure that the search results URLs are correct. The
Search
class insearchtools.js
assumes that searches are only executed from/search.html
whereas now it's possible to search from any page on the site. I think theSearch
class constructor should accept a new argument indicating what page the search is currently executing from, and thenSearch
class itself should take care of making sure that the relative URLs are correct.searchtools.js
assumes that a#search-results
node exists and populates the results into that container. It might be better to haveSearch
accept another new parameter indicating where the search results should be populated into.The most flexible option would probably be some kind of pub/sub API where upstream Sphinx just blasts off events while it conducts the search and then the theme maintainer has complete freedom to render the search results however they see fit based off the underlying data.
Beta Was this translation helpful? Give feedback.
All reactions