search logic changed to use .Permalink

This commit is contained in:
zzossig 2020-02-06 11:18:23 +09:00
parent 10c14879cb
commit 8e6013a964
12 changed files with 99 additions and 119 deletions

View File

@ -1,6 +1,6 @@
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
description:
type: publication
---
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
description:
type: publication
---

File diff suppressed because one or more lines are too long

View File

@ -35,7 +35,18 @@ googleAnalytics = ""
startLevel = 2
[outputs]
home = ["HTML", "RSS", "JSON"]
page = ["HTML", "SearchIndex"]
home = ["HTML", "RSS", "SearchIndex"]
section = ["HTML", "RSS", "SearchIndex"]
taxonomyTerm = ["HTML", "RSS", "SearchIndex"]
taxonomy = ["HTML", "RSS", "SearchIndex"]
[outputFormats]
[outputFormats.SearchIndex]
mediaType = "application/json"
baseName = "index"
isPlainText = true
notAlternative = true
[taxonomies]
category = "categories"

View File

@ -1,22 +0,0 @@
{{ if eq .Type "publication" }}
{{ $paginator := .Paginate (where .Pages "Type" "publication") 100 }}
[{{ range $index, $page := $paginator.Pages }}
{{- if ne $page.Type "json" -}}
{{- if and $index (gt $index 0) -}},{{- end }}
{
"uri": "{{ $page.Permalink }}",
"title": "{{ htmlEscape $page.Title}}",
"shorttitle": "{{ htmlEscape $page.Params.shorttitle}}",
"booktitle": "{{ htmlEscape $page.Params.booktitle}}",
"tags": [{{ range $tindex, $tag := $page.Params.tags }}{{ if $tindex }}, {{ end }}"{{ $tag| htmlEscape }}"{{ end }}],
"authors": "{{ with $page.Params.authors }}{{ delimit . ", " }}{{ end }}",
"abstract": "{{ with $page.Params.abstract }}{{ . | markdownify }}{{ end }}",
"ENTRYTYPE": "{{ with $page.Params.ENTRYTYPE }}{{ . }}{{ end }}",
"publication": "{{ with $page.Params.publication }}{{ . }}{{ end }}",
"publishDate": "{{ with $page.Params.publishDate }}{{ . }}{{ end }}",
"permalink": "{{ .Permalink }}",
"content": {{$page.Plain | jsonify}}
}
{{- end -}}
{{- end -}}]
{{ end }}

View File

@ -0,0 +1,26 @@
{{- $.Scratch.Add "searchindex" slice -}}
{{ if eq .Type "publication" }}
{{ if eq .Permalink .FirstSection.Permalink }}
{{- range $index, $element := (where .Site.RegularPages "Section" "publication") -}}
{{- $.Scratch.Add "searchindex" (dict "id" $index "title" $element.Title "shorttitle" $element.Params.shorttitle "booktitle" $element.Params.booktitle "uri" $element.Permalink "tags" $element.Params.tags "authors" $element.Params.authors "abstract" $element.Params.abstract "ENTRYTYPE" $element.Params.ENTRYTYPE "publication" $element.Params.publication "publishDate" $element.Params.publishDate) -}}
{{- end -}}
{{ else }}
{{ $paginator := .Paginate (where .Pages "Type" .Type) }}
{{- range $index, $element := $paginator.Pages -}}
{{- $.Scratch.Add "searchindex" (dict "id" $index "title" $element.Title "shorttitle" $element.Params.shorttitle "booktitle" $element.Params.booktitle "uri" $element.Permalink "tags" $element.Params.tags "authors" $element.Params.authors "abstract" $element.Params.abstract "ENTRYTYPE" $element.Params.ENTRYTYPE "publication" $element.Params.publication "publishDate" $element.Params.publishDate) -}}
{{- end -}}
{{ end }}
{{ else }}
{{- range $index, $element := (where .Site.Pages "Kind" "page") -}}
{{ with $element.Plain }}
{{- $.Scratch.Add "searchindex" (dict "id" $index "title" $element.Title "uri" $element.Permalink "tags" $element.Params.tags "section" $element.Section "content" $element.Plain "description" $element.Description) -}}
{{ end }}
{{- end -}}
{{ end }}
{{- $.Scratch.Get "searchindex" | jsonify -}}

