This commit is contained in:
yuweizzz
2024-09-03 23:41:28 +08:00
parent b6c3256428
commit 99afb0f751
10 changed files with 231 additions and 8 deletions

View File

@@ -4,6 +4,7 @@
<body>
{{ partial "header" . }}
{{ partial "search.html" .}}
{{ block "content" . }}{{ end }}
{{ partial "footer.html" . }}
</body>

View File

@@ -0,0 +1,67 @@
{{- /* Copy from https://github.com/HugoBlox/hugo-blox-builder/blob/main/modules/blox-bootstrap/layouts/index.json. */ -}}
{{- /* Generate the search index. */ -}}
{{- $index := slice -}}
{{- $pages := site.RegularPages -}}
{{- /* Add the index page of multi-page content separately since it's not in RegularPages above. */ -}}
{{- $pages := $pages | union (where (where site.Pages "Kind" "section") "Type" "book") -}}
{{- /* Add author pages to index so their bios can be searched. Hide empty `/authors/` node. */ -}}
{{- $pages := $pages | union (where (where site.Pages "Section" "authors") "Params.superuser" "!=" nil) -}}
{{- range $pages -}}
{{- /* Do not index drafts or private pages. */ -}}
{{- if and (not .Draft) (not .Params.private) | and (ne .Params.searchable false) -}}
{{- /* Generate page description. */ -}}
{{- $desc := "" -}}
{{- if .Params.summary -}}
{{- $desc = .Params.summary -}}
{{- else if .Params.abstract -}}
{{- $desc = .Params.abstract -}}
{{- else -}}
{{- $desc = .Summary -}}
{{- end -}}
{{- $authors := .Params.authors -}}
{{- $title := .Title}}
{{- $rel_permalink := .RelPermalink -}}
{{- $permalink := .Permalink -}}
{{/* Correct the title and URL for author profile pages. */}}
{{- if eq .Section "authors" -}}
{{- $username := path.Base .File.Dir -}}
{{- with site.GetPage (printf "/authors/%s" $username) -}}
{{- $permalink = .Permalink -}}
{{- $rel_permalink = .RelPermalink -}}
{{- end -}}
{{- else -}}
{{/* Include a user's display name rather than username where possible. */}}
{{- if .Params.authors -}}
{{- $authorLen := len .Params.authors -}}
{{- if gt $authorLen 0 -}}
{{- $authors = slice -}}
{{- range $k, $v := .Params.authors -}}
{{- $person_page_path := (printf "/authors/%s" (urlize $v)) -}}
{{- $person_page := site.GetPage $person_page_path -}}
{{- if and $person_page $person_page.File -}}
{{- $authors = $authors | append $person_page.Title -}}
{{- else -}}
{{- $authors = $authors | append ($v | plainify) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- /* Add page to index. */ -}}
{{/* Exclude virtual pages which aren't backed by a file */}}
{{- if .File }}
{{/* exclude "content/readme.md". */}}
{{ $hidden_file := "/readme/" }}
{{ if ne $hidden_file $rel_permalink }}
{{- $index = $index | append (dict "objectID" .File.UniqueID "date" .Date.UTC.Unix "publishdate" .PublishDate "lastmod" .Lastmod.UTC.Unix "expirydate" .ExpiryDate.UTC.Unix "lang" .Lang "permalink" $permalink "relpermalink" $rel_permalink "title" $title "summary" (plainify $desc | htmlUnescape) "content" (.Plain | htmlUnescape | truncate 5000) "authors" $authors "kind" .Kind "type" .Type "section" .Section "tags" .Params.Tags "categories" .Params.Categories) -}}
{{ end }}
{{ end -}}
{{- end -}}
{{- end -}}
{{- $index | jsonify -}}

View File

@@ -1,8 +1,6 @@
{{ define "content" }}
<div class="application-main">
<main>
{{ partial "user-profile.html" . }}
{{ partial "posts.html" .}}
{{ partial "user-profile.html" . }}
{{ partial "posts.html" .}}
</div>
{{end }}
{{ end }}

View File

@@ -1,4 +1,6 @@
{{ define "content" }}
{{ partial "post.html" .}}
{{ partial "gitalk.html" . }}
{{end }}
<div class="application-main">
{{ partial "post.html" .}}
{{ partial "gitalk.html" . }}
</div>
{{ end }}

View File

@@ -19,6 +19,17 @@
<div
class="Header-search header-search flex-auto js-site-search position-relative flex-self-stretch flex-md-self-auto mb-3 mb-md-0 mr-0 mr-md-3 scoped-search site-scoped-search js-jump-to">
<div class="position-relative">
{{ if .Site.Params.enableSearch }}
<form target="_blank" id="search-form" action="" accept-charset="UTF-8" method="get"
autocomplete="off">
<label
class="Header-search-label form-control input-sm header-search-wrapper p-0 js-chromeless-input-container header-search-wrapper-jump-to position-relative d-flex flex-justify-between flex-items-center">
<input type="text"
class="Header-search-input form-control input-sm header-search-input jump-to-field js-jump-to-field js-site-search-focus js-site-search-field is-clearable"
name="q" value="" placeholder="Search" autocomplete="off">
</label>
</form>
{{ else }}
<form target="_blank" action="https://www.google.com/search" accept-charset="UTF-8" method="get"
autocomplete="off">
<label
@@ -29,6 +40,7 @@
<input type="hidden" name="q" value="site:{{ .Site.BaseURL }}">
</label>
</form>
{{ end }}
</div>
</div>
</div>

View File

@@ -47,3 +47,8 @@
}
</style>
{{ end }}
{{ if .Site.Params.enableSearch }}
<script src="https://cdn.jsdelivr.net/npm/fuse.js/dist/fuse.min.js"></script>
<script type="application/javascript" src='{{ "js/search.js" | absURL }}'></script>
{{ end }}

View File

@@ -0,0 +1,2 @@
<div id="search-result" class="container-lg px-3 new-discussion-timeline" style="display: none;">
</div>