textlint
Version:
The pluggable linting tool for text and markdown.
209 lines • 7.92 kB
JavaScript
// LICENSE : MIT
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextLintCore = void 0;
/*
textlint-core.js is a class
textlint.js is a singleton object that is instance of textlint-core.js.
*/
const kernel_1 = require("@textlint/kernel");
const kernel_2 = require("@textlint/kernel");
const fs_1 = __importDefault(require("fs"));
const object_to_kernel_format_1 = require("../util/object-to-kernel-format");
const textlint_plugin_text_1 = __importDefault(require("@textlint/textlint-plugin-text"));
const textlint_plugin_markdown_1 = __importDefault(require("@textlint/textlint-plugin-markdown"));
const path_1 = __importDefault(require("path"));
const readFile = fs_1.default.promises.readFile;
const feature_flag_1 = require("@textlint/feature-flag");
const logger_1 = require("../util/logger");
/**
* @class {TextLintCore}
* @deprecated use new APIs https://textlint.github.io/docs/use-as-modules.html#new-apis
*/
class TextLintCore {
// TODO: can not show deprecated message on constructor
// because, constructor is called in `textlint` singleton object.
constructor(config = {}) {
// this.config often is undefined.
this.config = config;
// Markdown and Text is enabled by default
// Markdown and Text are for backward compatibility.
this.defaultPlugins = [
{
pluginId: "markdown",
plugin: textlint_plugin_markdown_1.default
},
{
pluginId: "text",
plugin: textlint_plugin_text_1.default
}
];
// TODO: remove `config`
// https://github.com/textlint/textlint/issues/296
this.kernel = new kernel_2.TextlintKernel(config);
this.textlintKernelDescriptor = new kernel_1.TextlintKernelDescriptor({
rules: [],
plugins: this.defaultPlugins,
filterRules: []
});
}
/**
* Use setupPlugins insteadof it.
*
* ````
* textlint.setupPlugins({
* yourPluginName: yourPlugin
* });
* ````
*
* @param {*} Processor
* @deprecated
*
* It will be removed until textlint@10
*/
addProcessor(Processor) {
(0, feature_flag_1.throwIfTesting)("Use setupPlugins insteadof addProcessor method.`addProcessor` will be removed in the future." +
"For more details, See https://github.com/textlint/textlint/issues/293");
this.textlintKernelDescriptor = this.textlintKernelDescriptor.shallowMerge({
plugins: [
{
pluginId: `${Processor.name} `,
plugin: { Processor }
}
].concat(this.defaultPlugins)
});
}
/**
* register Processors
* @param {Object} plugins
* @param {Object} [pluginsConfig]
*/
setupPlugins(plugins = {}, pluginsConfig = {}) {
// Append default plugin to the plugins list.
// Because, default plugin can be override by user plugins
this.textlintKernelDescriptor = this.textlintKernelDescriptor.shallowMerge({
plugins: (0, object_to_kernel_format_1.pluginsObjectToKernelRule)(plugins, pluginsConfig).concat(this.defaultPlugins)
});
}
/**
* Register rules and rulesConfig.
* if want to release rules, please call {@link resetRules}.
* @param {object} rules rule objects array
* @param {object} [rulesOption] ruleConfig is object
*/
setupRules(rules = {}, rulesOption = {}) {
logger_1.Logger.deprecate("TextLintCore is deprecated. Please use new APIs https://github.com/textlint/textlint/issues/1310");
this.textlintKernelDescriptor = this.textlintKernelDescriptor.shallowMerge({
rules: (0, object_to_kernel_format_1.rulesObjectToKernelRule)(rules, rulesOption)
});
}
/**
* Register filterRules and filterRulesConfig.
* if want to release rules, please call {@link resetRules}.
* @param {object} filterRules rule objects array
* @param {object} [filterRulesOption] ruleConfig is object
*/
setupFilterRules(filterRules = {}, filterRulesOption = {}) {
this.textlintKernelDescriptor = this.textlintKernelDescriptor.shallowMerge({
filterRules: (0, object_to_kernel_format_1.filterRulesObjectToKernelRule)(filterRules, filterRulesOption)
});
}
/**
* Remove all registered rule and clear messages.
*/
resetRules() {
this.textlintKernelDescriptor = new kernel_1.TextlintKernelDescriptor({
rules: [],
plugins: this.defaultPlugins,
filterRules: []
});
}
/**
* lint text by registered rules.
* The result contains target filePath and error messages.
* @param {string} text
* @param {string} ext ext is extension. default: .txt
* @returns {Promise.<TextlintResult>}
*/
lintText(text, ext = ".txt") {
const options = this._mergeSetupOptions({
ext
});
return this.kernel.lintText(text, options);
}
/**
* lint markdown text by registered rules.
* The result contains target filePath and error messages.
* @param {string} text markdown format text
* @returns {Promise.<TextlintResult>}
*/
lintMarkdown(text) {
const ext = ".md";
const options = this._mergeSetupOptions({
ext
});
return this.kernel.lintText(text, options);
}
/**
* lint file and return result object
* @param {string} filePath
* @returns {Promise.<TextlintResult>} result
*/
lintFile(filePath) {
const absoluteFilePath = path_1.default.resolve(process.cwd(), filePath);
const ext = path_1.default.extname(absoluteFilePath) || path_1.default.basename(absoluteFilePath);
const options = this._mergeSetupOptions({
ext,
filePath: absoluteFilePath
});
return readFile(absoluteFilePath, { encoding: "utf-8" }).then((text) => {
return this.kernel.lintText(text, options);
});
}
/**
* fix file and return fix result object
* @param {string} filePath
* @returns {Promise.<TextlintFixResult>}
*/
fixFile(filePath) {
const absoluteFilePath = path_1.default.resolve(process.cwd(), filePath);
const ext = path_1.default.extname(absoluteFilePath) || path_1.default.basename(absoluteFilePath);
const options = this._mergeSetupOptions({
ext,
filePath: absoluteFilePath
});
return readFile(absoluteFilePath, { encoding: "utf-8" }).then((text) => {
return this.kernel.fixText(text, options);
});
}
/**
* fix texts and return fix result object
* @param {string} text
* @param {string} ext
* @returns {Promise.<TextlintFixResult>}
*/
fixText(text, ext = ".txt") {
const options = this._mergeSetupOptions({
ext
});
return this.kernel.fixText(text, options);
}
/**
* @private
*/
_mergeSetupOptions(options) {
const configFileBaseDir = typeof this.config.configFile === "string" ? path_1.default.dirname(this.config.configFile) : undefined;
return {
...options,
configBaseDir: configFileBaseDir,
plugins: this.textlintKernelDescriptor.plugin.toKernelPluginsFormat(),
rules: this.textlintKernelDescriptor.rule.toKernelRulesFormat(),
filterRules: this.textlintKernelDescriptor.filterRule.toKernelFilterRulesFormat()
};
}
}
exports.TextLintCore = TextLintCore;
//# sourceMappingURL=textlint-core.js.map