View File

@ -0,0 +1,7 @@
{{- $.Scratch.Add "searchindex" slice -}}
{{- range $index, $element := (where .Site.Pages "Kind" "page") -}}
{{ with $element.Plain }}
{{- $.Scratch.Add "searchindex" (dict "id" $index "title" $element.Title "uri" $element.Permalink "tags" $element.Params.tags "section" $element.Section "content" $element.Plain "description" $element.Description) -}}
{{ end }}
{{- end -}}
{{- $.Scratch.Get "searchindex" | jsonify -}}

View File

@ -0,0 +1,7 @@
{{- $.Scratch.Add "searchindex" slice -}}
{{- range $index, $element := (where .Site.Pages "Kind" "page") -}}
{{ with $element.Plain }}
{{- $.Scratch.Add "searchindex" (dict "id" $index "title" $element.Title "uri" $element.Permalink "tags" $element.Params.tags "section" $element.Section "content" $element.Plain "description" $element.Description) -}}
{{ end }}
{{- end -}}
{{- $.Scratch.Get "searchindex" | jsonify -}}

View File

@ -0,0 +1,7 @@
{{- $.Scratch.Add "searchindex" slice -}}
{{- range $index, $element := (where .Site.Pages "Kind" "page") -}}
{{ with $element.Plain }}
{{- $.Scratch.Add "searchindex" (dict "id" $index "title" $element.Title "uri" $element.Permalink "tags" $element.Params.tags "section" $element.Section "content" $element.Plain "description" $element.Description) -}}
{{ end }}
{{- end -}}
{{- $.Scratch.Get "searchindex" | jsonify -}}

View File

@ -1,14 +1,7 @@
[{{ range $index, $page := .Site.Pages }}
{{- if ne $page.Type "json" -}}
{{- if and $index (gt $index 0) -}},{{- end }}
{
"uri": "{{ $page.Permalink }}",
"title": "{{ htmlEscape $page.Title}}",
"tags": [{{ range $tindex, $tag := $page.Params.tags }}{{ if $tindex }}, {{ end }}"{{ $tag| htmlEscape }}"{{ end }}],
"categories": [{{ range $cindex, $category := $page.Params.categoris }}{{ if $cindex }}, {{ end }}"{{ $category| htmlEscape }}"{{ end }}],
"series": [{{ range $sindex, $series := $page.Params.series }}{{ if $sindex }}, {{ end }}"{{ $series| htmlEscape }}"{{ end }}],
"description": "{{ htmlEscape .Description}}",
"content": {{$page.Plain | jsonify}}
}
{{- $.Scratch.Add "searchindex" slice -}}
{{- range $index, $element := (where .Site.Pages "Kind" "page") -}}
{{ with $element.Plain }}
{{- $.Scratch.Add "searchindex" (dict "id" $index "title" $element.Title "uri" $element.Permalink "tags" $element.Params.tags "section" $element.Section "content" $element.Plain "description" $element.Description) -}}
{{ end }}
{{- end -}}
{{- end -}}]
{{- $.Scratch.Get "searchindex" | jsonify -}}

View File

