basyura's blog

あしたになったらほんきだす。

Inkdrop - jsmigemo でノート選択

js で migemo できるようなので、Inkdrop の plugin に組み込んでみた。

組み込み先は ↑ で、ノートをキーワードで絞り込んで選択して表示するもの。オリジナルは ココ だけど、個人的に色々変えたいところがあってひっそりと fork 版を使っている。

常に使っていて、無くてはならない plugin になっている。

辞書づくり

サンプルコードがあるので使い方は分かるのだけど、migemo 初心者なので辞書の作り方が分からない。

jsmigemo にも同梱されているけど、 上記を参照しつつ自分でも作れるようにしてみる。
今回は Windows + GitBash な環境で開発。

nkf インストール (参考: https://zenn.dev/yotto428/scraps/1d9ed1df6ab55d)

$ wget https://ja.osdn.net/projects/nkf/downloads/70406/nkf-2.1.5.tar.gz
$ tar zxvf nkf-2.1.5.tar.gz
$ cd nkf-2.1.5
$ make
$ mkdir /usr/local/bin
$ make install

migemo ビルド

$ make cyg

辞書ビルド

$ make cyg-dict

js 用辞書作成

  • bin/jsmigemo-dict.mjs を実行する
  • C/Migemo で生成した辞書を指定する
./node_modules/jsmigemo/bin/jsmigemo-dict.mjs 
                    '{ path to migemo-dict}' migemo-compact-dict

jsmigemo を組み込む

plugin 設定に辞書のパスを持たせる

module.exports = {
  config: {
    migemoDictPath: {
      title: "migemo ditionary file path",
      type: "string",
      default: "",
    },
  },
  ・・・
}

plugin に組み込む

constructor(props) {
 ・・・
  const dictPath = inkdrop.config.get("switch-note.migemoDictPath");
  if (dictPath != "") {
    const buf = fs.readFileSync(dictPath);
    const dict = new migemo.CompactDictionary(buf.buffer);
    const jsm = new migemo.Migemo();
    jsm.setDict(dict);
    this.jsm = jsm;
  }
}

searchNotes = (_options, query) => {
  // Lowercase the query to make searching easier.
  if (this.state.all == null) {
    return [];
  }

  query = query.toLowerCase();
  // use migemo
  if (this.jsm != null) {
    const regex = new RegExp(this.jsm.query(query));
    return this.state.all.filter((option) => {
      const text = option.text.toLowerCase();
      if (regex.test(text)) {
        return true;
      }
    });
  }

  // use lower includes
  return this.state.all.filter((option) => {
    const text = option.text.toLowerCase();
    if (text.includes(query)) {
      return true;
    }
  });

まとめ

べんり。