textlint-filter-rule-comments
Version:
textlint rule that ignore texts using comments directive.
66 lines (61 loc) • 1.68 kB
JavaScript
// LICENSE : MIT
;
const HTML_COMMENT_REGEXP = /<!--((?:.|\s)*?)-->/g;
export function isHTMLComment(htmlString) {
return HTML_COMMENT_REGEXP.test(htmlString);
}
/**
* get comment value from html comment tag
* @param {string} commentValue <!-- comment -->
* @returns {string[]}
*/
export function getValuesFromHTMLComment(commentValue) {
const results = [];
commentValue.replace(HTML_COMMENT_REGEXP, function(all, comment){
results.push(comment);
});
return results;
}
/**
* Parses a config of values separated by comma.
* @param {string} string The string to parse.
* @returns {Object} Result map of values and true values
*/
export function parseListConfig(string) {
const items = {};
// Collapse whitespace around ,
string = string.replace(/\s*,\s*/g, ",");
string.split(/,+/).forEach(function (name) {
name = name.trim();
if (!name) {
return;
}
items[name] = true;
});
return items;
}
/**
* parse "textlint-enable aRule, bRule" and return ["aRule", "bRule"]
* @param {string} string
* @returns {string[]}
*/
export function parseRuleIds(string) {
return Object.keys(parseListConfig(string));
}
/**
* Remove trailing description part that follows `--` in comment directives.
* @param {string} string
* @returns {string}
* @example
* removeCommentDescription(" ruleA -- because of reasons"); // => " ruleA "
*/
export function removeCommentDescription(string) {
if (!string) {
return string;
}
const index = string.indexOf("--");
if (index === -1) {
return string;
}
return string.slice(0, index);
}