0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

QiitaがCLIを提供するようになったので文章校正を自動化しておく

Last updated at Posted at 2023-08-20

Qiita CLI が誕生したことで Qiita の記事を GitHub で管理できるようになり、github actions で記事公開までができるようになりました。

詳しくはこちら。

文章校正には textlint を使用します。
textlint は技術文書のルールプリセット textlint-rule-preset-ja-technical-writing を使用します。

パターン① github actions

main ブランチに対して Pull Request すると github actions で textlintreviewdog を動かします。reviewdogtextlint での指摘事項を Pull Request に書き込んでくれる仕組みです。

詳しくはこちら。(ほぼ丸々流用させていただきました。)

github actions 詳細
name: 'textlint & reviewdog'

on:
  pull_request:
    branches:
      - main
    paths:
      - 'public/*.md'

jobs:
  linter:
    permissions:
      checks: write
      contents: read
      pull-requests: write
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: 'Setup nodejs'
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: 'npm'

      - name: 'Setup reviewdog'
        uses: reviewdog/action-setup@v1
        with:
          reviewdog_version: latest

      - name: 'Install node dependencies'
        run: npm ci

      - name: 'textlint & reviewdog'
        env:
          REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        if: ${{ (github.event_name == 'pull_request') }}
        run: |
          DIFF_FILES=`git diff --name-status origin/main --diff-filter=MA | grep -E "public/.*.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

パターン② husky pre-commit

記事を個人で管理している場合、いちいち Pull Request するのが面倒臭いです。
その場合、huskypre-commit を起動し textlint を実行する方法も紹介します。なお textlint は変更のあった markdown (*.md) のみを対象にしています。

pre-commit 詳細

.husky/pre-commit

#!/usr/bin/env sh
. "$(dirname "$0")/_/husky.sh"

echo "Run textlint..."
DIFF_FILES=`git diff --name-status origin/main --diff-filter=MA | grep -E ".*.md" | cut -f2`
if [ -z "${DIFF_FILES}" ]; then exit 0; fi
npx textlint $(echo ${DIFF_FILES})

その他

自分のエディタに prettier も設定しておくとさらに快適になります。(ここでは割愛)

textlintrc.json の設定は Qiita なので少し緩めに設定しておきます。

textlintrc.json 詳細
{
  "plugins": {},
  "filters": {},
  "rules": {
    // 技術文書向けのtextlintルールプリセット
    // See https://github.com/textlint-ja/textlint-rule-preset-ja-technical-writing
    "preset-ja-technical-writing": {
      "max-kanji-continuous-len": {
        "max": 6,
        "allow": ["倍精度浮動小数点数"]
      },
      "sentence-length": {
        "max": 125 // 1行あたりの文字数
      },
      "ja-no-mixed-period": {
        "allowPeriodMarks": [":"], // 文末が ":" で終わることを許可
        "allowEmojiAtEnd": true, // 文末が絵文字で終わることを許可
        "forceAppendPeriod": true
      },
      "no-exclamation-question-mark": false // 感嘆符(!!)、疑問符(??)で文章が終わることを許可
    }
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?