UNPKG

textlint-rule-helper

Version:
104 lines 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ast_node_types_1 = require("@textlint/ast-node-types"); /** * RuleHelper is helper class for textlint. * @class RuleHelper */ var RuleHelper = /** @class */ (function () { /** * Initialize RuleHelper with RuleContext object. * @param ruleContext the ruleContext is context object of the rule. */ function RuleHelper(ruleContext) { this._ruleContext = ruleContext; } /** * Get parents of node. * The parent nodes are returned in order from the closest parent to the outer ones. * {@link node} is not contained in the results. * @param {TxtNode} node the node is start point. */ RuleHelper.prototype.getParents = function (node) { var result = []; var parent = node.parent; while (parent != null) { result.push(parent); parent = parent.parent; } return result; }; /** * Return true if `node` is wrapped any one of node {@link types}. * @param {TxtNode} node is target node * @param {string[]} types are wrapped target node * @returns {boolean} */ RuleHelper.prototype.isChildNode = function (node, types) { var parents = this.getParents(node); var parentsTypes = parents.map(function (parent) { return parent.type; }); return types.some(function (type) { return parentsTypes.some(function (parentType) { return parentType === type; }); }); }; /** * Return true if the node is Str node and fill following conditions * * - the node is Str node * - the node is under the Paragraph node * - the node is not under the BlockQuote * * This function is useful for common use-case. * If you want to lint Str node, but you not want to lint styled node, this function is useful. * styled node is Link, Strong, BlockQuote, Header etc... * Opposite of it, plain str node is just under the Paragraph node. * * @example * * Return true * * --- * str str str * - list text * --- * * Return false * * ---- * # Header * ![alt text](https://example.com) * [link title](https://example.com) * > BlockQuote * **Strong** * [^footnote]: text text * ---- * * Summary: * * Return true if the node is plain text. * In other words, return false if the node is styled * * @param node */ RuleHelper.prototype.isPlainStrNode = function (node) { var _a; if (node.type !== ast_node_types_1.ASTNodeTypes.Str) { return false; } if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) !== ast_node_types_1.ASTNodeTypes.Paragraph) { return false; } var isInUncontrollableNode = this.isChildNode(node, [ // <blockquote> is Block node in html. It can has Pragaraph node as children ast_node_types_1.ASTNodeTypes.BlockQuote ]); return !isInUncontrollableNode; }; return RuleHelper; }()); exports.default = RuleHelper; //# sourceMappingURL=textlint-rule-helper.js.map