Add support for inline comments markdownlint-capture/markdownlint-restore (fixes #194).

This commit is contained in:
David Anson 2019-06-08 19:26:11 -07:00
parent 64351f73be
commit 65052f6200
5 changed files with 167 additions and 15 deletions

View file

@ -142,17 +142,38 @@ 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 within a file, add one of these markers to the appropriate place (HTML
comments don't appear in the final markup): comments don't appear in the final markup):
* Disable all rules: `<!-- markdownlint-disable -->` * Disable all rules unconditionally: `<!-- markdownlint-disable -->`
* Enable all rules: `<!-- markdownlint-enable -->` * Enable all rules unconditionally: `<!-- markdownlint-enable -->`
* Disable one or more rules: `<!-- markdownlint-disable MD001 MD002 -->` * Disable one or more rules by name: `<!-- markdownlint-disable MD001 MD005 -->`
* Enable one or more rules: `<!-- markdownlint-enable MD001 MD002 -->` * Enable one or more rules by name: `<!-- markdownlint-enable MD001 MD005 -->`
* Capture the current rule configuration: `<!-- markdownlint-capture -->`
* Restore the captured rule configuration: `<!-- markdownlint-restore -->`
For example: For example:
```markdown ```markdown
<!-- markdownlint-disable MD037 --> <!-- markdownlint-disable no-space-in-emphasis -->
deliberate space * in * emphasis deliberate space * in * emphasis
<!-- markdownlint-enable MD037 --> <!-- markdownlint-enable no-space-in-emphasis -->
```
To temporarily disable rule(s), then restore the former configuration:
```markdown
<!-- markdownlint-capture -->
<!-- markdownlint-disable -->
any violations you want
<!-- markdownlint-restore -->
```
The initial configuration is captured by default (as if every document began
with `<!-- markdownlint-capture -->`), so the pattern above can be expressed
more simply:
```markdown
<!-- markdownlint-disable -->
any violations you want
<!-- markdownlint-restore -->
``` ```
Changes take effect starting with the line a comment is on, so the following Changes take effect starting with the line a comment is on, so the following

View file

@ -12,7 +12,8 @@ module.exports.frontMatterRe =
// Regular expression for matching inline disable/enable comments // Regular expression for matching inline disable/enable comments
const inlineCommentRe = const inlineCommentRe =
/<!--\s*markdownlint-(dis|en)able((?:\s+[a-z0-9_-]+)*)\s*-->/ig; // eslint-disable-next-line max-len
/<!--\s*markdownlint-(disable|enable|capture|restore)((?:\s+[a-z0-9_-]+)*)\s*-->/ig;
module.exports.inlineCommentRe = inlineCommentRe; module.exports.inlineCommentRe = inlineCommentRe;
// Regular expressions for range matching // Regular expressions for range matching

View file

@ -255,16 +255,24 @@ function getEnabledRulesPerLineNumber(
allRuleNames.push(ruleName); allRuleNames.push(ruleName);
enabledRules[ruleName] = !!effectiveConfig[ruleName]; enabledRules[ruleName] = !!effectiveConfig[ruleName];
}); });
let capturedRules = enabledRules;
function forMatch(match) { function forMatch(match) {
const enabled = match[1].toUpperCase() === "EN"; const action = match[1].toUpperCase();
const items = match[2] ? if (action === "CAPTURE") {
match[2].trim().toUpperCase().split(/\s+/) : capturedRules = { ...enabledRules };
allRuleNames; } else if (action === "RESTORE") {
items.forEach(function forItem(nameUpper) { enabledRules = { ...capturedRules };
(aliasToRuleNames[nameUpper] || []).forEach(function forRule(ruleName) { } else {
enabledRules[ruleName] = enabled; 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;
});
}); });
}); }
} }
const enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length); const enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length);
lines.forEach(function forLine(line) { lines.forEach(function forLine(line) {

View file

@ -0,0 +1,4 @@
{
"default": true,
"no-hard-tabs": false
}

View file

@ -0,0 +1,118 @@
# Inline Capture/Restore
hard tab
space * in * emphasis {MD037}
space ` in ` code {MD038}
<!-- markdownlint-disable -->
hard tab
space * in * emphasis
space ` in ` code
<!-- markdownlint-restore -->
hard tab
space * in * emphasis {MD037}
space ` in ` code {MD038}
<!-- markdownlint-disable no-space-in-emphasis -->
hard tab
space * in * emphasis
space ` in ` code {MD038}
<!-- markdownlint-disable -->
hard tab
space * in * emphasis
space ` in ` code
<!-- markdownlint-restore -->
hard tab
space * in * emphasis {MD037}
space ` in ` code {MD038}
<!-- markdownlint-disable no-space-in-emphasis -->
hard tab
space * in * emphasis
space ` in ` code {MD038}
<!-- markdownlint-capture -->
<!-- markdownlint-disable -->
hard tab
space * in * emphasis
space ` in ` code
<!-- markdownlint-restore -->
hard tab
space * in * emphasis
space ` in ` code {MD038}
<!-- markdownlint-enable no-hard-tabs -->
hard tab {MD010}
space * in * emphasis
space ` in ` code {MD038}
<!-- markdownlint-restore -->
hard tab
space * in * emphasis
space ` in ` code {MD038}
<!-- markdownlint-enable no-hard-tabs -->
hard tab {MD010}
space * in * emphasis
space ` in ` code {MD038}
<!-- markdownlint-capture -->
hard tab {MD010}
space * in * emphasis
space ` in ` code {MD038}
<!-- markdownlint-restore -->
hard tab {MD010}
space * in * emphasis
space ` in ` code {MD038}
<!-- markdownlint-enable -->
hard tab {MD010}
space * in * emphasis {MD037}
space ` in ` code {MD038}
<!-- markdownlint-restore -->
hard tab {MD010}
space * in * emphasis
space ` in ` code {MD038}
<!-- markdownlint-disable no-space-in-code -->
hard tab {MD010}
space * in * emphasis
space ` in ` code
<!-- markdownlint-capture --><!-- markdownlint-disable -->
hard tab
space * in * emphasis
space ` in ` code
<!-- markdownlint-restore -->
hard tab {MD010}
space * in * emphasis
space ` in ` code
<!-- markdownlint-disable no-hard-tabs -->
hard tab
space * in * emphasis
space ` in ` code
<!-- markdownlint-restore --> <!-- markdownlint-enable no-space-in-emphasis -->
hard tab {MD010}
space * in * emphasis {MD037}
space ` in ` code