Skip to content

Commit b96db71

Browse files
committed
fix: allow to use multiple comment
Before: This filter rule go unrecognized. <!-- textlint-disable preset-ja-technical-writing/max-ten --> <!-- Test Text -->
1 parent 3ee37f5 commit b96db71

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

src/parse-comment.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@ const HTML_COMMENT_REGEXP = /<!--((?:.|\s)*?)-->/g;
44
export function isHTMLComment(htmlString) {
55
return HTML_COMMENT_REGEXP.test(htmlString);
66
}
7+
78
/**
89
* get comment value from html comment tag
910
* @param {string} commentValue <!-- comment -->
11+
* @returns {string[]}
1012
*/
11-
export function getValueFromHTMLComment(commentValue) {
12-
return commentValue.replace(HTML_COMMENT_REGEXP, "$1");
13+
export function getValuesFromHTMLComment(commentValue) {
14+
const results = [];
15+
commentValue.replace(HTML_COMMENT_REGEXP, function(all, comment){
16+
results.push(comment);
17+
});
18+
return results;
1319
}
1420
/**
1521
* Parses a config of values separated by comma.
1622
* @param {string} string The string to parse.
1723
* @returns {Object} Result map of values and true values
1824
*/
1925
export function parseListConfig(string) {
20-
var items = {};
26+
const items = {};
2127

2228
// Collapse whitespace around ,
2329
string = string.replace(/\s*,\s*/g, ",");
24-
2530
string.split(/,+/).forEach(function (name) {
2631
name = name.trim();
2732
if (!name) {

src/textlint-filter-rule-comments.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// LICENSE : MIT
22
"use strict";
33
import StatusManager from "./StatusManager";
4-
import {parseRuleIds, getValueFromHTMLComment, isHTMLComment} from "./parse-comment";
4+
import {parseRuleIds, getValuesFromHTMLComment, isHTMLComment} from "./parse-comment";
55
const defaultOptions = {
66
// enable comment directive
77
// if comment has the value, then enable textlint rule
@@ -10,7 +10,7 @@ const defaultOptions = {
1010
// if comment has the value, then disable textlint rule
1111
"disablingComment": "textlint-disable"
1212
};
13-
module.exports = function (context, options = defaultOptions) {
13+
module.exports = function(context, options = defaultOptions) {
1414
const {Syntax, shouldIgnore, getSource} = context;
1515

1616
const enablingComment = options.enablingComment || defaultOptions.enablingComment;
@@ -42,14 +42,16 @@ This is ignored.
4242
if (!isHTMLComment(nodeValue)) {
4343
return;
4444
}
45-
const commentValue = getValueFromHTMLComment(nodeValue);
46-
if (commentValue.indexOf(enablingComment) !== -1) {
47-
const configValue = commentValue.replace(enablingComment, "");
48-
statusManager.enableReporting(node, parseRuleIds(configValue));
49-
} else if (commentValue.indexOf(disablingComment) !== -1) {
50-
const configValue = commentValue.replace(disablingComment, "");
51-
statusManager.disableReporting(node, parseRuleIds(configValue));
52-
}
45+
const comments = getValuesFromHTMLComment(nodeValue);
46+
comments.forEach(commentValue => {
47+
if (commentValue.indexOf(enablingComment) !== -1) {
48+
const configValue = commentValue.replace(enablingComment, "");
49+
statusManager.enableReporting(node, parseRuleIds(configValue));
50+
} else if (commentValue.indexOf(disablingComment) !== -1) {
51+
const configValue = commentValue.replace(disablingComment, "");
52+
statusManager.disableReporting(node, parseRuleIds(configValue));
53+
}
54+
});
5355
},
5456
[Syntax.Comment](node){
5557
const commentValue = node.value || "";

test/textlint-filter-rule-comments-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@ This is ignored.
3333
});
3434
});
3535
});
36+
context("when multi-line comments", function () {
37+
it("should be ignored", function () {
38+
const textlint = new TextLintCore();
39+
textlint.setupRules({
40+
report: reportRule
41+
}, {
42+
report: {
43+
nodeTypes: [TextLintNodeType.Str]
44+
}
45+
});
46+
textlint.setupFilterRules({
47+
filter: filterRule
48+
});
49+
return textlint.lintMarkdown(`
50+
<!-- textlint-disable -->
51+
<!-- This is comment -->
52+
53+
This is ignored.
54+
55+
<!-- textlint-enable -->
56+
`).then(({messages}) => {
57+
assert.equal(messages.length, 0);
58+
});
59+
});
60+
});
3661
context("when during disable -- enable", function () {
3762
it("should messages is ignored between disable and enable", function () {
3863
const textlint = new TextLintCore();

0 commit comments

Comments
 (0)