feat: enhance search functionality with additional attributes and improved anchor handling

This commit is contained in:
Cell
2025-11-13 16:19:13 +08:00
parent 96b2f5f8ae
commit 1ba0303572
3 changed files with 53 additions and 20 deletions
+9 -4
View File
@@ -240,12 +240,17 @@ class FixIt {
};
if (searchConfig.type === 'algolia') {
this._algoliaIndex =
this._algoliaIndex || algoliasearch(searchConfig.algoliaAppID, searchConfig.algoliaSearchKey).initIndex(searchConfig.algoliaIndex);
this._algoliaIndex ||
algoliasearch(
searchConfig.algoliaAppID,
searchConfig.algoliaSearchKey
).initIndex(searchConfig.algoliaIndex);
this._algoliaIndex
.search(query, {
offset: 0,
length: maxResultLength * 8,
attributesToHighlight: ['title'],
attributesToRetrieve: ['*'],
attributesToSnippet: [`content:${snippetLength}`],
highlightPreTag: `<${highlightTag}>`,
highlightPostTag: `</${highlightTag}>`
@@ -333,8 +338,8 @@ class FixIt {
}
},
templates: {
suggestion: ({ title, date, context }) =>
`<div><span class="suggestion-title">${title}</span><span class="suggestion-date">${date}</span></div><div class="suggestion-context">${context}</div>`,
suggestion: ({ title, uri, date, context }) =>
`<div><a href="${uri}"><span class="suggestion-title">${title}</span></a><span class="suggestion-date">${date}</span></div><div class="suggestion-context">${context}</div>`,
empty: ({ query }) => `<div class="search-empty">${searchConfig.noResultsFound}: <span class="search-query">"${query}"</span></div>`,
footer: ({}) => {
let searchType, icon, href;
@@ -1360,7 +1365,7 @@ class FixIt {
this.initReward();
this.initPostChatUser();
// todo] refactor async init toc
// [todo] refactor async init toc
window.setTimeout(() => {
this.initComment();
if (!this.config.encryption?.all) {
+1 -1
View File
@@ -1,4 +1,4 @@
{{- hugo.Store.Set "version" "v0.4.0-alpha.2-20251113034916-6f27a2b2" -}}
{{- hugo.Store.Set "version" "v0.4.0-alpha.2-20251113081914-96b2f5f8" -}}
{{- .Store.Set "this" dict -}}
{{- partial "init/detection-env.html" . -}}
+43 -15
View File
@@ -11,7 +11,7 @@
{{- if $.Site.Params.search.absoluteURL -}}
{{- $uri = .Permalink -}}
{{- end -}}
{{- $meta := dict "uri" $uri "title" .Title "tags" .Params.tags "categories" .Params.categories -}}
{{- $meta := dict "uri" $uri "title" .Title "tags" .Params.tags "categories" .Params.categories "collections" .Params.collections -}}
{{- $meta = $.Site.Params.dateFormat | default "2006-01-02" | .PublishDate.Format | dict "date" | merge $meta -}}
{{- with .Description -}}
{{- $index = $index | append (dict "content" . "objectID" $uri | merge $meta) -}}
@@ -21,22 +21,50 @@
{{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" -}}
{{/* Remove line number for code */}}
{{- $content = $content | replaceRE `<span class="lnt?"> *\d*\n?</span>` "" -}}
{{- range $i, $contenti := split $content "<h2 id=" -}}
{{- if gt $i 0 -}}
{{- $contenti = printf "<h2 id=%v" $contenti -}}
{{- $content = $content | replaceRE ` class="heading-element"` "" -}}
{{- $anchor := "" -}}
{{- range $h, $contenth := split $content "<h1 id=" -}}
{{- if gt $h 0 -}}
{{- $anchor = replace (index (split $contenth ">") 0) `"` "" -}}
{{- $contenth = printf "<h1 id=%v" $contenth -}}
{{- end -}}
{{- range $j, $contentj := split $contenti "<h3 id=" -}}
{{- if gt $j 0 -}}
{{- $contentj = printf "<h3 id=%v" $contentj -}}
{{- range $i, $contenti := split $contenth "<h2 id=" -}}
{{- if gt $i 0 -}}
{{- $anchor = replace (index (split $contenti ">") 0) `"` "" -}}
{{- $contenti = printf "<h2 id=%v" $contenti -}}
{{- end -}}
{{/* Plainify, unescape and remove (\n, \t) */}}
{{- $contentj = $contentj | plainify | htmlUnescape | replaceRE `[\n\t ]+` " " -}}
{{- if gt $.Site.Params.search.contentLength 0 -}}
{{- $contentj = substr $contentj 0 $.Site.Params.search.contentLength -}}
{{- end -}}
{{- if $contentj | and (ne $contentj " ") -}}
{{- $one := printf "%v:%v:%v" $uri $i $j | dict "content" $contentj "objectID" | merge $meta -}}
{{- $index = $index | append $one -}}
{{- range $j, $contentj := split $contenti "<h3 id=" -}}
{{- if gt $j 0 -}}
{{- $anchor = replace (index (split $contentj ">") 0) `"` "" -}}
{{- $contentj = printf "<h3 id=%v" $contentj -}}
{{- end -}}
{{- range $k, $contentk := split $contentj "<h4 id=" -}}
{{- if gt $k 0 -}}
{{- $anchor = replace (index (split $contentk ">") 0) `"` "" -}}
{{- $contentk = printf "<h4 id=%v" $contentk -}}
{{- end -}}
{{- range $l, $contentl := split $contentk "<h5 id=" -}}
{{- if gt $l 0 -}}
{{- $anchor = replace (index (split $contentl ">") 0) `"` "" -}}
{{- $contentk = printf "<h5 id=%v" $contentl -}}
{{- end -}}
{{- range $m, $contentm := split $contentl "<h6 id=" -}}
{{- if gt $m 0 -}}
{{- $anchor = replace (index (split $contentm ">") 0) `"` "" -}}
{{- $contentm = printf "<h6 id=%v" $contentm -}}
{{- end -}}
{{/* Plainify, unescape and remove (\n, \t) */}}
{{- $contentj = $contentj | plainify | htmlUnescape | replaceRE `[\n\t ]+` " " -}}
{{- if gt $.Site.Params.search.contentLength 0 -}}
{{- $contentj = substr $contentj 0 $.Site.Params.search.contentLength -}}
{{- end -}}
{{- if $contentj | and (ne $contentj " ") -}}
{{- $one := printf "%v:%v:%v" $uri $i $j | dict "content" $contentj "uri" (printf "%v#%v" $uri $anchor) "objectID" | merge $meta -}}
{{- $index = $index | append $one -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}