search: create search index and page from template and content adapter #888

This commit is contained in:
Sören Weber 2024-08-26 23:07:33 +02:00
parent e1a1f01f4c
commit d9e69db2f1
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
18 changed files with 129 additions and 103 deletions

View file

@ -0,0 +1,6 @@
+++
[_build]
render = "never"
list = "never"
publishResources = false
+++

View file

@ -0,0 +1,16 @@
{{- if and (not .Site.Params.disableSearchIndex) (not .Site.Params.disableSearchPage) }}
{{- .EnableAllLanguages }}
{{- $url := trim (or .Site.Params.searchPageURL "search") "/" }}
{{- $content := dict
"mediaType" "text/markdown"
"value" "{{% _relearn_searchform %}}"
}}
{{- $page := dict
"content" $content
"kind" "page"
"path" "_relearn_searchpage"
"title" (T "Search")
"url" $url
}}
{{- .AddPage $page }}
{{- end }}

View file

@ -0,0 +1,6 @@
+++
[_build]
render = "never"
list = "never"
publishResources = false
+++

View file

@ -32,12 +32,9 @@ title = "Hugo Relearn Theme"
summaryLength = 10
[outputs]
# add `search` to the home to support Lunr search; This is a mandatory setting
# for the side wide search functionality
# add `searchpage` to the home to create a dedicated searchpage
# add `print` to home, section and page to activate the feature to print whole
# chapters
home = ["html", "rss", "print", "search", "searchpage"]
home = ["html", "rss", "print"]
section = ["html", "rss", "print"]
page = ["html", "rss", "print"]

View file

@ -98,15 +98,6 @@ disableGeneratorVersion = false
# may not function correctly anymore.
disableRandomIds = false
# Multilanguage content.
# Default: not set
# If your pages contain further languages besides the main one used, add all
# those auxiliary languages here. This will create a search index with
# support for all used languages of your site.
# This is handy for example if you are writing in Spanish but have lots of
# source code on your page which typically uses English terminology.
additionalContentLanguage = [ "en" ]
# Additional code dependencies.
# Default: See hugo.toml of the theme
# The theme provides a mechanism to load further JavaScript and CSS
@ -147,18 +138,57 @@ disableNextPrev = false
# in the pages frontmatter. This is useful if you want to give the opportunity
# for people to create merge request for your content.
editURL = "https://github.com/McShelby/hugo-theme-relearn/edit/main/exampleSite/content/${FilePath}"
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Search
# These options modify various aspects of the search functionality.
# Disable the search.
# Default: false
# If the search is disabled, no search box will be displayed in the menu,
# nor in-page search, search popup or dedicated search page will be available.
# This will also cause the keyboard shortcut to be disabled.
disableSearch = false
# Disable the search index generation.
# Default: false
# `disableSearch=false` must be set to let the generation of the search index
# file to be affected by this option. If the search index is disabled, no
# search popup or dedicated search page will be available.
disableSearchIndex = false
# URL of the search index file relative to the language home.
# Default: "search_index.js"
# You have to set this option if your page already has a content file named
# `search_index.js` in the language home.
searchIndexURL = "search_index.js"
# Disable the dedicated search page.
# Default: false
# `disableSearch=false` and `disableSearchIndex=false` must be set to let the
# generation of the dedicated search page to be affected by this option.
disableSearchPage = false
# URL of the dedicated search page relative to the language home.
# Default: "search"
# In its basic form the search page URL is named the same for all languages
# but you are free to override it in each language options to localised the
# URL. You also need to set this option if your page already has a content
# page named `search`.
searchPageURL = "search"
# Multilanguage content.
# Default: not set
# If the search index is enabled and your pages contain further languages
# besides the main one used, add all those auxiliary languages here. This
# will create a search index with support for all used languages of your site.
# This is handy for example if you are writing in Spanish but have lots of
# source code on your page which typically uses English terminology.
additionalContentLanguage = [ "en" ]
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Menu
# These options modify the menu appearance.
# Hide the search box.
# Default: false
# If the search box is disabled, the search functionality is disabled too.
# This will also cause the keyboard shortcut to be disabled and the dedicated
# search page is not linked although it mighty be configured.
disableSearch = false
# Hide the Home entry.
# Default: false
# If shown, a Home button will appear below the search bar and the main menu.

View file

