From 09d4e07d4fe3156a6196b5e44df987a6f1626a86 Mon Sep 17 00:00:00 2001 From: George Cushen Date: Fri, 8 Feb 2019 17:48:17 +0000 Subject: [PATCH] feat(search): enable searching by author name or category Also, attempt to increase relevancy of search results in general. Close #921 --- assets/js/academic-search.js | 8 +++++--- layouts/index.json | 39 +++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/assets/js/academic-search.js b/assets/js/academic-search.js index 79a6401b..d4a84ae1 100644 --- a/assets/js/academic-search.js +++ b/assets/js/academic-search.js @@ -20,10 +20,12 @@ let fuseOptions = { maxPatternLength: 32, minMatchCharLength: 2, keys: [ - {name:'title', weight:0.8}, + {name:'title', weight:0.99}, /* 1.0 doesn't work o_O */ {name:'summary', weight:0.6}, - {name:'content', weight:0.5}, - {name:'tags', weight:0.3} + {name:'authors', weight:0.5}, + {name:'content', weight:0.2}, + {name:'tags', weight:0.5}, + {name:'categories', weight:0.5} ] }; diff --git a/layouts/index.json b/layouts/index.json index c6be5be1..0863ffb6 100644 --- a/layouts/index.json +++ b/layouts/index.json @@ -3,8 +3,8 @@ {{- $pages := where .Site.RegularPages "Section" "!=" "home" -}} {{- /* Add the index page of docs separately since it's not in RegularPages above. */ -}} {{- $pages := $pages | union (where (where .Site.Pages "Kind" "section") "Type" "docs") -}} -{{- /* Add author pages to index so their bios can be searched. */ -}} -{{- $pages := $pages | union (where $.Site.Pages "Section" "author") -}} +{{- /* Add author pages to index so their bios can be searched. Hide empty `/author/` node. */ -}} +{{- $pages := $pages | union (where (where $.Site.Pages "Section" "author") "Params.name" "!=" nil) -}} {{- range $pages -}} {{- /* Do not index drafts or private pages. */ -}} @@ -20,8 +20,41 @@ {{- $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 "author" -}} + {{- $title = .Params.name -}} + {{- $dir := path.Base (path.Split .Path).Dir -}} + {{- with $.Site.GetPage (printf "/authors/%s" (path.Base (path.Split .Path).Dir)) -}} + {{- $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 "/author/%s" (urlize $v)) -}} + {{- $person_page := $.Site.GetPage $person_page_path -}} + {{- if $person_page -}} + {{- $person := $person_page.Params -}} + {{- $authors = $authors | append $person.name -}} + {{- else -}} + {{- $authors = $authors | append ($v | plainify) -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- /* Add page to index. */ -}} - {{- $index = $index | append (dict "objectID" .UniqueID "date" .Date.UTC.Unix "publishdate" .PublishDate "lastmod" .Lastmod.UTC.Unix "expirydate" .ExpiryDate.UTC.Unix "lang" .Lang "permalink" .Permalink "relpermalink" .RelPermalink "title" .Title "summary" (plainify $desc) "content" .Plain "authors" .Params.Authors "kind" .Kind "type" .Type "section" .Section "tags" .Params.Tags "categories" .Params.Categories) -}} + {{- $index = $index | append (dict "objectID" .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) "content" .Plain "authors" $authors "kind" .Kind "type" .Type "section" .Section "tags" .Params.Tags "categories" .Params.Categories) -}} {{- end -}} {{- end -}}