textlint を試す

textlint を使って文章校正するメモ。

以下を参考に実施。

プロジェクトを初期化する。

npm init --yes

npmlint をインストールする。

npm install textlint

ルールを導入する。

npm install textlint-filter-rule-comments
npm install textlint-rule-preset-ja-spacing
npm install textlint-rule-preset-ja-technical-writing
npm install textlint-rule-preset-japanese
npm install textlint-rule-prh

.textlintrc ファイルを作成する。

cat << EOF > .textlintrc
{
    "plugins": {
        "@textlint/markdown": {
            "extensions": [
                ".md"
            ]
        }
    },
    "filters": {
        "comments": true
    },
    "rules": {
        "preset-ja-technical-writing": {
            "no-exclamation-question-mark": {
                "allowFullWidthExclamation": true,
                "allowFullWidthQuestion": true
            },
            "no-doubled-joshi": {
                "strict": false,
                "allow": [
                    ""
                ]
            }
        },
        "ja-technical-writing/ja-no-mixed-period": {
            "allowPeriodMarks": [
                ":"
            ]
        },
        "ja-technical-writing/sentence-length": false,
        "preset-ja-spacing": {
            "ja-space-between-half-and-full-width": {
                "space": "always",
                "exceptPunctuation": true
            },
            "ja-space-around-code": {
                "before": true,
                "after": true
            }
        },
        "prh": {
            "rulePaths" :["./prh.yml"]
        }
    }
}
EOF

prh.yaml を作成する。

cat << EOF > prh.yml
version: 1
rules:
  - expected: FireLens
    pattern:  /firelens|Firelens/
  - expected: リポジトリ
    pattern:  /リポジトリー|レポジトリ|レポジトリー/
EOF

テスト用に引っかかりそうな言葉を入れておく。

spaceスペースなし

しゃべれる.

食べれる。

textlint を実行する。

$ npx textlint README.md

/Users/sotosugi/workspace/2022/blog/20221226_textlint/README.md
   99:5   ✓ error  原則として、全角文字と半角文字の間にスペースを入れます。  ja-spacing/ja-space-between-half-and-full-width
   99:11  error    文末が""で終わっていません。                            ja-technical-writing/ja-no-mixed-period
  101:6   ✓ error  文末が""で終わっていません。                            ja-technical-writing/ja-no-mixed-period
  103:3   error    ら抜き言葉を使用しています。                              ja-technical-writing/no-dropping-the-ra

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

参考

某ドキュメント翻訳プロジェクトでは GitHub Actions で以下のような設定をしていた。

package.json

{
    "name": "temp-hogehoge-doc-ja",
    "version": "1.0.0",
    "description": "",
    "scripts": {
        "lint": "./node_modules/.bin/textlint"
    },
    "dependencies": {
        "textlint": "^11.9.0",
        "textlint-filter-rule-comments": "^1.2.2",
        "textlint-rule-preset-ja-spacing": "^2.0.2",
        "textlint-rule-preset-ja-technical-writing": "^4.0.1",
        "textlint-rule-preset-japanese": "^5.0.0",
        "textlint-rule-prh": "^5.3.0"
    }
}

.github/workflows/main.yml

name: Text Lint and Auto Request Review

on:
  pull_request:
  workflow_dispatch:

jobs:
  text-lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2
      - name: Setup Node.js environment
        uses: actions/setup-node@v2.1.2
        with:
          node-version: "14"
      - name: Install dependencies
        run: npm ci
      - name: Exec textlint
        uses: tsuyoshicho/action-textlint@v1
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          reporter: github-pr-review
          level: warning
          textlint_flags: "site/ja/**"
  auto-request-review:
    if: ${{ toJson(github.event.pull_request.requested_reviewers) == '[]' }}
    runs-on: ubuntu-latest
    steps:
      - name: Assign reviewers
        uses: kentaro-m/auto-assign-action@v1.1.2
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          configuration-path: ".github/reviewers.yml"

prh.yml

version: 1
rules:
  - expected: FireLens
    pattern:  /firelens|Firelens/
  - expected: リポジトリ
    pattern:  /リポジトリー|レポジトリ|レポジトリー/

.textlintrc

{
    "plugins": {
        "@textlint/markdown": {
            "extensions": [
                ".md"
            ]
        }
    },
    "filters": {
        "comments": true
    },
    "rules": {
        "preset-ja-technical-writing": {
            "no-exclamation-question-mark": {
                "allowFullWidthExclamation": true,
                "allowFullWidthQuestion": true
            },
            "no-doubled-joshi": {
                "strict": false,
                "allow": [
                    ""
                ]
            }
        },
        "ja-technical-writing/ja-no-mixed-period": {
            "allowPeriodMarks": [
                ":"
            ]
        },
        "ja-technical-writing/sentence-length": false,
        "preset-ja-spacing": {
            "ja-space-between-half-and-full-width": {
                "space": "always",
                "exceptPunctuation": true
            },
            "ja-space-around-code": {
                "before": true,
                "after": true
            }
        },
        "prh": {
            "rulePaths" :["./prh.yml"]
        }
    }
}