@ -61,7 +61,7 @@ If not already present, add the following lines in your `hugo.toml` file.
{{< multiconfig file=hugo >}}
[outputs]
home = ["html", "rss", "search"]
home = ["html", "rss"]
{{< /multiconfig >}}
This will generate a search index file at the root of your public folder ready to be consumed by the Lunr search library.
@ -72,7 +72,7 @@ You can add a dedicated search page for your page by adding the `searchpage` out
{{< multiconfig file=hugo >}}
[outputs]
home = ["html", "rss", "search", "searchpage"]
home = ["html", "rss"]
{{< /multiconfig >}}
You can access this page by either clicking on the magnifier glass or by typing some search term and pressing `ENTER` inside of the menu's search box .
@ -91,7 +91,7 @@ You can activate print support to add the capability to print whole chapters or
{{< multiconfig file=hugo >}}
[outputs]
home = ["html", "rss", "print", "search"]
home = ["html", "rss", "print"]
section = ["html", "rss", "print"]
page = ["html", "rss", "print"]
{{< /multiconfig >}}

View file

@ -15,22 +15,6 @@
permalinkable = false
noUgly = true
[outputFormats.search]
name= "search"
baseName = "index.search"
isPlainText = true
mediaType = 'text/javascript'
permalinkable = false
noUgly = true
[outputFormats.searchpage]
name= "searchpage"
baseName = "search"
isHTML = true
mediaType = 'text/html'
permalinkable = false
noUgly = true
[params.relearn.dependencies]
[params.relearn.dependencies.mathjax]
name = "MathJax"

View file

@ -1,2 +0,0 @@
{{- $ret := path.Base (strings.TrimSuffix (path.Ext .) .) }}
{{- return $ret }}

View file

@ -1,12 +1,6 @@
<!DOCTYPE html>
{{- $format := partial "get-format.hugo" . }}
{{- $outputFormat := partial "output-format.hugo" (dict "page" . "format" $format) }}
{{- $basename := "index" }}
{{- if eq .Kind "home" }}
{{- if eq $outputFormat "searchpage" }}
{{- $basename = partial "BaseName.hugo" $format.RelPermalink }}
{{- end }}
{{- end }}
{{- if eq . .Site.Sites.First.Home }}
{{- $hugoVersion := "0.121.0" }}
{{- if lt hugo.Version $hugoVersion }}
@ -27,9 +21,9 @@
{{- if .IsTranslated -}}
{{- range $index, $trans := .AllTranslations }}
{{- if eq $index 0 }}
<link href="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true "basename" $basename) }}" rel="alternate" hreflang="x-default">
<link href="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true) }}" rel="alternate" hreflang="x-default">
{{- end }}
<link href="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true "basename" $basename) }}" rel="alternate" hreflang="{{ .Language.LanguageCode }}">
<link href="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true) }}" rel="alternate" hreflang="{{ .Language.LanguageCode }}">
{{- end }}
{{- end }}
@ -37,10 +31,8 @@
{{- else }}
{{- range .AlternativeOutputFormats }}
{{- if eq .Rel "canonical" }}
{{ (printf $link (partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true "basename" $basename)) .Rel .MediaType.Type ($title | htmlEscape)) | safeHTML }}
{{- else if eq $outputFormat "searchpage" }}
{{- else if eq .Name "searchpage" }}
{{- else if ne .Name "search" }}
{{ (printf $link (partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true)) .Rel .MediaType.Type ($title | htmlEscape)) | safeHTML }}
{{- else }}
{{ (printf $link (partial "relLangPrettyUglyURL.hugo" (dict "to" .)) .Rel .MediaType.Type ($title | htmlEscape)) | safeHTML }}
{{- end }}
{{- end }}

View file

