みつきんのメモ

組み込みエンジニアです。Interface誌で「Yocto Projectではじめる 組み込みLinux開発入門」連載中

Ubuntu 15.04でtextlint

Twitterでtextlintが良さげと流れてきたので、Ubuntu15.04に導入してみる。

npmでインストールするのだが、apt-getでインストールされるバージョンでは古く、多少手順が必要だったためメモっておく。

npmのインストール

Ubuntuに最新のNode.jsを難なくインストールするの手順でnpmの最新版をインストールする。

  1. apt-getでnodejsとnpmをインストールする。
  2. npmでnパッケージをインストールする。
  3. nパッケージでnodeをインストールする。
  4. apt-getでインストールしたnodejsとnpmを削除する。

実際には以下のような感じになる。

$ sudo apt-get install -y nodejs npm
$ sudo npm cache clean
$ sudo npm install n -g
$ sudo n stable
$ sudo ln -sf /usr/local/bin/node /usr/bin/node
$ sudo apt-get purge -y nodejs npm

apt-getのnodejsとnpmは/usr/binにインストールされるが、 nパッケージの実行ファイルは/usr/local/binにインストールされる。

textlintのインストール

textlint 6.0リリース。--fixでの自動修正に対応にまとまったインストール手順が載っていたので、参考にする。

textlintとルールプリセットのインストール

以下のコマンドでtextlint本体と3つのルールプリセットをインストールする。

$ cd ~/
$ npm i -D textlint textlint-rule-preset-jtf-style textlint-rule-spellcheck-tech-word textlint-rule-common-misspellings

インストールするルールプリセットは次の3つ。

  1. preset-jtf-style
  2. spellcheck-tech-word
  3. common-misspellings

これらは--fixによる自動修正に対応している。

設定ファイルの作成

以下のコマンドで空の.textlintrcが作成される。

$ $(npm bin)/textlint --init

$(npm bin)でnpmでインストールされたコマンドのパスを解決する。

生成されたファイルを以下の内容に編集する。

{
  "rules": {
    "preset-jtf-style": true,
    "spellcheck-tech-word": true,
    "common-misspellings": true
  }
}

textlintの実行

実行してみる。

$ $(npm bin)/textlint ./textlint.md

結果

/home/mickey/memo/textlint.md
   3:1   ✓ error  twitter => Twitter                                          spellcheck-tech-word
  52:48  ✓ error  原則として、全角文字と半角文字の間にスペースを入れません。  preset-jtf-style/3.1.1.全角文字と半角文字の間

✖ 2 problems (2 errors, 0 warnings)
✓ 2 fixable problems.
Try to run: $ textlint --fix [file]

すばらしい。