はてブの記事を管理しているgithubに文章校正ツール(textlint)を導入した。
前提
私ははてブの記事をgithubで管理している。
文章フォーマットを統一させるため、校正ツールのtextlintを使ってみた。
詳細
textlintは校正パターンがいくつかあり、今回は技術文書向けのものを利用する。
ルールを1から自分で考え作るのは大変なので、プリセットを使った。
導入
導入は簡単。
npm i textlint textlint-rule-preset-ja-technical-writing
ルールの適応を設定する。
plugins: filters: rules: present-ja-technical-writing: true
設定ファイルは .json
でも書けるが、コメントも残したいのでyamlにした。
実行も簡単。
npx textlint ./entry/記事.md /Users/username/blog/marty-martini.hatenablog.com/entry/hoge.md 27:5 error 文末が"。"で終わっていません。 ja-technical-writing/ja-no-mixed-period 51:6 error 文末が"。"で終わっていません。 ja-technical-writing/ja-no-mixed-period
こんな感じで指摘してくれる。
CIに導入する
ローカルでチェックするのも良いが、CIに導入して自動化したい。
ついでに、指摘もわかりやすくしてほしい。
導入した結果はこちら
チェック結果は 、reviewdog/action-setup: :dog: Setup reviewdog actionでPRにコメントを残す。
設定したyamlはここ
name: textlint on: pull_request: branches: - main paths: - 'entries/**/*.md' jobs: linter: permissions: checks: write contents: read pull-requests: write runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - uses: actions/setup-node@v4 with: node-version: 20 cache: 'npm' - uses: reviewdog/action-setup@v1 - name: node dependencies run: | npm ci - name: textlint & reviewdog env: REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | DIFF_FILES=`git diff --name-status origin/main --diff-filter=MA | grep -E "entries/.*/*.md" | cut -f2` if [ -z "${DIFF_FILES}" ]; then exit 0; fi npx textlint -f checkstyle $(echo ${DIFF_FILES}) | reviewdog -f=checkstyle -name="textlint" -reporter=github-pr-review --fail-on-error=true -filter-mode=added
ポイント
- 記事の更新がある場合、実行する
git diff
で差分のファイルを抽出する- 差分ファイルのみ、textlintに渡す
- textlintをパイプで繋いで、
reviewdog
に渡す
まとめ
文章校正ツールによって文章が矯正されるけど、いい方向に矯正されるのでよし。 ちょっと気になるところもあるけど、textlint/textlint-filter-rule-commentsやtextlint-filter-rule-allowlistを使って除外すれば良いので、気になれば調べてほしい。