@ -551,44 +551,8 @@
// ========================== search ==========================
var baseurl = null;
{{ $siteBaseURL:= .Site.BaseURL }}
var siteBaseURL = JSON.parse({{ $siteBaseURL | jsonify }});
var siteBaseChecker = /\/\w+\//i;
var isSlug = siteBaseChecker.test(siteBaseURL);
var isThemeSite = location.origin.includes('themes.gohugo.io');
{{ if .Site.IsMultiLingual }}
if (isThemeSite) {
baseurl = "{{.Site.BaseURL}}{{.Site.LanguagePrefix}}";
} else {
var hasLangPostfix = location.pathname.includes("/{{.Site.Language.Lang}}");
if (hasLangPostfix) {
if (isSlug) {
baseurl = location.origin + siteBaseURL.match(siteBaseChecker)[0] + "{{.Site.Language.Lang}}";
} else {
baseurl = location.origin + "/{{.Site.Language.Lang}}";
}
} else {
if (isSlug) {
baseurl = location.origin + siteBaseURL.match(siteBaseChecker)[0];
} else {
baseurl = location.origin;
}
}
}
{{ else }}
if (isThemeSite) {
baseurl = "{{.Site.BaseURL}}";
} else {
if (isSlug) {
baseurl = location.origin + siteBaseURL.match(siteBaseChecker)[0];
} else {
baseurl = location.origin;
}
}
{{ end }}
{{ $permalink:= .Permalink }}
var permalink = JSON.parse({{ $permalink | jsonify }});
var searchResults = null;
var searchMenu = null;
var searchText = null;
@ -598,17 +562,9 @@
var fuse = null;
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
function initFuse() {
if (!endsWith(baseurl, "/")) {
baseurl = baseurl + '/';
};
var xhr = new XMLHttpRequest();
xhr.open('GET', baseurl + "index.json");
xhr.open('GET', permalink + "index.json");
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.onload = function () {
if (xhr.status === 200) {

View File

@ -1,18 +1,18 @@
{{ $current := . }}
<nav class="showcase__nav">
<div class="pub__toolbar--search search">
<input id="pubSearch" aria-label="Publication Search" class="input" type="text" placeholder="{{T "search-placeholder"}}" autocomplete="off"/>
</div>
<a href="{{ .FirstSection.Permalink }}" class="showcase__button {{ if eq .Permalink .FirstSection.Permalink }}active{{ end }}">
{{ i18n "showcase-overview" }}
</a>
{{ range (.Site.GetPage "section" .Type).Pages }}
{{ $active := false }}
{{ $active = or $active (eq .Name $current.Title) }}
{{ $active = or $active (eq (lower .Permalink) (lower $current.Title)) }}
{{ $active = or $active (eq (lower .Permalink) (lower $current.Type)) }}
<a href="{{ .Permalink }}" class="showcase__button {{ if $active }}active{{ end }}" data-meta="{{ len .Pages }}">
{{ i18n (lower .Title) | default .Title }}
</a>
{{ end }}
{{ $current := . }}
<nav class="showcase__nav">
<div class="pub__toolbar--search search">
<input id="pubSearch" aria-label="Publication Search" class="input" type="text" placeholder="{{T "search-placeholder"}}" autocomplete="off"/>
</div>
<a href="{{ .FirstSection.Permalink }}" class="showcase__button {{ if eq .Permalink .FirstSection.Permalink }}active{{ end }}">
{{ i18n "showcase-overview" }}
</a>
{{ range (.Site.GetPage "section" .Type).Pages }}
{{ $active := false }}
{{ $active = or $active (eq .Name $current.Title) }}
{{ $active = or $active (eq (lower .Permalink) (lower $current.Title)) }}
{{ $active = or $active (eq (lower .Permalink) (lower $current.Type)) }}
<a href="{{ .Permalink }}" class="showcase__button {{ if $active }}active{{ end }}" data-meta="{{ len .Pages }}">
{{ i18n (lower .Title) | default .Title }}
</a>
{{ end }}
</nav>

View File

@ -6,8 +6,6 @@
<script>
{{ $permalink := .Permalink }}
var permalink = JSON.parse({{ $permalink | jsonify }});
{{ $isRootSection := eq .Permalink .FirstSection.Permalink }}
var isRootSection = JSON.parse({{ $isRootSection | jsonify }});
var data = null;
const options = {
limit: 100, // don't return more results than you need!
@ -58,7 +56,7 @@
if (obj.publishDate) {
var dateSpan = document.createElement('span');
dateSpan.className = 'caption pub__meta';
dateSpan.innerText = '📅 ' + obj.publishDate.slice(0, 11);
dateSpan.innerText = '📅 ' + obj.publishDate.slice(0, 10);
metaDiv.appendChild(dateSpan);
}
@ -97,9 +95,6 @@
pubSearch ?
pubSearch.addEventListener('input', function(e) {
if (isRootSection) {
return null;
}
var originUl = pubList.querySelector('ul');
var ul = document.createElement('ul');