Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Feature Request: Add a search bar or a tutorial on how to add one. #465

Open
AribYadi opened this issue May 8, 2023 · 2 comments
Open

Comments

@AribYadi
Copy link

AribYadi commented May 8, 2023

I need a search bar or a search page on my blog. If you don't want to add one, can you make a tutorial on how to add one?

@blakeashleyjr
Copy link
Contributor

Hi @AribYadi, because Hugo produces a static website, dynamic functionality like search would have to generally have to be provided by an external provider like Elasticsearch, Solr, or Algolia.

There are some client-side search options you can look at: https://github.com/nextapps-de/flexsearch

@ben-krieger
Copy link

I wouldn't say I love the content matching algorithm, but this was a simple solution that looks quite nice with this theme.

Add hugomods/search:

> hugo mod get github.com/hugomods/search@latest

Add to config.toml:

# Navbar menu item
[[languages.en.menu.main]]
  identifier = "search"
  name = "Search (^K)"
  url = "/search"

# Search mod config
[params.search]
  min_match_char_length = 3
  ignore_location = true
  threshold = 0
  distance = 0
  index_content = true
  index_all_pages = false
  expand_results_meta = true

# Output search index for modal and search page
[outputs]
  home = ['HTML', 'Search', 'SearchIndex']

# Add module
[[module.imports]]
  path = 'github.com/hugomods/search'

Add JS/CSS to layouts/partials/extended_footer.html:

{{ partial "search/assets/js" . }}
{{ partial "search/assets/css" . }}

<!-- NOTE: Styling is configured for the green theme -->
<style>
:root {
  --search-primary: rgb(120, 226, 160);
  --search-color: rgba(120, 226, 160);
  --search-bg: rgb(31, 34, 42);
  --search-result-bg: rgb(31, 34, 42);
  --search-border-color: rgb(120, 226, 160);
} 
</style>

Create a search page (for when not using the modal via keyboard shortcut) by adding layouts/index.search.html:

{{ define "main" }}
<div class="search-container"></div>
{{ end }}

If this is helpful, I can create a doc. And if anyone has better search algorithms or parameters, let me know!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants