LoginSignup
4
4

More than 3 years have passed since last update.

簡単なtextlintサーバーを作る

Posted at

textlint/textlint: The pluggable natural language linter for text and markdown.

より、apiで問い合わせられるようにしてみる。

ファイル構成

appフォルダーをtextlintなどこの後にインストールするパッケージ名と同じにするとエラーになるので注意。

app/
  ├ .textlintrc // 設定
  ├ app.js      // webメイン
  └ a.py        // クライアント

パッケージのインストール

--saveが良いとかは適宜

$ npm init --yes
$ npm install --save-dev express
$ npm install --save-dev textlint
// TODOをチェックするルール
$ npm install --save-dev textlint-rule-no-todo
// 連続する漢字をチェックするルール
$ npm install --save-dev textlint-rule-max-kanji-continuous-len

現在のバージョンは以下の通り。

$ node --version
v10.21.0
$ npm --version
6.14.4
$ rpm list
express@4.17.1
textlint@11.7.6
textlint-rule-no-todo@2.0.1
textlint-rule-max-kanji-continuous-len@1.1.1

ファイル

app.js
const TextLintEngine = require('textlint').TextLintEngine;
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

// postデータのjsonをパースするおまじない
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());

// 8080番ポートで待ちうける
app.listen(8080, () => {
    console.log('Running at Port 8080...');
});

app.post('/api/textlint', (req, res, next) => {
    const req_text = req.body.text;
    const engine = new TextLintEngine();
    engine.executeOnText(req_text).then(results => {
        res.json({
            messages: results[0].messages
        });
    });
});

// その他のリクエストに対する404エラー
app.use((req, res) => {
    res.sendStatus(404);
});

書き方は各ルールのgithub参照

.textlintrc
{
  "filters": {},
  "rules": {
    "no-todo": true,
    "max-kanji-continuous-len": true,
  }
}

text変数の文字列に対してlintをかける

a.py
import json
import urllib.request

url = 'http://localhost:8080/api/textlint'
text = '''
TODO: this is TODO
一二三四五六
'''.strip()

data = {"text": text}
headers = {
    'Content-Type': 'application/json',
}
req = urllib.request.Request(url, json.dumps(data).encode(), headers)
try:
    with urllib.request.urlopen(req) as res:
        body = json.load(res)
        json_str = json.dumps(body)
        print(json_str)
except:
    print("fail")

サーバーを起動

$ node app.js
Running at Port 8080...

リクエストしてみる

$ python3 a.py | jq .
{
  "messages": [
    {
      "type": "lint",
      "ruleId": "no-todo",
      "message": "Found TODO: 'TODO: this is TODO'",
      "index": 0,
      "line": 1,
      "column": 1,
      "severity": 2
    },
    {
      "type": "lint",
      "ruleId": "max-kanji-continuous-len",
      "message": "漢字が6つ以上連続しています: 一二三四五六",
      "index": 19,
      "line": 2,
      "column": 1,
      "severity": 2
    }
  ]
}

もちろんコマンドラインからも呼べる

$ curl -X POST -H "Content-Type: application/json" \
  -d '{"text":"TODO: this is TODO\n一二三四五六"}'  \
  localhost:8080/api/textlint | jq .
{
  "messages": [
    {
      "type": "lint",
      "ruleId": "no-todo",
      "message": "Found TODO: 'TODO: this is TODO'",
      "index": 0,
      "line": 1,
      "column": 1,
      "severity": 2
    },
    {
      "type": "lint",
      "ruleId": "max-kanji-continuous-len",
      "message": "漢字が6つ以上連続しています: 一二三四五六",
      "index": 19,
      "line": 2,
      "column": 1,
      "severity": 2
    }
  ]
}

参考情報

Node.jsとExpressでローカルサーバーを構築する(1) ―Node.jsとnpmの導入― - Qiita
コードからtextlintを実行する - Qiita
textlintをnodejs/express環境で動作させる
node.js - Textlint の実行結果が空になる - スタック・オーバーフロー
Node.js + Express でPOSTデータを取得後、WebAPIへ問い合わせる - Qiita
node.js(express)のbody-parserを理解する - Qiita

4
4
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
4
4