mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add support for inline comments markdownlint-capture/markdownlint-restore (fixes #194).
This commit is contained in:
parent
64351f73be
commit
65052f6200
5 changed files with 167 additions and 15 deletions
33
README.md
33
README.md
|
@ -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
|
||||
comments don't appear in the final markup):
|
||||
|
||||
* Disable all rules: `<!-- markdownlint-disable -->`
|
||||
* Enable all rules: `<!-- markdownlint-enable -->`
|
||||
* Disable one or more rules: `<!-- markdownlint-disable MD001 MD002 -->`
|
||||
* Enable one or more rules: `<!-- markdownlint-enable MD001 MD002 -->`
|
||||
* Disable all rules unconditionally: `<!-- markdownlint-disable -->`
|
||||
* Enable all rules unconditionally: `<!-- 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 -->`
|
||||
* Restore the captured rule configuration: `<!-- markdownlint-restore -->`
|
||||
|
||||
For example:
|
||||
|
||||
```markdown
|
||||
<!-- markdownlint-disable MD037 -->
|
||||
<!-- markdownlint-disable no-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
|
||||
|
|
|
@ -12,7 +12,8 @@ module.exports.frontMatterRe =
|
|||
|
||||
// Regular expression for matching inline disable/enable comments
|
||||
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;
|
||||
|
||||
// Regular expressions for range matching
|
||||
|
|
|
@ -255,16 +255,24 @@ function getEnabledRulesPerLineNumber(
|
|||
allRuleNames.push(ruleName);
|
||||
enabledRules[ruleName] = !!effectiveConfig[ruleName];
|
||||
});
|
||||
let capturedRules = enabledRules;
|
||||
function forMatch(match) {
|
||||
const enabled = match[1].toUpperCase() === "EN";
|
||||
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 action = match[1].toUpperCase();
|
||||
if (action === "CAPTURE") {
|
||||
capturedRules = { ...enabledRules };
|
||||
} else if (action === "RESTORE") {
|
||||
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;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
const enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length);
|
||||
lines.forEach(function forLine(line) {
|
||||
|
|
4
test/inline-capture-restore.json
Normal file
4
test/inline-capture-restore.json
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"default": true,
|
||||
"no-hard-tabs": false
|
||||
}
|
118
test/inline-capture-restore.md
Normal file
118
test/inline-capture-restore.md
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue