From 0848be17bac7fbabbc10429e8510fe2816a3c734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chuso=20P=C3=A9rez?= Date: Sat, 13 Mar 2021 18:43:10 +0100 Subject: [PATCH] #42 Add suppor for client-side search (#227) * Add support for client-side search with Fuse.js. Implements #42. Based on https://gist.github.com/eddiewebb/735feb48f50f0ddd65ae5606a1cb41ae * Move search form code repeated in many places to one single template. * Add search box in home page. * Fix search UI + Some improvements Signed-off-by: hossainemruz * Remove search.md from exampleSite Signed-off-by: hossainemruz Co-authored-by: Emruz Hossain --- content/posts/search.md | 52 ++++++++++++++++ exampleSite/config.yaml | 8 +++ layouts/_default/index.json | 5 ++ layouts/_default/list.html | 4 +- layouts/_default/search.html | 71 ++++++++++++++++++++++ layouts/_default/single.html | 4 +- static/js/search.js | 112 +++++++++++++++++++++++++++++++++++ 7 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 content/posts/search.md create mode 100644 layouts/_default/index.json create mode 100644 layouts/_default/search.html create mode 100644 static/js/search.js diff --git a/content/posts/search.md b/content/posts/search.md new file mode 100644 index 0000000..0f038c1 --- /dev/null +++ b/content/posts/search.md @@ -0,0 +1,52 @@ +--- +title: "Search Results" +sitemap: + priority : 0.1 +layout: "search" +url: search +--- + + +This file exists solely to respond to /search URL with the related `search` layout template. + +No content shown here is rendered, all content is based in the template layouts/page/search.html + +Setting a very low sitemap priority will tell search engines this is not important content. + +This implementation uses Fusejs, jquery and mark.js + + +## Initial setup + +Search depends on additional output content type of JSON in config.toml +\``` +[outputs] + home = ["HTML", "JSON"] +\``` + +## Searching additional fileds + +To search additional fields defined in front matter, you must add it in 2 places. + +### Edit layouts/_default/index.JSON +This exposes the values in /index.json +i.e. add `category` +\``` +... + "contents":{{ .Content | plainify | jsonify }} + {{ if .Params.tags }}, + "tags":{{ .Params.tags | jsonify }}{{end}}, + "categories" : {{ .Params.categories | jsonify }}, +... +\``` + +### Edit fuse.js options to Search +`static/js/search.js` +\``` +keys: [ + "title", + "contents", + "tags", + "categories" +] +\``` diff --git a/exampleSite/config.yaml b/exampleSite/config.yaml index 670c99c..52c5095 100644 --- a/exampleSite/config.yaml +++ b/exampleSite/config.yaml @@ -32,6 +32,14 @@ languages: languageName: 中文 weight: 8 +# At least HTML and JSON are required for the main HTML content and +# client-side JavaScript search +outputs: + home: + - HTML + - RSS + - JSON + # Force a locale to be use, really useful to develop the application ! Should be commented in production, the "weight" should rocks. # DefaultContentLanguage: fr diff --git a/layouts/_default/index.json b/layouts/_default/index.json new file mode 100644 index 0000000..c29397c --- /dev/null +++ b/layouts/_default/index.json @@ -0,0 +1,5 @@ +{{- $.Scratch.Add "index" slice -}} +{{- range .Site.RegularPages -}} + {{- $.Scratch.Add "index" (dict "title" .Title "hero" (partial "helpers/get-hero.html" .) "date" (.Date.Format "January 2, 2006") "summary" .Summary "tags" .Params.tags "categories" .Params.categories "contents" .Plain "permalink" .Permalink) -}} +{{- end -}} +{{- $.Scratch.Get "index" | jsonify -}} diff --git a/layouts/_default/list.html b/layouts/_default/list.html index f6e79cb..ae014d1 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -16,7 +16,9 @@