From 78322220be2655e6360500da4e40a6c21b14a421 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Sat, 30 Nov 2024 20:50:31 +0700 Subject: [PATCH] [Add] search (WIP) --- config.yml | 6 ++ content/search.md | 51 +++++++++++++++++ layouts/_default/index.json | 5 ++ layouts/_default/search.html | 27 +++++++++ static/js/search.js | 108 +++++++++++++++++++++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 content/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/config.yml b/config.yml index 1bf77d1..1d403ed 100644 --- a/config.yml +++ b/config.yml @@ -112,3 +112,9 @@ languages: url: https://tiennm99.github.io/webcv - name: PDF CV url: https://tiennm99.github.io/cv/miti99.pdf + +outputs: + home: + - html + - rss + - json diff --git a/content/search.md b/content/search.md new file mode 100644 index 0000000..9f30562 --- /dev/null +++ b/content/search.md @@ -0,0 +1,51 @@ +--- +title: "Search Results" +sitemap: + priority : 0.1 +layout: "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/layouts/_default/index.json b/layouts/_default/index.json new file mode 100644 index 0000000..c93f805 --- /dev/null +++ b/layouts/_default/index.json @@ -0,0 +1,5 @@ +{{- $.Scratch.Add "index" slice -}} +{{- range .Site.RegularPages -}} + {{- $.Scratch.Add "index" (dict "title" .Title "tags" .Params.tags "categories" .Params.categories "contents" .Plain "permalink" .Permalink) -}} +{{- end -}} +{{- $.Scratch.Get "index" | jsonify -}} diff --git a/layouts/_default/search.html b/layouts/_default/search.html new file mode 100644 index 0000000..daa85e3 --- /dev/null +++ b/layouts/_default/search.html @@ -0,0 +1,27 @@ +{{ define "footerfiles" }} + + + + +{{ end }} +{{ define "main" }} +
+
+
+ +
+
+

Matching pages

+
+
+
+ + +{{ end }} diff --git a/static/js/search.js b/static/js/search.js new file mode 100644 index 0000000..add8549 --- /dev/null +++ b/static/js/search.js @@ -0,0 +1,108 @@ +summaryInclude=60; +var fuseOptions = { + shouldSort: true, + includeMatches: true, + threshold: 0.0, + tokenize:true, + location: 0, + distance: 100, + maxPatternLength: 32, + minMatchCharLength: 1, + keys: [ + {name:"title",weight:0.8}, + {name:"contents",weight:0.5}, + {name:"tags",weight:0.3}, + {name:"categories",weight:0.3} + ] +}; + + +var searchQuery = param("s"); +if(searchQuery){ + $("#search-query").val(searchQuery); + executeSearch(searchQuery); +}else { + $('#search-results').append("

Please enter a word or phrase above

"); +} + + + +function executeSearch(searchQuery){ + $.getJSON( "/index.json", function( data ) { + var pages = data; + var fuse = new Fuse(pages, fuseOptions); + var result = fuse.search(searchQuery); + console.log({"matches":result}); + if(result.length > 0){ + populateResults(result); + }else{ + $('#search-results').append("

No matches found

"); + } + }); +} + +function populateResults(result){ + $.each(result,function(key,value){ + var contents= value.item.contents; + var snippet = ""; + var snippetHighlights=[]; + var tags =[]; + if( fuseOptions.tokenize ){ + snippetHighlights.push(searchQuery); + }else{ + $.each(value.matches,function(matchKey,mvalue){ + if(mvalue.key == "tags" || mvalue.key == "categories" ){ + snippetHighlights.push(mvalue.value); + }else if(mvalue.key == "contents"){ + start = mvalue.indices[0][0]-summaryInclude>0?mvalue.indices[0][0]-summaryInclude:0; + end = mvalue.indices[0][1]+summaryInclude