Skip to content

Commit

Permalink
feat: enhance searching experience (#260)
Browse files Browse the repository at this point in the history
* feat: enhance index.js

- add anchor information to uri
- add support from h4 to h6

* chore(fusejs): adjust fusejs search settings

* feat: add support for h1 tag

* chore: tweak the search settings
  • Loading branch information
HEIGE-PCloud authored Oct 2, 2021
1 parent d34ebf0 commit 0b5c679
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
10 changes: 5 additions & 5 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ enableEmoji = true
# max number of results length
maxResultLength = 10
# snippet length of the result
snippetLength = 30
snippetLength = 300
# HTML tag name of the highlight part in results
highlightTag = "em"
# whether to use the absolute URL based on the baseURL in search index
Expand All @@ -151,9 +151,9 @@ enableEmoji = true
minMatchCharLength = 2
findAllMatches = false
location = 0
threshold = 0.3
threshold = 0.1
distance = 100
ignoreLocation = false
ignoreLocation = true
useExtendedSearch = false
ignoreFieldNorm = false
# Home page config
Expand Down Expand Up @@ -381,9 +381,9 @@ enableEmoji = true
minMatchCharLength = 2
findAllMatches = false
location = 0
threshold = 0.3
threshold = 0.1
distance = 100
ignoreLocation = false
ignoreLocation = true
useExtendedSearch = false
ignoreFieldNorm = false
# 主页信息设置
Expand Down
59 changes: 43 additions & 16 deletions layouts/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,59 @@
{{- if $.Site.Params.search.absoluteURL -}}
{{- $uri = .Permalink -}}
{{- end -}}
{{- $meta := dict "uri" $uri "title" .Title "tags" .Params.tags "categories" .Params.categories "series" .Params.series -}}
{{- $meta := dict "title" .Title "tags" .Params.tags "categories" .Params.categories "series" .Params.series -}}
{{- $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) -}}
{{- $index = $index | append (dict "content" . "objectID" $uri "uri" $uri| merge $meta) -}}
{{- end -}}
{{- $params := .Params | merge $.Site.Params.page -}}
{{/* Extended Markdown syntax */}}
{{- $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 -}}
{{- $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 -}}
Expand Down

1 comment on commit 0b5c679

@vercel
Copy link

@vercel vercel bot commented on 0b5c679 Oct 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.