From d9e69db2f1d41dd4714a5536b9ba6d9c93c822f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Mon, 26 Aug 2024 23:07:33 +0200 Subject: [PATCH] search: create search index and page from template and content adapter #888 --- .../_relearn_search_index.js | 0 content/_relearn/_index.md | 6 ++ content/_relearn/searchpage/_content.gotmpl | 16 +++++ content/_relearn/searchpage/_index.md | 6 ++ exampleSite/config/_default/hugo.toml | 5 +- exampleSite/config/_default/params.toml | 62 ++++++++++++++----- .../content/basics/customization/_index.en.md | 6 +- hugo.toml | 16 ----- layouts/partials/BaseName.hugo | 2 - layouts/partials/header.html | 16 ++--- layouts/partials/menu.html | 12 +--- layouts/partials/opengraph.html | 10 +-- layouts/partials/pageHelper/title.hugo | 8 --- layouts/partials/search.html | 24 +++++-- layouts/partials/stylesheet.html | 3 - layouts/partials/topbar/button/next.html | 8 +-- layouts/partials/topbar/button/prev.html | 8 +-- layouts/shortcodes/_relearn_searchform.html | 24 +++++++ 18 files changed, 129 insertions(+), 103 deletions(-) rename layouts/_default/index.search.js => assets/_relearn_search_index.js (100%) create mode 100644 content/_relearn/_index.md create mode 100644 content/_relearn/searchpage/_content.gotmpl create mode 100644 content/_relearn/searchpage/_index.md delete mode 100644 layouts/partials/BaseName.hugo create mode 100644 layouts/shortcodes/_relearn_searchform.html diff --git a/layouts/_default/index.search.js b/assets/_relearn_search_index.js similarity index 100% rename from layouts/_default/index.search.js rename to assets/_relearn_search_index.js diff --git a/content/_relearn/_index.md b/content/_relearn/_index.md new file mode 100644 index 0000000000..3d92033868 --- /dev/null +++ b/content/_relearn/_index.md @@ -0,0 +1,6 @@ ++++ +[_build] + render = "never" + list = "never" + publishResources = false ++++ diff --git a/content/_relearn/searchpage/_content.gotmpl b/content/_relearn/searchpage/_content.gotmpl new file mode 100644 index 0000000000..3eb30dc69f --- /dev/null +++ b/content/_relearn/searchpage/_content.gotmpl @@ -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 }} \ No newline at end of file diff --git a/content/_relearn/searchpage/_index.md b/content/_relearn/searchpage/_index.md new file mode 100644 index 0000000000..3d92033868 --- /dev/null +++ b/content/_relearn/searchpage/_index.md @@ -0,0 +1,6 @@ ++++ +[_build] + render = "never" + list = "never" + publishResources = false ++++ diff --git a/exampleSite/config/_default/hugo.toml b/exampleSite/config/_default/hugo.toml index de428d9939..cac8e61b46 100644 --- a/exampleSite/config/_default/hugo.toml +++ b/exampleSite/config/_default/hugo.toml @@ -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"] diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml index 8af1356ecb..ee1aec258f 100644 --- a/exampleSite/config/_default/params.toml +++ b/exampleSite/config/_default/params.toml @@ -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. diff --git a/exampleSite/content/basics/customization/_index.en.md b/exampleSite/content/basics/customization/_index.en.md index 841e1a1f8d..5d1530d34a 100644 --- a/exampleSite/content/basics/customization/_index.en.md +++ b/exampleSite/content/basics/customization/_index.en.md @@ -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 >}} diff --git a/hugo.toml b/hugo.toml index 9afe9057c5..026cc8231e 100644 --- a/hugo.toml +++ b/hugo.toml @@ -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" diff --git a/layouts/partials/BaseName.hugo b/layouts/partials/BaseName.hugo deleted file mode 100644 index a383b7e602..0000000000 --- a/layouts/partials/BaseName.hugo +++ /dev/null @@ -1,2 +0,0 @@ -{{- $ret := path.Base (strings.TrimSuffix (path.Ext .) .) }} -{{- return $ret }} \ No newline at end of file diff --git a/layouts/partials/header.html b/layouts/partials/header.html index 384393beba..a8d34f034d 100644 --- a/layouts/partials/header.html +++ b/layouts/partials/header.html @@ -1,12 +1,6 @@ {{- $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 }} - + {{- end }} - + {{- 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 }} diff --git a/layouts/partials/menu.html b/layouts/partials/menu.html index d2d42fff15..66cd08e304 100644 --- a/layouts/partials/menu.html +++ b/layouts/partials/menu.html @@ -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 }}