2021-07-02 / syui

hugo

hugoでsearchするやつ付けてみた

/search

記事を検索したい場合、自分はcontent/postをgrepするだけなので、あまり使わないのですが、一応、webにも検索フォームを付けてみました。

hugo + vue + lunr.jsです。

ただ、日本語はおそらく対応してない。対応するには日本語対応するためのlibを追加で読み込む必要がありそう。

lunrに渡すdataは、hugoがjson出力できるので、それを使います。

[outputs]
  home = ["JSON", "HTML"]

jsはこんな感じで(要点記述だけですが)。

axios('/index.json')

this.searchIndex = lunr(function () {
  this.ref('href')
  this.field('contents')
  this.field('title')
  this.field('tags')
  documents.forEach(doc => {
    this.add(doc)
  })
}