Hugo live search using Fuse.js and Mark.js
This is not a standalone theme. This is a Hugo theme component. To use it:
-
Clone this repo to
your-site/themes/hugo-search-fuse-jsdirectory. Note that the theme name has to remainhugo-search-fuse-js. -
Add "hugo-search-fuse-js" as the leftmost element of the
themelist variable in your site's or theme'sconfig.toml. Example:theme = ["hugo-search-fuse-js", "my-theme"] -
Create
content/search.mdwithlayoutparameter set to "search", andoutputparameter set to["html", "json"]. The content or body of thissearch.mdis not used by thesearch.htmltemplate which is part of this theme component.Here is an example
search.md:+++ title = "Search" layout = "search" outputs = ["html", "json"] [sitemap] priority = 0.1 +++
Requirements
For this theme component to work as-is:
- Your main theme must be structured using base template and blocks, and
- The
basetemplate needs to definemainandfooterblocks (shown in that previous link).
If the main theme does not meet these requirements, you will need to edit the
search.html
partial to make search work. You can find it in layouts/_default/search.html in this component.
What if the main theme does not have baseof.html?
You have two options:
- Edit the
search.htmlto make it fit your theme. - Upgrade the theme to use the new "base template and blocks" approach.
What if the main theme has baseof.html, but doesn't have the main and/or footer blocks?
It's very easy to add those blocks without breaking your existing theme.
To add the main block, add this to your baseof.html at the
appropriate place:
{{ block "main" . }}{{ end }}
And similarly add the below if the footer block is missing:
{{ block "footer" . }}{{ end }}
For an example, see the baseof.html snippet in Hugo
docs.
Credits
- Search functionality by fuse.js.
- Keyword highlighting by mark.js.
- Initial Hugo implementation by @eddiewebb in this gist.
- Hugo theme component packaging by @kaushalmodi.
- jQuery removal and IE polyfill integration by @lpar.
- IE compatibility polyfills: