Skip to content
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

feat(search): allow complete section customization #799

Merged
merged 5 commits into from
Jul 5, 2022

Conversation

james-d-elliott
Copy link
Contributor

@james-d-elliott james-d-elliott commented Jun 25, 2022

Summary

This allows configuring the searchable sections and which sections show the search box. In addition the javascript is only included on pages where the search box is visible.

Basic example

Defaults

image

searchSectionsIndex set to ["docs", "blog"]

image

searchSectionsShow set to ["docs"]

image

image

Motivation

Closes #630

Checks

  • Read Create a Pull Request
  • Supports all screen sizes (if relevant)
  • Supports both light and dark mode (if relevant)
  • Passes npm run test

ToDo

  • Checks for searchSectionsShow / searchSectionsIndex being undefined (currently causes error calling len: reflect: call of reflect.Value.Type on zero Value) fa01f4a
  • Decide on a method for handling the main page for searchSectionsShow
  • Implement ALL options b532c69

This allows configuring the searchable sections and which sections show the search box. In addition the javascript is only included on pages where the search box is visible.
@james-d-elliott
Copy link
Contributor Author

james-d-elliott commented Jun 25, 2022

To clarify the intended/expected behavior:

  1. HomePage is a special pseudo-section which refers to the index / home page.
  2. Search Box Visibility is configured via the Param options.searchSectionsShow which can be a string with the value ALL or a slice of section names.
  3. Search Indexed sections can be configured via the Param options.searchSectionsIndex which can be a string with the value ALL or a slice of section names.
  4. The ALL option includes the HomePage. If users wish to exclude it they need to use a slice with all relevant section names.
  5. By default all pages should show the search box.
  6. By default only the docs section should be indexed.
  7. The default behavior is present with an empty slice or if the respective Param is commented or otherwise does not exist.
  8. The flexSearch javascript should only load on pages that have the search box.

@james-d-elliott
Copy link
Contributor Author

For reference if interested, I've got a modified version of this which can exclude individual pages from the index:

---
title: "A Title"
description: "A Description"
lead: ""
date: 2022-03-20T12:52:27+11:00
draft: false
images: []
weight: 600
search:
  index: false
---
  {{ $list := slice }}
  {{- if and (isset .Site.Params.options "searchsectionsindex") (not (eq (len .Site.Params.options.searchSectionsIndex) 0)) }}
  {{- if eq .Site.Params.options.searchSectionsIndex "ALL" }}
  {{- $list = .Site.Pages }}
  {{- else }}
  {{- $list = (where (where .Site.Pages "Type" "in" .Site.Params.options.searchSectionsIndex) ".Params.search.index" "!=" "false") }}
  {{- if (in .Site.Params.options.searchSectionsIndex "HomePage") }}
  {{ $list = $list | append .Site.Home }}
  {{- end }}
  {{- end }}
  {{- else }}
  {{- $list = (where .Site.Pages "Section" "docs") }}
  {{- end }}

  {{ $len := (len $list) -}}

@h-enk
Copy link
Member

h-enk commented Jun 28, 2022

That certainly is a nice one!

Copy link
Member

@h-enk h-enk left a comment

Choose a reason for hiding this comment

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

For now, I'd like to go forward w/ Doks v0.5, so I'll merge your work. See also #816

@h-enk h-enk merged commit 58d77b2 into thuliteio:master Jul 5, 2022
@james-d-elliott james-d-elliott deleted the feat-search-sections branch July 5, 2022 09:24
@StefanSalewski
Copy link

Unfortunately, for my custom, doc-like sections created with commands like "npm run create -- --kind docs [section]" it does not work. Search box visibility works, but actual search in books does not work. Strings "ALL" seems to not work at all. I have section names like "books" and "faq", and no "doks" at all. Name "doks" makes not much sense for my use case. Blog search works.

  flexSearch = true
  searchSectionsShow = ["blog", "books"]
  searchSectionsIndex = ["blog", "books"]

@h-enk
Copy link
Member

h-enk commented Apr 15, 2023

See #395 (comment)

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.

Add site wide search as an option
3 participants