From 5a836e5d7cabfd80bcb00cefa5af8db03a0e1fc0 Mon Sep 17 00:00:00 2001 From: Razon Yang Date: Sat, 26 Oct 2024 14:49:53 +0800 Subject: [PATCH] fix: import github.com/hugomods/search-index (#275) --- hugo.toml | 9 +-- layouts/_default/index.searchindex.json | 1 - .../partials/search/assets/js-resource.html | 2 +- layouts/partials/search/functions/pages.html | 5 +- .../search/functions/parse-headings.html | 6 +- .../search/functions/search-index-hash.html | 10 +-- .../search/functions/walk-headings.html | 20 +----- layouts/partials/search/functions/years.html | 20 +----- layouts/partials/search/index.json.html | 68 ------------------- 9 files changed, 9 insertions(+), 132 deletions(-) delete mode 100644 layouts/_default/index.searchindex.json delete mode 100644 layouts/partials/search/index.json.html diff --git a/hugo.toml b/hugo.toml index 1dfedac592a..d31c89772fe 100644 --- a/hugo.toml +++ b/hugo.toml @@ -7,17 +7,14 @@ isHTML = true mediaType = 'text/html' notAlternative = true -[outputFormats.SearchIndex] -baseName = 'search' -isPlainText = true -mediaType = 'application/json' -notAlternative = true - [module] [module.hugoVersion] extended = true min = "0.128.0" +[[module.imports]] +path = "github.com/hugomods/search-index" + [[module.imports]] path = "github.com/hugomods/i18n-js" diff --git a/layouts/_default/index.searchindex.json b/layouts/_default/index.searchindex.json deleted file mode 100644 index 062a9dd51a6..00000000000 --- a/layouts/_default/index.searchindex.json +++ /dev/null @@ -1 +0,0 @@ -{{- partialCached "search/index.json" . -}} diff --git a/layouts/partials/search/assets/js-resource.html b/layouts/partials/search/assets/js-resource.html index 2b1358be680..4efdb596ba3 100644 --- a/layouts/partials/search/assets/js-resource.html +++ b/layouts/partials/search/assets/js-resource.html @@ -36,7 +36,7 @@ {{- range .Sites }} {{- $site := . }} {{- with .Home.OutputFormats.Get "SearchIndex" }} - {{- $hash := partialCached "search/functions/search-index-hash" $site $site }} + {{- $hash := partialCached "search-index/functions/hash" $site $site }} {{- $indexURL := printf "%s?v=%s" .Permalink $hash }} {{- $indices = $indices | append $indexURL }} {{- end }} diff --git a/layouts/partials/search/functions/pages.html b/layouts/partials/search/functions/pages.html index 328bf112a39..d4cb004e4d8 100644 --- a/layouts/partials/search/functions/pages.html +++ b/layouts/partials/search/functions/pages.html @@ -1,4 +1 @@ -{{- $indexAllPages := default true site.Params.search.index_all_pages -}} -{{- $pages := cond $indexAllPages site.Pages site.RegularPages -}} -{{- $pages = where $pages "Params.noindex" "ne" true -}} -{{- return $pages }} +{{- return partial "search-index/functions/pages" . }} diff --git a/layouts/partials/search/functions/parse-headings.html b/layouts/partials/search/functions/parse-headings.html index 46a8a52c08c..c8e235427f5 100644 --- a/layouts/partials/search/functions/parse-headings.html +++ b/layouts/partials/search/functions/parse-headings.html @@ -1,5 +1 @@ -{{- $headings := slice }} -{{- with .Fragments }} - {{- $headings = partial "search/functions/walk-headings" (dict "Headings" .Headings "PID" -1) }} -{{- end }} -{{- return $headings -}} +{{- return partial "search-index/functions/parse-headings" . }} diff --git a/layouts/partials/search/functions/search-index-hash.html b/layouts/partials/search/functions/search-index-hash.html index 2ef58c16668..fb0215ae63a 100644 --- a/layouts/partials/search/functions/search-index-hash.html +++ b/layouts/partials/search/functions/search-index-hash.html @@ -1,9 +1 @@ -{{/* This hashing function just calculate the number of pages and the lastmod time. */}} -{{- $ctx := dict "Site" . }} -{{- $pages := partialCached "search/functions/pages" $ctx .Language.Lang -}} -{{- $hash := printf "total=%d" $pages.Len }} -{{- $latest := first 1 $pages.ByLastmod.Reverse }} -{{- with index $latest 0 }} - {{- $hash = printf "%s&lastmod=%d" $hash .Lastmod.Unix }} -{{- end }} -{{- return ($hash | md5) }} +{{- return partial "search-index/functions/hash" . }} diff --git a/layouts/partials/search/functions/walk-headings.html b/layouts/partials/search/functions/walk-headings.html index 63bd49f15ba..819cb14bbd2 100644 --- a/layouts/partials/search/functions/walk-headings.html +++ b/layouts/partials/search/functions/walk-headings.html @@ -1,19 +1 @@ -{{- $v := slice }} -{{- $pid := .PID }} -{{- range .Headings }} - {{- if gt .Level (default 6 site.Params.search.headings_end_level) }} - {{- break }} - {{- end }} - {{- if ne .Title "" }} - {{- $v = $v | append (dict - "pid" $pid - "anchor" .ID - "title" (.Title | plainify)) - }} - {{- end }} - {{- $v = $v | append (partial "search/functions/walk-headings" (dict - "Headings" .Headings - "PID" (add $pid (len $v)))) - }} -{{- end }} -{{- return $v }} +{{- return partial "search-index/functions/walk-headings" . }} diff --git a/layouts/partials/search/functions/years.html b/layouts/partials/search/functions/years.html index 308fb48996a..b6e37a8fb0d 100644 --- a/layouts/partials/search/functions/years.html +++ b/layouts/partials/search/functions/years.html @@ -1,19 +1 @@ -{{- $years := slice }} -{{- if default true site.Params.search.filter_years }} - {{- $indexAllPages := default true site.Params.search.index_all_pages }} - {{- $pages := site.AllPages }} - {{- if not $indexAllPages }} - {{- $pages = where $pages "Kind" "page" }} - {{- end }} - {{- $pages = where $pages "Params.noindex" "ne" true }} - {{- with $pages.GroupByDate "2006" }} - {{- range . }} - {{- if eq .Key "0001" }} - {{- continue }} - {{- end }} - {{- $years = $years | append .Key }} - {{- end }} - {{- $years = sort $years }} - {{- end }} -{{- end }} -{{- return $years }} +{{- return partial "search-index/functions/years" }} diff --git a/layouts/partials/search/index.json.html b/layouts/partials/search/index.json.html deleted file mode 100644 index d584868bdbf..00000000000 --- a/layouts/partials/search/index.json.html +++ /dev/null @@ -1,68 +0,0 @@ -{{- $items := slice -}} -{{- $indexContent := default false site.Params.search.index_content -}} -{{- $filterYears := default true site.Params.search.filter_years }} -{{- $pages := partial "search/functions/pages" . -}} -{{- range $pages -}} - {{- $page := . -}} - {{- $item := newScratch -}} - {{- $item.Set "title" .Title -}} - {{- if default true site.Params.search.index_summary -}} - {{- $item.Set "summary" (default .Summary .Description | plainify | htmlUnescape) -}} - {{- end -}} - {{- if $indexContent }} - {{- $item.Set "content" .Plain }} - {{- end }} - {{- $item.Set "kind" .Kind -}} - {{- $item.Set "lang" .Language.Lang -}} - {{- $item.Set "url" .RelPermalink -}} - {{- $item.Set "date" .Date.Unix -}} - {{- if $filterYears -}} - {{- $item.Set "year" (.Date.Format "2006") -}} - {{- end -}} - {{/* Image */}} - {{- if or $page.Params.images ($page.Resources.GetMatch "{*feature*,*cover*,*thumbnail*}") -}} - {{- with partial "_funcs/get-page-images" $page -}} - {{- $img := index . 0 -}} - {{- $item.Set "img" $img.Permalink -}} - {{- with $img.Image -}} - {{- with .Width -}} - {{- $item.Set "img_w" . -}} - {{- end -}} - {{- with .Height -}} - {{- $item.Set "img_h" . -}} - {{- end -}} - {{- end -}} - {{- end -}} - {{- end -}} - {{- if default true site.Params.search.index_headings -}} - {{- $item.Set "headings" (partialCached "search/functions/parse-headings" . .) -}} - {{- end }} - {{/* Taxonomies */}} - {{- if default true site.Params.search.index_taxonomies -}} - {{- range $name, $taxonomy := site.Taxonomies -}} - {{- range $page.GetTerms $name -}} - {{- $item.Add $name (slice .Title) }} - {{- end -}} - {{- end -}} - {{- end }} - {{- $items = $items | append $item.Values -}} -{{- end -}} -{{- $langs := newScratch -}} -{{- range .Site.Languages -}} - {{- $langs.Set .Lang .LanguageName -}} -{{- end }} -{{- $taxonomies := newScratch -}} -{{- range $name, $taxonomy := site.Taxonomies -}} - {{- range $taxonomy -}} - {{- $taxonomies.Add $name (slice .Page.Title) -}} - {{- end -}} -{{- end -}} -{{- $years := partial "search/functions/years" . -}} -{{- $data := dict - "baseURL" site.BaseURL - "langs" $langs.Values - "pages" $items - "taxonomies" $taxonomies.Values - "years" $years --}} -{{- $data | jsonify -}}