mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add markdownlint-disable-file/markdownlint-enable-file inline comments (fixes #215).
This commit is contained in:
parent
c0f040e5c1
commit
a9251c533f
5 changed files with 85 additions and 27 deletions
18
README.md
18
README.md
|
@ -141,11 +141,11 @@ Two kinds of text are ignored:
|
||||||
|
|
||||||
Rules can be enabled, disabled, and configured via `options.config` (described
|
Rules can be enabled, disabled, and configured via `options.config` (described
|
||||||
below) to define the expected behavior for a set of inputs. To enable or disable
|
below) to define the expected behavior for a set of inputs. To enable or disable
|
||||||
rules within a file, add one of these markers to the appropriate place (HTML
|
rules at a particular location within a file, add one of these markers to the
|
||||||
comments don't appear in the final markup):
|
appropriate place (HTML comments don't appear in the final markup):
|
||||||
|
|
||||||
* Disable all rules unconditionally: `<!-- markdownlint-disable -->`
|
* Disable all rules: `<!-- markdownlint-disable -->`
|
||||||
* Enable all rules unconditionally: `<!-- markdownlint-enable -->`
|
* Enable all rules: `<!-- markdownlint-enable -->`
|
||||||
* Disable one or more rules by name: `<!-- markdownlint-disable MD001 MD005 -->`
|
* Disable one or more rules by name: `<!-- markdownlint-disable MD001 MD005 -->`
|
||||||
* Enable one or more rules by name: `<!-- markdownlint-enable MD001 MD005 -->`
|
* Enable one or more rules by name: `<!-- markdownlint-enable MD001 MD005 -->`
|
||||||
* Capture the current rule configuration: `<!-- markdownlint-capture -->`
|
* Capture the current rule configuration: `<!-- markdownlint-capture -->`
|
||||||
|
@ -185,6 +185,16 @@ has no effect:
|
||||||
space * in * emphasis <!-- markdownlint-disable --> <!-- markdownlint-enable -->
|
space * in * emphasis <!-- markdownlint-disable --> <!-- markdownlint-enable -->
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To apply changes to an entire file regardless of where the comment is located,
|
||||||
|
the following syntax is supported:
|
||||||
|
|
||||||
|
* Disable all rules: `<!-- markdownlint-disable-file -->`
|
||||||
|
* Enable all rules: `<!-- markdownlint-enable-file -->`
|
||||||
|
* Disable one or more rules by name: `<!-- markdownlint-disable-file MD001 -->`
|
||||||
|
* Enable one or more rules by name: `<!-- markdownlint-enable-file MD001 -->`
|
||||||
|
|
||||||
|
This can be used to "hide" `markdownlint` comments at the bottom of a file.
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
### Linting
|
### Linting
|
||||||
|
|
|
@ -17,7 +17,7 @@ module.exports.frontMatterRe =
|
||||||
// Regular expression for matching inline disable/enable comments
|
// Regular expression for matching inline disable/enable comments
|
||||||
const inlineCommentRe =
|
const inlineCommentRe =
|
||||||
// eslint-disable-next-line max-len
|
// eslint-disable-next-line max-len
|
||||||
/<!--\s*markdownlint-(disable|enable|capture|restore)((?:\s+[a-z0-9_-]+)*)\s*-->/ig;
|
/<!--\s*markdownlint-(disable|enable|capture|restore|disable-file|enable-file)((?:\s+[a-z0-9_-]+)*)\s*-->/ig;
|
||||||
module.exports.inlineCommentRe = inlineCommentRe;
|
module.exports.inlineCommentRe = inlineCommentRe;
|
||||||
|
|
||||||
// Regular expressions for range matching
|
// Regular expressions for range matching
|
||||||
|
|
|
@ -259,43 +259,55 @@ function getEnabledRulesPerLineNumber(
|
||||||
effectiveConfig, aliasToRuleNames) {
|
effectiveConfig, aliasToRuleNames) {
|
||||||
let enabledRules = {};
|
let enabledRules = {};
|
||||||
const allRuleNames = [];
|
const allRuleNames = [];
|
||||||
ruleList.forEach(function forRule(rule) {
|
ruleList.forEach((rule) => {
|
||||||
const ruleName = rule.names[0].toUpperCase();
|
const ruleName = rule.names[0].toUpperCase();
|
||||||
allRuleNames.push(ruleName);
|
allRuleNames.push(ruleName);
|
||||||
enabledRules[ruleName] = !!effectiveConfig[ruleName];
|
enabledRules[ruleName] = !!effectiveConfig[ruleName];
|
||||||
});
|
});
|
||||||
let capturedRules = enabledRules;
|
let capturedRules = enabledRules;
|
||||||
function forMatch(match) {
|
function forMatch(match, byLine) {
|
||||||
const action = match[1].toUpperCase();
|
const action = match[1].toUpperCase();
|
||||||
if (action === "CAPTURE") {
|
if (action === "CAPTURE") {
|
||||||
|
if (byLine) {
|
||||||
capturedRules = { ...enabledRules };
|
capturedRules = { ...enabledRules };
|
||||||
|
}
|
||||||
} else if (action === "RESTORE") {
|
} else if (action === "RESTORE") {
|
||||||
|
if (byLine) {
|
||||||
enabledRules = { ...capturedRules };
|
enabledRules = { ...capturedRules };
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const enabled = (action === "ENABLE");
|
// action in [ENABLE, DISABLE, ENABLE-FILE, DISABLE-FILE]
|
||||||
|
const isfile = action.endsWith("-FILE");
|
||||||
|
if ((byLine && !isfile) || (!byLine && isfile)) {
|
||||||
|
const enabled = (action.startsWith("ENABLE"));
|
||||||
const items = match[2] ?
|
const items = match[2] ?
|
||||||
match[2].trim().toUpperCase().split(/\s+/) :
|
match[2].trim().toUpperCase().split(/\s+/) :
|
||||||
allRuleNames;
|
allRuleNames;
|
||||||
items.forEach(function forItem(nameUpper) {
|
items.forEach((nameUpper) => {
|
||||||
(aliasToRuleNames[nameUpper] || []).forEach(function forRule(ruleName) {
|
(aliasToRuleNames[nameUpper] || []).forEach((ruleName) => {
|
||||||
enabledRules[ruleName] = enabled;
|
enabledRules[ruleName] = enabled;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length);
|
const enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length);
|
||||||
lines.forEach(function forLine(line) {
|
[ false, true ].forEach((byLine) => {
|
||||||
|
lines.forEach((line) => {
|
||||||
if (!noInlineConfig) {
|
if (!noInlineConfig) {
|
||||||
let match = helpers.inlineCommentRe.exec(line);
|
let match = helpers.inlineCommentRe.exec(line);
|
||||||
if (match) {
|
if (match) {
|
||||||
enabledRules = { ...enabledRules };
|
enabledRules = { ...enabledRules };
|
||||||
while (match) {
|
while (match) {
|
||||||
forMatch(match);
|
forMatch(match, byLine);
|
||||||
match = helpers.inlineCommentRe.exec(line);
|
match = helpers.inlineCommentRe.exec(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (byLine) {
|
||||||
enabledRulesPerLineNumber.push(enabledRules);
|
enabledRulesPerLineNumber.push(enabledRules);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
return enabledRulesPerLineNumber;
|
return enabledRulesPerLineNumber;
|
||||||
}
|
}
|
||||||
|
|
4
test/inline-disable-enable-file.json
Normal file
4
test/inline-disable-enable-file.json
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"no-hard-tabs": false
|
||||||
|
}
|
32
test/inline-disable-enable-file.md
Normal file
32
test/inline-disable-enable-file.md
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
# Heading
|
||||||
|
|
||||||
|
hard tab {MD010}
|
||||||
|
|
||||||
|
space * in * emphasis {MD037}
|
||||||
|
|
||||||
|
space ` in ` code
|
||||||
|
|
||||||
|
<!-- markdownlint-disable no-hard-tabs -->
|
||||||
|
|
||||||
|
hard tab
|
||||||
|
|
||||||
|
space * in * emphasis {MD037}
|
||||||
|
|
||||||
|
space ` in ` code
|
||||||
|
|
||||||
|
<!-- markdownlint-enable no-space-in-code -->
|
||||||
|
|
||||||
|
hard tab
|
||||||
|
|
||||||
|
space * in * emphasis {MD037}
|
||||||
|
|
||||||
|
space ` in ` code {MD038}
|
||||||
|
|
||||||
|
<!-- markdownlint-enable-file no-hard-tabs -->
|
||||||
|
<!-- markdownlint-disable-file no-space-in-code -->
|
||||||
|
|
||||||
|
hard tab
|
||||||
|
|
||||||
|
space * in * emphasis {MD037}
|
||||||
|
|
||||||
|
space ` in ` code {MD038}
|
Loading…
Add table
Add a link
Reference in a new issue