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
|
||||
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
|
||||
comments don't appear in the final markup):
|
||||
rules at a particular location within a file, add one of these markers to the
|
||||
appropriate place (HTML comments don't appear in the final markup):
|
||||
|
||||
* Disable all rules unconditionally: `<!-- markdownlint-disable -->`
|
||||
* Enable all rules unconditionally: `<!-- markdownlint-enable -->`
|
||||
* Disable all rules: `<!-- markdownlint-disable -->`
|
||||
* Enable all rules: `<!-- markdownlint-enable -->`
|
||||
* Disable one or more rules by name: `<!-- markdownlint-disable MD001 MD005 -->`
|
||||
* Enable one or more rules by name: `<!-- markdownlint-enable MD001 MD005 -->`
|
||||
* Capture the current rule configuration: `<!-- markdownlint-capture -->`
|
||||
|
@ -185,6 +185,16 @@ has no effect:
|
|||
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
|
||||
|
||||
### Linting
|
||||
|
|
|
@ -17,7 +17,7 @@ module.exports.frontMatterRe =
|
|||
// Regular expression for matching inline disable/enable comments
|
||||
const inlineCommentRe =
|
||||
// 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;
|
||||
|
||||
// Regular expressions for range matching
|
||||
|
|
|
@ -259,43 +259,55 @@ function getEnabledRulesPerLineNumber(
|
|||
effectiveConfig, aliasToRuleNames) {
|
||||
let enabledRules = {};
|
||||
const allRuleNames = [];
|
||||
ruleList.forEach(function forRule(rule) {
|
||||
ruleList.forEach((rule) => {
|
||||
const ruleName = rule.names[0].toUpperCase();
|
||||
allRuleNames.push(ruleName);
|
||||
enabledRules[ruleName] = !!effectiveConfig[ruleName];
|
||||
});
|
||||
let capturedRules = enabledRules;
|
||||
function forMatch(match) {
|
||||
function forMatch(match, byLine) {
|
||||
const action = match[1].toUpperCase();
|
||||
if (action === "CAPTURE") {
|
||||
capturedRules = { ...enabledRules };
|
||||
if (byLine) {
|
||||
capturedRules = { ...enabledRules };
|
||||
}
|
||||
} else if (action === "RESTORE") {
|
||||
enabledRules = { ...capturedRules };
|
||||
if (byLine) {
|
||||
enabledRules = { ...capturedRules };
|
||||
}
|
||||
} else {
|
||||
const enabled = (action === "ENABLE");
|
||||
const items = match[2] ?
|
||||
match[2].trim().toUpperCase().split(/\s+/) :
|
||||
allRuleNames;
|
||||
items.forEach(function forItem(nameUpper) {
|
||||
(aliasToRuleNames[nameUpper] || []).forEach(function forRule(ruleName) {
|
||||
enabledRules[ruleName] = enabled;
|
||||
// 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] ?
|
||||
match[2].trim().toUpperCase().split(/\s+/) :
|
||||
allRuleNames;
|
||||
items.forEach((nameUpper) => {
|
||||
(aliasToRuleNames[nameUpper] || []).forEach((ruleName) => {
|
||||
enabledRules[ruleName] = enabled;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
const enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length);
|
||||
lines.forEach(function forLine(line) {
|
||||
if (!noInlineConfig) {
|
||||
let match = helpers.inlineCommentRe.exec(line);
|
||||
if (match) {
|
||||
enabledRules = { ...enabledRules };
|
||||
while (match) {
|
||||
forMatch(match);
|
||||
match = helpers.inlineCommentRe.exec(line);
|
||||
[ false, true ].forEach((byLine) => {
|
||||
lines.forEach((line) => {
|
||||
if (!noInlineConfig) {
|
||||
let match = helpers.inlineCommentRe.exec(line);
|
||||
if (match) {
|
||||
enabledRules = { ...enabledRules };
|
||||
while (match) {
|
||||
forMatch(match, byLine);
|
||||
match = helpers.inlineCommentRe.exec(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
enabledRulesPerLineNumber.push(enabledRules);
|
||||
if (byLine) {
|
||||
enabledRulesPerLineNumber.push(enabledRules);
|
||||
}
|
||||
});
|
||||
});
|
||||
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