Add noInlineConfig option to disable inline config comments.

This commit is contained in:
David Anson 2017-05-21 22:58:10 -07:00
parent ac6b80b42a
commit 614ac8fa23
4 changed files with 110 additions and 45 deletions

View file

@ -56,7 +56,7 @@
"max-len": "error",
"max-lines": "off",
"max-nested-callbacks": "error",
"max-params": ["error", 6],
"max-params": ["error", 7],
"max-statements": ["error", 30],
"max-statements-per-line": "error",
"multiline-ternary": "off",

View file

@ -206,34 +206,6 @@ Example:
}
```
##### options.frontMatter
Type: `RegExp`
Matches any [front matter](https://jekyllrb.com/docs/frontmatter/) found at the
beginning of a file.
Some Markdown content begins with metadata; the default `RegExp` for this option
ignores common forms of "front matter". To match differently, specify a custom
`RegExp` or use the value `null` to disable the feature.
Note: Matches must occur at the start of the file.
Default:
```js
/^---$[^]*?^---$(\r\n|\r|\n)/m
```
Ignores:
```text
---
layout: post
title: Title
---
```
##### options.config
Type: `Object` mapping `String` to `Boolean | Object`
@ -318,6 +290,45 @@ var options = {
};
```
##### options.frontMatter
Type: `RegExp`
Matches any [front matter](https://jekyllrb.com/docs/frontmatter/) found at the
beginning of a file.
Some Markdown content begins with metadata; the default `RegExp` for this option
ignores common forms of "front matter". To match differently, specify a custom
`RegExp` or use the value `null` to disable the feature.
Note: Matches must occur at the start of the file.
Default:
```js
/^---$[^]*?^---$(\r\n|\r|\n)/m
```
Ignores:
```text
---
layout: post
title: Title
---
```
##### options.noInlineConfig
Type: `Boolean`
Disables the use of HTML comments like `<!-- markdownlint-disable -->` to toggle
rules within the body of Markdown content.
By default, properly-formatted inline comments can be used to create exceptions
for parts of a document. Setting `noInlineConfig` to `true` ignores all such
comments.
##### options.resultVersion
Type: `Number`

View file

@ -84,7 +84,8 @@ function uniqueFilterForSortedErrors(value, index, array) {
}
// Lints a single string
function lintContent(content, config, frontMatter, resultVersion) {
function lintContent(
content, config, frontMatter, noInlineConfig, resultVersion) {
// Remove UTF-8 byte order marker (if present)
if (content.charCodeAt(0) === 0xfeff) {
content = content.slice(1);
@ -192,6 +193,7 @@ function lintContent(content, config, frontMatter, resultVersion) {
}
var enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length);
lines.forEach(function forLine(line) {
if (!noInlineConfig) {
var match = shared.inlineCommentRe.exec(line);
if (match) {
enabledRules = shared.clone(enabledRules);
@ -200,6 +202,7 @@ function lintContent(content, config, frontMatter, resultVersion) {
match = shared.inlineCommentRe.exec(line);
}
}
}
enabledRulesPerLineNumber.push(enabledRules);
});
// Create parameters for rules
@ -303,6 +306,7 @@ function lintFile(
file,
config,
frontMatter,
noInlineConfig,
resultVersion,
synchronous,
callback) {
@ -310,7 +314,8 @@ function lintFile(
if (err) {
return callback(err);
}
var result = lintContent(content, config, frontMatter, resultVersion);
var result = lintContent(
content, config, frontMatter, noInlineConfig, resultVersion);
callback(null, result);
}
// Make a/synchronous call to read file
@ -347,9 +352,10 @@ function markdownlint(options, callback) {
files = [ String(options.files) ];
}
var strings = options.strings || {};
var config = options.config || { "default": true };
var frontMatter = (options.frontMatter === undefined) ?
shared.frontMatterRe : options.frontMatter;
var config = options.config || { "default": true };
var noInlineConfig = !!options.noInlineConfig;
var resultVersion = options.resultVersion || 0;
var synchronous = (callback === markdownlintSynchronousCallback);
var results = new Results();
@ -357,7 +363,13 @@ function markdownlint(options, callback) {
function lintFilesArray() {
var file = files.shift();
if (file) {
lintFile(file, config, frontMatter, resultVersion, synchronous,
lintFile(
file,
config,
frontMatter,
noInlineConfig,
resultVersion,
synchronous,
function lintedFile(err, result) {
if (err) {
return callback(err);
@ -376,6 +388,7 @@ function markdownlint(options, callback) {
strings[key] || "",
config,
frontMatter,
noInlineConfig,
resultVersion);
results[key] = result;
});

View file

@ -83,11 +83,15 @@ module.exports.projectFiles = function projectFiles(test) {
test.expect(2);
var options = {
"files": [ "README.md" ],
"config": { "MD013": { "line_length": 150 } }
"noInlineConfig": true,
"config": {
"MD013": { "line_length": 150 },
"MD024": false
}
};
markdownlint(options, function callback(err, actual) {
test.ifError(err);
var expected = { "README.md": { "MD024": [ 392, 398 ] } };
var expected = { "README.md": {} };
test.deepEqual(actual, expected, "Issue(s) with project files.");
test.done();
});
@ -762,10 +766,42 @@ module.exports.customFrontMatter = function customFrontMatter(test) {
});
};
module.exports.noInlineConfig = function noInlineConfig(test) {
test.expect(2);
markdownlint({
"strings": {
"content": [
"# Heading",
"",
"\tTab",
"",
"<!-- markdownlint-disable-->",
"",
"\tTab",
"",
"<!-- markdownlint-enable-->",
"",
"\tTab"
].join("\n")
},
"noInlineConfig": true
}, function callback(err, result) {
test.ifError(err);
var expectedResult = {
"content": {
"MD010": [ 3, 7, 11 ]
}
};
test.deepEqual(result, expectedResult, "Undetected issues.");
test.done();
});
};
module.exports.readmeHeaders = function readmeHeaders(test) {
test.expect(2);
markdownlint({
"files": "README.md",
"noInlineConfig": true,
"config": {
"default": false,
"MD013": {
@ -786,8 +822,9 @@ module.exports.readmeHeaders = function readmeHeaders(test) {
"#### options",
"##### options.files",
"##### options.strings",
"##### options.frontMatter",
"##### options.config",
"##### options.frontMatter",
"##### options.noInlineConfig",
"##### options.resultVersion",
"#### callback",
"#### result",
@ -803,7 +840,7 @@ module.exports.readmeHeaders = function readmeHeaders(test) {
}
}, function callback(err, result) {
test.ifError(err);
var expected = { "README.md": { "MD024": [ 392, 398 ] } };
var expected = { "README.md": {} };
test.deepEqual(result, expected, "Unexpected issues.");
test.done();
});
@ -827,10 +864,14 @@ module.exports.filesArrayAsString = function filesArrayAsString(test) {
test.expect(2);
markdownlint({
"files": "README.md",
"config": { "MD013": { "line_length": 150 } }
"noInlineConfig": true,
"config": {
"MD013": { "line_length": 150 },
"MD024": false
}
}, function callback(err, actual) {
test.ifError(err);
var expected = { "README.md": { "MD024": [ 392, 398 ] } };
var expected = { "README.md": {} };
test.deepEqual(actual, expected, "Unexpected issues.");
test.done();
});