diff --git a/README.md b/README.md index 4b983ac0..4ea88989 100644 --- a/README.md +++ b/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: `` -* Enable all rules: `` -* Disable one or more rules: `` -* Enable one or more rules: `` +* Disable all rules unconditionally: `` +* Enable all rules unconditionally: `` +* Disable one or more rules by name: `` +* Enable one or more rules by name: `` +* Capture the current rule configuration: `` +* Restore the captured rule configuration: `` For example: ```markdown - + deliberate space * in * emphasis - + +``` + +To temporarily disable rule(s), then restore the former configuration: + +```markdown + + +any violations you want + +``` + +The initial configuration is captured by default (as if every document began +with ``), so the pattern above can be expressed +more simply: + +```markdown + +any violations you want + ``` Changes take effect starting with the line a comment is on, so the following diff --git a/helpers/helpers.js b/helpers/helpers.js index 8f21e800..0bd8d8e2 100644 --- a/helpers/helpers.js +++ b/helpers/helpers.js @@ -12,7 +12,8 @@ module.exports.frontMatterRe = // Regular expression for matching inline disable/enable comments const inlineCommentRe = - //ig; + // eslint-disable-next-line max-len + //ig; module.exports.inlineCommentRe = inlineCommentRe; // Regular expressions for range matching diff --git a/lib/markdownlint.js b/lib/markdownlint.js index 68c75ddf..2b80a19a 100644 --- a/lib/markdownlint.js +++ b/lib/markdownlint.js @@ -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) { diff --git a/test/inline-capture-restore.json b/test/inline-capture-restore.json new file mode 100644 index 00000000..3baf5c1e --- /dev/null +++ b/test/inline-capture-restore.json @@ -0,0 +1,4 @@ +{ + "default": true, + "no-hard-tabs": false +} diff --git a/test/inline-capture-restore.md b/test/inline-capture-restore.md new file mode 100644 index 00000000..56355eba --- /dev/null +++ b/test/inline-capture-restore.md @@ -0,0 +1,118 @@ +# Inline Capture/Restore + +hard tab +space * in * emphasis {MD037} +space ` in ` code {MD038} + + +hard tab +space * in * emphasis +space ` in ` code + + +hard tab +space * in * emphasis {MD037} +space ` in ` code {MD038} + + + +hard tab +space * in * emphasis +space ` in ` code {MD038} + + +hard tab +space * in * emphasis +space ` in ` code + + +hard tab +space * in * emphasis {MD037} +space ` in ` code {MD038} + + + +hard tab +space * in * emphasis +space ` in ` code {MD038} + + + +hard tab +space * in * emphasis +space ` in ` code + + +hard tab +space * in * emphasis +space ` in ` code {MD038} + + + +hard tab {MD010} +space * in * emphasis +space ` in ` code {MD038} + + + +hard tab +space * in * emphasis +space ` in ` code {MD038} + + + +hard tab {MD010} +space * in * emphasis +space ` in ` code {MD038} + + + +hard tab {MD010} +space * in * emphasis +space ` in ` code {MD038} + + + +hard tab {MD010} +space * in * emphasis +space ` in ` code {MD038} + + + +hard tab {MD010} +space * in * emphasis {MD037} +space ` in ` code {MD038} + + + +hard tab {MD010} +space * in * emphasis +space ` in ` code {MD038} + + + +hard tab {MD010} +space * in * emphasis +space ` in ` code + + +hard tab +space * in * emphasis +space ` in ` code + + +hard tab {MD010} +space * in * emphasis +space ` in ` code + + + +hard tab +space * in * emphasis +space ` in ` code + + + +hard tab {MD010} +space * in * emphasis {MD037} +space ` in ` code