Permalink
Switch branches/tags
v7.4.0 v7.3.0 v7.2.2 v3.6.0 v3.5.0 v3.3.1 v3.3.0 v3.1.2 v3.1.1 txt-to-ast@3.0.3 txt-to-ast@3.0.2 txt-to-ast@3.0.1 txt-to-ast@3.0.0 txt-to-ast@3.0.0-next.0 txt-to-ast@2.0.0 txt-to-ast@2.0.0-beta.0 txt-ast-traverse@2.0.4 txt-ast-traverse@2.0.3 txt-ast-traverse@2.0.2 txt-ast-traverse@2.0.1 txt-ast-traverse@2.0.0 txt-ast-traverse@2.0.0-next.0 textlint@11.0.0 textlint@10.2.1 textlint@10.2.0 textlint@10.1.5 textlint@10.1.4 textlint@10.1.3 textlint@10.1.2 textlint@10.1.1 textlint@10.1.0 textlint@10.0.1 textlint@10.0.0 textlint@10.0.0-next.2 textlint@10.0.0-next.1 textlint@10.0.0-next.0 textlint@9.1.1 textlint@9.1.0 textlint@9.0.1 textlint@9.0.0 textlint@9.0.0-beta.0 textlint@8.2.1 textlint@8.2.0 textlint@8.1.0 textlint@8.0.1 textlint@8.0.0 @textlint/textlint-plugin-text@4.0.0 @textlint/textlint-plugin-text@3.0.10 @textlint/textlint-plugin-text@3.0.9 @textlint/textlint-plugin-text@3.0.8 @textlint/textlint-plugin-text@3.0.7 @textlint/textlint-plugin-markdown@5.0.0 @textlint/textlint-plugin-markdown@4.0.10 @textlint/textlint-plugin-markdown@4.0.9 @textlint/textlint-plugin-markdown@4.0.8 @textlint/textlint-plugin-markdown@4.0.7 @textlint/text-to-ast@3.0.9 @textlint/text-to-ast@3.0.8 @textlint/text-to-ast@3.0.7 @textlint/text-to-ast@3.0.6 @textlint/text-to-ast@3.0.5 @textlint/text-to-ast@3.0.4 @textlint/monorepo-scripts@2.0.0 @textlint/monorepo-scripts@2.0.0-beta.0 @textlint/markdown-to-ast@6.0.9 @textlint/markdown-to-ast@6.0.8 @textlint/markdown-to-ast@6.0.7 @textlint/markdown-to-ast@6.0.6 @textlint/markdown-to-ast@6.0.5 @textlint/markdown-to-ast@6.0.4 @textlint/linter-formatter@3.0.8 @textlint/linter-formatter@3.0.7 @textlint/linter-formatter@3.0.6 @textlint/linter-formatter@3.0.5 @textlint/linter-formatter@3.0.4 @textlint/linter-formatter@3.0.3 @textlint/kernel@3.0.0 @textlint/kernel@2.0.9 @textlint/kernel@2.0.8 @textlint/kernel@2.0.7 @textlint/kernel@2.0.6 @textlint/kernel@2.0.5 @textlint/kernel@2.0.4 @textlint/kernel@2.0.3 @textlint/kernel@2.0.2 @textlint/kernel@2.0.1 @textlint/kernel@2.0.0 @textlint/kernel@2.0.0-next.2 @textlint/kernel@2.0.0-next.1 @textlint/kernel@2.0.0-next.0 @textlint/kernel@1.0.3 @textlint/kernel@1.0.2 @textlint/kernel@1.0.1 @textlint/kernel@1.0.0 @textlint/kernel@1.0.0-beta.0 @textlint/kernel@0.2.1 @textlint/kernel@0.2.0 @textlint/kernel@0.1.0 @textlint/fixer-formatter@3.0.8 @textlint/fixer-formatter@3.0.7
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
119 lines (86 sloc) 3.38 KB
id title
use-as-modules
Use as Node Modules

Overview

overview

textlint module expose these header at index.js

// Level of abstraction(descending order)
// cli > TextLintEngine > TextLintCore(textlint)
// See: https://github.com/textlint/textlint/blob/master/docs/use-as-modules.md

// Command line interface
export { cli } from "./cli";

// It is a singleton object of TextLintCore
// Recommend: use TextLintCore
export { textlint } from "./textlint";

// TextLintEngine is a wrapper around `textlint` for linting **multiple** files
// include formatter, detecting utils
// <Recommend>: It is easy to use
// You can see engine/textlint-engine-core.js for more detail
export { TextLintEngine } from "./textlint-engine";

// TextFixEngine is a wrapper around `textlint` for linting **multiple** files
// include formatter, detecting utils
// <Recommend>: It is easy to use
// You can see engine/textlint-engine-core.js for more detail
export { TextFixEngine } from "./textfix-engine";

// Core API for linting a **single** text or file.
export { TextLintCore } from "./textlint-core";

Recommend to use TextLintEngine.

Architecture

See README.md for details.

CLI(Command Line Interface)

CLI parse command arguments, and run Engine with the options.

Engine

textlint has two engines TextLintEngine and TextFixEngine.

Both engine

  • Load configuration from .textlintrc.
  • Handle multiple files or text string.
  • Return a array of TextLintResult or TextLintFixResult
    • actually, return a Promise like Promise<TextLintResult[]>

Core

textlint's core

  • Accept configuration as object.
  • Handle a single file or text string.
  • Return TextLintResult or TextLintFixResult
    • actually, return a Promise like Promise<TextLintResult>

Example

Lint files using TextLintEngine:

See examples/use-as-module/index.js

// LICENSE : MIT
"use strict";
const TextLintEngine = require("textlint").TextLintEngine;
const path = require("path");
function lintFile(filePath) {
    /**
     * See lib/_typing/textlint.d.ts
     */
    const options = {
        // load rules from [../rules]
        rules: ["no-todo"],
        formatterName: "pretty-error"
    };
    const engine = new TextLintEngine(options);
    const filePathList = [path.resolve(process.cwd(), filePath)];
    return engine.executeOnFiles(filePathList).then(function(results) {
        if (engine.isErrorResults(results)) {
            const output = engine.formatResults(results);
            console.log(output);
        } else {
            console.log("All Passed!");
        }
    });
}

lintFile(`${__dirname}/README.md`).catch(function(error) {
    console.error(error);
    process.exit(1);
});

Testing

You can use textlint-tester for testing your custom rule.

Consult link: