diff options
author | Dillon <dillonzq@outlook.com> | 2020-05-01 18:54:29 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-01 18:54:29 +0300 |
commit | 971eff815c8c1826a50ef85954714a429b96e990 (patch) | |
tree | 8136cef397ee9250bcb77bbb100bd747ecae85c4 | |
parent | e3e3bdf9ee64d8732216b2182ec2bac9f3d4e1e1 (diff) |
feat(search): add absoluteURL config for search index (#302)
-rw-r--r-- | exampleSite/config.toml | 6 | ||||
-rw-r--r-- | exampleSite/content/posts/theme-documentation-basics.en.md | 4 | ||||
-rw-r--r-- | exampleSite/content/posts/theme-documentation-basics.fr.md | 4 | ||||
-rw-r--r-- | exampleSite/content/posts/theme-documentation-basics.zh-cn.md | 4 | ||||
-rw-r--r-- | layouts/index.json | 73 |
5 files changed, 57 insertions, 34 deletions
diff --git a/exampleSite/config.toml b/exampleSite/config.toml index 7712f8c3..9cb66a1a 100644 --- a/exampleSite/config.toml +++ b/exampleSite/config.toml @@ -125,6 +125,8 @@ enableEmoji = true snippetLength = 30 # HTML tag name of the highlight part in results highlightTag = "em" + # whether to use the absolute URL based on the baseURL in search index + absoluteURL = false [languages.en.params.search.algolia] index = "index.en" appID = "PASDMWALPK" @@ -321,6 +323,8 @@ enableEmoji = true snippetLength = 50 # 搜索结果中高亮部分的 HTML 标签 highlightTag = "em" + # 是否在搜索索引中使用基于 baseURL 的绝对路径 + absoluteURL = false [languages.zh-cn.params.search.algolia] index = "index.zh-cn" appID = "PASDMWALPK" @@ -513,6 +517,8 @@ enableEmoji = true snippetLength = 30 # HTML tag name of the highlight part in results highlightTag = "em" + # whether to use the absolute URL based on the baseURL in search index + absoluteURL = false [languages.fr.params.search.algolia] index = "index.fr" appID = "PASDMWALPK" diff --git a/exampleSite/content/posts/theme-documentation-basics.en.md b/exampleSite/content/posts/theme-documentation-basics.en.md index cc479b0f..86f7deb6 100644 --- a/exampleSite/content/posts/theme-documentation-basics.en.md +++ b/exampleSite/content/posts/theme-documentation-basics.en.md @@ -231,6 +231,8 @@ Please open the code block below to view the complete sample configuration :(far snippetLength = 30 # {{< version 0.2.1 >}} HTML tag name of the highlight part in results highlightTag = "em" + # {{< version 0.2.4 >}} whether to use the absolute URL based on the baseURL in search index + absoluteURL = false [params.search.algolia] index = "" appID = "" @@ -987,6 +989,8 @@ Here is the search configuration in your [site configuration](#site-configuratio snippetLength = 30 # {{< version 0.2.1 >}} HTML tag name of the highlight part in results highlightTag = "em" + # {{< version 0.2.4 >}} whether to use the absolute URL based on the baseURL in search index + absoluteURL = false [params.search.algolia] index = "" appID = "" diff --git a/exampleSite/content/posts/theme-documentation-basics.fr.md b/exampleSite/content/posts/theme-documentation-basics.fr.md index 45f19532..57494067 100644 --- a/exampleSite/content/posts/theme-documentation-basics.fr.md +++ b/exampleSite/content/posts/theme-documentation-basics.fr.md @@ -236,6 +236,8 @@ Please open the code block below to view the complete sample configuration :(far snippetLength = 30 # {{< version 0.2.1 >}} HTML tag name of the highlight part in results highlightTag = "em" + # {{< version 0.2.4 >}} whether to use the absolute URL based on the baseURL in search index + absoluteURL = false [params.search.algolia] index = "" appID = "" @@ -992,6 +994,8 @@ Here is the search configuration in your [site configuration](#site-configuratio snippetLength = 30 # {{< version 0.2.1 >}} HTML tag name of the highlight part in results highlightTag = "em" + # {{< version 0.2.4 >}} whether to use the absolute URL based on the baseURL in search index + absoluteURL = false [params.search.algolia] index = "" appID = "" diff --git a/exampleSite/content/posts/theme-documentation-basics.zh-cn.md b/exampleSite/content/posts/theme-documentation-basics.zh-cn.md index 76103d67..a280d9df 100644 --- a/exampleSite/content/posts/theme-documentation-basics.zh-cn.md +++ b/exampleSite/content/posts/theme-documentation-basics.zh-cn.md @@ -234,6 +234,8 @@ hugo snippetLength = 50 # {{< version 0.2.1 >}} 搜索结果中高亮部分的 HTML 标签 highlightTag = "em" + # {{< version 0.2.4 >}} 是否在搜索索引中使用基于 baseURL 的绝对路径 + absoluteURL = false [params.search.algolia] index = "" appID = "" @@ -992,6 +994,8 @@ defaultContentLanguage = "zh-cn" snippetLength = 50 # {{< version 0.2.1 >}} 搜索结果中高亮部分的 HTML 标签 highlightTag = "em" + # {{< version 0.2.4 >}} 是否在搜索索引中使用基于 baseURL 的绝对路径 + absoluteURL = false [params.search.algolia] index = "" appID = "" diff --git a/layouts/index.json b/layouts/index.json index 7ba934fd..c85af1f7 100644 --- a/layouts/index.json +++ b/layouts/index.json @@ -1,41 +1,46 @@ -{{- $index := slice -}} -{{- $pages := .Site.RegularPages -}} -{{- if .Site.Params.page.hiddenFromSearch -}} - {{- $pages = where $pages "Params.hiddenfromsearch" false -}} -{{- else -}} - {{- $pages = where $pages "Params.hiddenfromsearch" "!=" true -}} -{{- end -}} -{{- range $pages -}} - {{- $uri := .RelPermalink -}} - {{- $meta := dict "uri" $uri "title" .Title "tags" .Params.tags "categories" .Params.categories -}} - {{- $meta = .PublishDate.Format ($.Site.Params.dateFormat | default "2006-01-02") | dict "date" | merge $meta -}} - {{- with .Description -}} - {{- $index = $index | append (dict "content" . "objectID" $uri | merge $meta) -}} +{{- if .Site.Params.search -}} + {{- $index := slice -}} + {{- $pages := .Site.RegularPages -}} + {{- if .Site.Params.page.hiddenFromSearch -}} + {{- $pages = where $pages "Params.hiddenfromsearch" false -}} + {{- else -}} + {{- $pages = where $pages "Params.hiddenfromsearch" "!=" true -}} {{- 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=%s" $contenti -}} + {{- range $pages -}} + {{- $uri := .RelPermalink -}} + {{- if $.Site.Params.search.absoluteURL -}} + {{- $uri = .Permalink -}} {{- end -}} - {{- range $j, $contentj := split $contenti "<h3 id=" -}} - {{- if gt $j 0 -}} - {{- $contentj = printf "<h3 id=%s" $contentj -}} - {{- 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 -}} + {{- $meta := dict "uri" $uri "title" .Title "tags" .Params.tags "categories" .Params.categories -}} + {{- $meta = .PublishDate.Format ($.Site.Params.dateFormat | default "2006-01-02") | dict "date" | merge $meta -}} + {{- with .Description -}} + {{- $index = $index | append (dict "content" . "objectID" $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=%s" $contenti -}} {{- end -}} - {{- if $contentj | and (ne $contentj " ") -}} - {{- $one := printf "%s:%d:%d" $uri $i $j | dict "content" $contentj "objectID" | merge $meta -}} - {{- $index = $index | append $one -}} + {{- range $j, $contentj := split $contenti "<h3 id=" -}} + {{- if gt $j 0 -}} + {{- $contentj = printf "<h3 id=%s" $contentj -}} + {{- 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 "%s:%d:%d" $uri $i $j | dict "content" $contentj "objectID" | merge $meta -}} + {{- $index = $index | append $one -}} + {{- end -}} {{- end -}} {{- end -}} {{- end -}} -{{- end -}} -{{- $index | jsonify | safeJS -}} + {{- $index | jsonify | safeJS -}} +{{- end -}} |