Skip to content

Commit

Permalink
import from textlint/textlint
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jan 7, 2024
0 parents commit 94db072
Show file tree
Hide file tree
Showing 17 changed files with 8,218 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: test
on:
push:
pull_request:
permissions:
contents: read
jobs:
test:
name: "Test on Node.js ${{ matrix.node-version }} OS: ${{matrix.os}}"
runs-on: ${{matrix.os}}
strategy:
matrix:
os: [ macos-latest, windows-latest, ubuntu-latest ]
node-version: [ 18, 20 ]
steps:
- name: checkout
uses: actions/checkout@v4
- name: setup Node ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install
run: npm ci
- name: Test
run: npm test
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
test.md
webdb.yml

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

# Optional npm cache directory
.npm
4 changes: 4 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"forbidOnly": true,
"spec": ["test/**/*.js"]
}
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# gulp-textlint

gulp plugin for [textlint](https://github.com/textlint/textlint).

* Require [.textlintrc](https://github.com/textlint/textlint#textlintrc)
* Require [rules](https://github.com/textlint/textlint#rule-list---collection-of-textlint-rule)
* As you can use [vvakame/prh](https://github.com/vvakame/prh)'s format dictionary.

## Usage

```js
var gulp = require('gulp');
var textlint = require('gulp-textlint');

gulp.task('textlint', function() {
return gulp.src('./path/to/src/**/*.md')
.pipe(textlint());
});
```

As you can path to config for textlint. Like below.

```js
...
gulp.task('textlint', function() {
return gulp.src('./path/to/src/**/*.md')
.pipe(textlint({
formatterName: "pretty-error"
});
});
```
When you would like to change target dynamically you should use [yargs](https://github.com/bcoe/yargs).
e.g.
```js
var argv = require('yargs').argv;

gulp.task('textlint', function() {
var src = argv.t;
return gulp.src(src)
.pipe(textlint());
});

gulp.task('watch', function() {
var src = argv.t;
if (src) {
gulp.watch(src, ['textlint']);
}
});
```
Then execute `watch` task with `-t` option.
```sh
gulp watch -t "./path/to/*.md"
```
## Example
Please See [example/](./example/).
8 changes: 8 additions & 0 deletions example/.textlintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rules": {
"no-todo": true,
"prh": {
"rulePaths" :["./prh.yml"]
}
}
}
13 changes: 13 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Example

Example of gulp-textlint

## Usage

npm run textlint
# or
gulp textlint

## Playground

Add `- [ ]` to Here and run textlint.
6 changes: 6 additions & 0 deletions example/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
const gulp = require("gulp");
const textlint = require("gulp-textlint");
gulp.task("textlint", function () {
return gulp.src("./*.md").pipe(textlint());
});
17 changes: 17 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "example",
"version": "1.0.0",
"private": true,
"scripts": {
"textlint": "gulp textlint",
"test": "npm rm gulp-textlint && npm i --production && npm run textlint"
},
"author": "azu",
"license": "MIT",
"dependencies": {
"gulp": "^3.9.1",
"gulp-textlint": "file:..",
"textlint-rule-no-todo": "^1.0.3",
"textlint-rule-prh": "^2.4.1"
}
}
80 changes: 80 additions & 0 deletions example/prh.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
version: 1

# もし、もっと別なルールを使いたい時は以下のURLを参照して真似してください。
# https://github.com/vvakame/prh/tree/master/misc
# おすすめは techbooster.yml です。

# 別の設定ファイルを読み込み、mergeすることもできます。
imports:
# - ./techbooster.yml

rules:

# 大文字小文字全角半角の統一
- expected: Cookie
# 以下と等価 正規表現には強制でgフラグが付く
# - expected: Cookie
# pattern: "/[CcCc][OoOo][OoOo][KkKk][IiIi][EeEe]/g"
# options:
# wordBoundary: false
# specs: []

# 変換結果についてテストも書ける
- expected: jQuery
specs:
- from: jquery
to: jQuery
- from: JQUERY
to: jQuery

# 変換結果が期待通りではなかった場合、ルールのロードに失敗する つまり、ルールのテストが書ける
# - expected: JavaScript
# specs:
# - from: JAVASCRIPT
# to: JavaScprit # この場合はテスト側が間違ってる!
# Error: JavaScript spec failed. "JAVASCRIPT", expected "JavaScprit", but got "JavaScript", /[JjJj][AaAa][VvVv][AaAa][SsSs][CcCc][RrRr][IiIi][PpPp][TtTt]/g

# 表現の統一を図る
- expected: デフォルト
pattern: ディフォルト

# patternは複数記述可能 patterns としてもOK
- expected: ハードウェア
patterns:
- ハードウエアー # 正規表現に変換する都合上、より長いものを先に書いたほうがよい
- ハードウェアー
- ハードウエア

# patternには正規表現が利用可能
- expected: ($1)
pattern: /\(([^)]+)\)/
specs:
# 半角括弧を全角括弧へ
- from: (そのとおり)
to: (そのとおり)

# 否定戻り先読みが欲しいがJSにはない… regexpMustEmptyで、特定のキャプチャグループが空であることを指定して代用とする
- expected: ソフトウェア
pattern: /(日経)?ソフトウエア/
regexpMustEmpty: $1
specs:
# 普通に変換
- from: 広義のソフトウエア
to: 広義のソフトウェア
# 日経ソフトウエア(書名)は変換しない
- from: 日経ソフトウエア
to: 日経ソフトウエア

# 単語境界の区別
- expected: js
# pattern: "/\b[JjJj][SsSs]\b/g" # と等価 \b が前後に付与される
options:
wordBoundary: true
specs:
- from: foo JS bar
to: foo js bar
- from: foo altJS bar
to: foo altJS bar
# 日本語+単語境界の仕様は自分で調べてね…!
- from: 今日もJS祭り
to: 今日もjs祭り
35 changes: 35 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const log = require("fancy-log");
const PluginError = require("plugin-error");
const through = require("through2");
const TextLintEngine = require("textlint").TextLintEngine;

module.exports = function (options) {
options = options || {};
const textlint = new TextLintEngine(options);
const filePaths = [];

return through.obj(
function (file, enc, cb) {
filePaths.push(file.path);
this.push(file);
cb();
},
function (cb) {
const that = this;
textlint
.executeOnFiles(filePaths)
.then(function (results) {
if (textlint.isErrorResults(results)) {
log(textlint.formatResults(results));
that.emit("error", new PluginError("textlint", "Lint failed."));
}
})
.catch(function (error) {
that.emit("error", new PluginError("textlint", `Lint failed. \n${error.message}`));
})
.then(function () {
cb();
});
}
);
};
Loading

0 comments on commit 94db072

Please sign in to comment.