Hugo本地搜索

配置

修改 config.toml,

1
2
[outputs]
  home = ["HTML", "RSS", "JSON"]

修改 themepartials/index.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
/// YOUR_TEMPLATE/partials/index.json
[
    {{ range $index, $element := (where .Site.RegularPages "Type" "post") }}
        {
            "title" : {{ jsonify .Title }},
            "date" : {{- jsonify .Date.Format ( or .Site.Params.dateFormat "2006, Jan 02" ) -}},
            "url" : {{ jsonify .Permalink }},
            "content": {{ jsonify .Summary }},
            "tag" : {{ jsonify .Params.Tag }}
        }{{if not (eq $index (sub (len (where .Site.RegularPages "Type" "post")) 1 )) }} , {{end}}
    {{ end }}
]

YOUR_TEMPLATE/partials/_default/search.html

1
2
3
4
5
6
7
<main id="searchLayout" class="container">
    <input id="search-input" type="text" placeholder="Type something and hit enter"/>
    <p id="search-info"></p>
    <div id="search-result"></div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/3.0.5/fuse.min.js"></script>
    <script src="search.js"></script>
</main>