perf(search): enable anchorify for headings in search results (#672)

This commit is contained in:
Cell
2025-12-31 16:43:04 +08:00
committed by GitHub
parent 825d458222
commit e87060f707
3 changed files with 61 additions and 34 deletions
+1
View File
@@ -372,6 +372,7 @@ class FixIt {
}
);
autosearch.on('autocomplete:selected', (_event, suggestion, _dataset, _context) => {
document.getElementById('mask')?.click();
window.location.assign(suggestion.uri);
});
if (isMobile) {
+2
View File
@@ -496,6 +496,8 @@ snippetLength = 30
highlightTag = "em"
# whether to use the absolute URL based on the baseURL in search index
absoluteURL = false
# FixIt 0.4.0 | NEW whether to enable anchorify for headings in search results
anchorify = true
[params.search.algolia]
index = ""
+58 -34
View File
@@ -22,48 +22,72 @@
{{/* Remove line number for code */}}
{{- $content = $content | replaceRE `<span class="lnt?"> *\d*\n?</span>` "" -}}
{{- $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 -}}
{{- if ne $.Site.Params.search.anchorify false -}}
{{- /* When anchorify is enabled (default), create separate index entries for each heading */ -}}
{{- $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 $i, $contenti := split $contenth "<h2 id=" -}}
{{- if gt $i 0 -}}
{{- $anchor = replace (index (split $contenti ">") 0) `"` "" -}}
{{- $contenti = printf "<h2 id=%v" $contenti -}}
{{- end -}}
{{- 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 -}}
{{- range $i, $contenti := split $contenth "<h2 id=" -}}
{{- else -}}
{{- /* When anchorify is disabled, create index entries by splitting at h2 and h3 headings only */ -}}
{{- range $i, $contenti := split $content "<h2 id=" -}}
{{- if gt $i 0 -}}
{{- $anchor = replace (index (split $contenti ">") 0) `"` "" -}}
{{- $contenti = printf "<h2 id=%v" $contenti -}}
{{- end -}}
{{- 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 -}}
{{/* 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 -}}
{{- end -}}
{{- end -}}
{{- end -}}