@ -1,13 +1,5 @@
{{- $showvisitedlinks := .Site.Params.showVisitedLinks }}
{{- $basename := "index" }}
{{- if eq .Kind "home" }}
{{- $format := partial "get-format.hugo" . }}
{{- $outputFormat := partial "output-format.hugo" (dict "page" . "format" $format) }}
{{- if eq $outputFormat "searchpage" }}
{{- $basename = partial "BaseName.hugo" $format.RelPermalink }}
{{- end }}
{{- end }}
<aside id="R-sidebar" class="default-animation{{ if $showvisitedlinks }} showVisitedLinks{{ end }}">
{{- $currentNode := . }}
<div id="R-header-topbar" class="default-animation"></div>
@ -15,9 +7,7 @@
<div id="R-header" class="default-animation">
{{ partial "logo.html" . }}
</div>
{{- if not .Site.Params.disableSearch }}
{{ partial "search.html" . }}
{{- end }}
</div>
<div id="R-homelinks" class="default-animation{{ if not .Site.Params.disableLandingPageButton }} homelinks{{ end }}">
{{- if not .Site.Params.disableLandingPageButton }}
@ -79,7 +69,7 @@
{{- $page := .Page }}
{{- $pageLang := .Page.Language.Lang }}
{{- range .Page.AllTranslations }}
<option id="R-select-language-{{ .Language.Lang }}" value="#R-select-language-{{ .Language.Lang }}" data-url="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "basename" $basename) }}" lang="{{ .Language.LanguageCode }}"{{ if eq $pageLang .Language.Lang }} selected{{ end }}>{{ .Language.LanguageName }}</option>
<option id="R-select-language-{{ .Language.Lang }}" value="#R-select-language-{{ .Language.Lang }}" data-url="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" .) }}" lang="{{ .Language.LanguageCode }}"{{ if eq $pageLang .Language.Lang }} selected{{ end }}>{{ .Language.LanguageName }}</option>
{{- end }}
</select>
</div>

View file

@ -1,13 +1,5 @@
{{- /* based on Hugo 0.125.5 opengraph.html */}}
{{- $basename := "index" }}
{{- if eq .Kind "home" }}
{{- $format := partial "get-format.hugo" . }}
{{- $outputFormat := partial "output-format.hugo" (dict "page" . "format" $format) }}
{{- if eq $outputFormat "searchpage" }}
{{- $basename = partial "BaseName.hugo" $format.RelPermalink }}
{{- end }}
{{- end }}
<meta property="og:url" content="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true "basename" $basename) }}">
<meta property="og:url" content="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true) }}">
{{- with site.Title | plainify }}
<meta property="og:site_name" content="{{ . }}">

View file

@ -11,14 +11,6 @@
{{- if eq .Kind "home" }}
{{- $title = $title | default $siteTitle }}
{{- $outputFormat := $.outputFormat }}
{{- if not $outputFormat }}
{{- $format := $.format | default (partial "get-format.hugo" .) }}
{{- $outputFormat = $.outputFormat | default (partial "output-format.hugo" (dict "page" . "format" $format)) }}
{{- end }}
{{- if eq $outputFormat "searchpage" }}
{{- $title = T "Search" }}
{{- end }}
{{- else if eq .Kind "taxonomy" }}
{{- $title = default (default .Data.Plural (i18n .Data.Plural)) .Params.Title }}
{{- else if eq .Kind "term" }}

View file

@ -1,12 +1,26 @@
{{- if not .Site.Params.disableSearch }}
{{- $assetBusting := partialCached "assetbusting.hugo" . }}
{{- $link := "" }}
{{- with .Site.Home.OutputFormats.Get "json" }}
{{- warnf "UNSUPPORTED usage of 'json' output format found, use 'search' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#600" }}
{{- end }}
{{- if .Site.Home.OutputFormats.Get "search" }}
{{- with .Site.Home.OutputFormats.Get "searchpage" }}
{{- warnf "UNSUPPORTED usage of 'searchpage' output format found, remove it from your config; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#700" }}
{{- end }}
{{- if not .Site.Params.disableSearchIndex }}
{{- $url := path.Join .Site.LanguagePrefix (trim (or .Site.Params.searchIndexURL "search_index.js") "/") }}
{{- $templateres := resources.Get "_relearn_search_index.js" }}
{{- $resultres := $templateres | resources.ExecuteAsTemplate $url .Site.Home }}
{{- /* the following code causes Hugo to generate our file - although it is in comments */}}<!-- {{ $resultres.RelPermalink }} -->
<script>
window.index_js_url={{ (printf "%s%s" $url $assetBusting) | relLangURL }};
</script>
{{- if not .Site.Params.disableSearchPage }}
{{- with .Site.GetPage "_relearn_searchpage" }}
{{- $link = partial "relLangPrettyUglyURL.hugo" (dict "to" .) }}
{{- end }}
{{- end }}
{{- end }}
<search>{{ if $link }}<form action="{{ $link }}" method="get">{{ end }}
<div class="searchbox default-animation">
{{ if $link }}<button class="search-detail" type="submit" title="{{ T "Search" }} (CTRL+ALT+f)">{{ end }}<i class="fas fa-search"{{ if not $link }} title="{{ T "Search" }} (CTRL+ALT+f)"{{ end }}></i>{{ if $link }}</button>{{ end }}
@ -15,7 +29,6 @@
<button class="search-clear" type="button" data-search-clear="" title="{{ T "Clear-search" }}"><i class="fas fa-times" title="{{ T "Clear-search" }}"></i></button>
</div>
{{ if $link }}</form>{{ end }}</search>
{{- $assetBusting := partialCached "assetbusting.hugo" . }}
{{- $pageBaseLang := replaceRE "([a-z]+).*" "${1}" .Page.Language.LanguageCode }}
{{- $contentlangs := (union (slice | append (.Site.Params.additionalContentLanguage | default slice)) (slice $pageBaseLang)) }}
{{- $quotedcontentlangs := slice }}
@ -51,3 +64,4 @@
<script src="{{ $file | relURL}}{{ $assetBusting }}" defer></script>
{{- end }}
<script src="{{ "js/search.js" | relURL }}{{ $assetBusting }}" defer></script>
{{- end }}

