stkevintan / canoe

A beautifull and powerfull material design theme of hugo
MIT License
13 stars 6 forks source link

A new way for search without lunrjs #1

Open kuleyu opened 6 years ago

kuleyu commented 6 years ago

A new way for search without any other dependencies but Hugo itself

As a fact, Hugo does have a way to generate .json format files of itself without any other dependencies. You can find it from Hugo official doc, and with some variables, you can generate a index.json file which looks like below:

[
  {
    "categories": ["A","B"],
    "content": "article content .........",
    "oriTitle": "article title",
    "permalink": "article url",
    "tags": ["tagA","tagB","tabC"],    
    "title": "file title"    
  }
];

Only two steps, you'll get it:

{{- $.Scratch.Add "index" slice -}}
{{- range .Site.RegularPages -}}
    {{- $.Scratch.Add "index" (dict "oriTitle" .Params.title "title" .Title "tags" .Params.tags "categories" .Params.categories "content" .Plain "permalink" .Permalink) -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

Now everytime you hugo serve or hugo -b "" , Hugo will generate a index.json file in your site root.

As hugo-canoe-theme already have it's own way to render index.json file for searching in themes/canoe/static/js/index.js , you won't have to do other things.

Summary

改完之后比较了一下,搜索能力与老方案比似乎还是差了一小点,应该可以从改善 canoe 内部的 index.js 出发得以解决,因为Client side searching for Hugo.io with Fuse.js方案的搜索能力比老方案似乎还强有一些。博客https://kuleyu-hugo.netlify.com/就是采用了 Fuse.js 方案。

kuleyu commented 6 years ago