textlint/textlint: The pluggable natural language linter for text and markdown.
I will try to make inquiries with api.
Note that an error will occur if the app folder is the same as the package name to be installed later, such as textlint
.
app/
├ .textlintrc //Setting
├ app.js //web main
└ a.py //client
It is appropriate that --save
is good
$ npm init --yes
$ npm install --save-dev express
$ npm install --save-dev textlint
//Rules to check TODO
$ npm install --save-dev textlint-rule-no-todo
//Rules for checking consecutive kanji
$ npm install --save-dev textlint-rule-max-kanji-continuous-len
The current version is as follows.
$ node --version
v10.21.0
$ npm --version
6.14.4
$ rpm list
[email protected]
[email protected]
[email protected]
[email protected]
app.js
const TextLintEngine = require('textlint').TextLintEngine;
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
//Magic to parse json of post data
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
//Wait at port 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 error for other requests
app.use((req, res) => {
res.sendStatus(404);
});
See github for each rule for how to write
.textlintrc
{
"filters": {},
"rules": {
"no-todo": true,
"max-kanji-continuous-len": true,
}
}
Apply lint to the string of the text variable
a.py
import json
import urllib.request
url = 'http://localhost:8080/api/textlint'
text = '''
TODO: this is TODO
1 2 3 4 5 6
'''.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 or more kanji in a row:1 2 3 4 5 6",
"index": 19,
"line": 2,
"column": 1,
"severity": 2
}
]
}
Of course you can also call it from the command line
$ curl -X POST -H "Content-Type: application/json" \
-d '{"text":"TODO: this is TODO\n 1 2 3 4 5 6"}' \
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 or more kanji in a row:1 2 3 4 5 6",
"index": 19,
"line": 2,
"column": 1,
"severity": 2
}
]
}
Building a local server with Node \ .js and Express \ (1 ) ―Introduction of Node \ .js and npm― -Qiita Run textlint from code -Qiita Run textlint in nodejs / express environment [node \ .js -Textlint execution result is empty -Stack overflow](https://ja.stackoverflow.com/questions/60015/textlint-%E3%81%AE%E5%AE%9F % E8% A1% 8C% E7% B5% 90% E6% 9E% 9C% E3% 81% 8C% E7% A9% BA% E3% 81% AB% E3% 81% AA% E3% 82% 8B / 70543 ) Inquire to WebAPI after getting POST data with Node \ .js \ + Express -Qiita Understanding body \ -parser of node \ .js \ (express ) -Qiita
Recommended Posts