View file

@ -32,9 +32,6 @@
window.relearn.absBaseUri='{{ replaceRE "/*$" "" .Site.BaseURL | safeJS }}';
window.relearn.disableAnchorCopy={{ printf "%t" (eq .Site.Params.disableAnchorCopy true) | safeJS }};
window.relearn.disableAnchorScrolling={{ printf "%t" (eq .Site.Params.disableAnchorScrolling true) | safeJS }};
{{- with .Site.Home.OutputFormats.Get "search" }}
window.index_js_url={{ (printf "index.search.js%s" $assetBusting) | relLangURL }};
{{- end }}
{{ "// variant stuff" | safeJS }}
{{- $quotedthemevariants := slice }}
{{- range $themevariants }}

View file

@ -9,13 +9,7 @@
{{- $endarrow = "🡐" }}
{{- end }}
{{- $next := .Scratch.Get "relearnNextPage" }}
{{- if eq .Page.Kind "home" }}
{{- $format := partial "get-format.hugo" .Page }}
{{- $outputFormat := partial "output-format.hugo" (dict "page" .Page "format" $format) }}
{{- if eq $outputFormat "searchpage" }}
{{- $next = "" }}
{{- end }}
{{- else if eq .Page.Kind "term" }}
{{- if eq .Page.Kind "term" }}
{{- /* go to next term page */}}
{{- $taxonomy_page := .Site.GetPage .Data.Plural }}
{{- $pages := partialCached "partials/pageHelper/taxonomyPages.html" $taxonomy_page $taxonomy_page }}

View file

@ -9,13 +9,7 @@
{{- $startarrow = "🡒" }}
{{- end }}
{{- $prev := .Scratch.Get "relearnPrevPage" }}
{{- if eq .Page.Kind "home" }}
{{- $format := partial "get-format.hugo" .Page }}
{{- $outputFormat := partial "output-format.hugo" (dict "page" .Page "format" $format) }}
{{- if eq $outputFormat "searchpage" }}
{{- $prev = .Site.Home }}
{{- end }}
{{- else if eq .Page.Kind "term" }}
{{- if eq .Page.Kind "term" }}
{{- /* go to previous term page or taxonomy page if it is the first term */}}
{{- $taxonomy_page := .Site.GetPage .Data.Plural }}
{{- $pages := partialCached "partials/pageHelper/taxonomyPages.html" $taxonomy_page $taxonomy_page }}

View file

@ -0,0 +1,24 @@
{{/*
This is an internal shortcode used for the dedicated search page;
it is undocumented and not be meant to be used in your content files!
*/ -}}
<search>
<form action="javascript:triggerSearch()">
<div class="searchform">
<label class="a11y-only" for="R-search-by-detail">{{ T "Search" }}</label>
<input data-search-input id="R-search-by-detail" class="search-by" name="search-by" type="search" placeholder="{{ T "Search-placeholder" }}">
{{- partial "shortcodes/button.html" (dict
"page" .Page
"type" "submit"
"style" "secondary"
"icon" "search"
"content" (T "Search")
)}}
</div>
</form>
</search>
<div class="searchhint">
</div>
<hr>
<div id="R-searchresults">
</div>