mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-21 21:30:47 +02:00
Implement markdownlint-disable-next-line inline comment (fixes #295).
This commit is contained in:
parent
bd63c57fde
commit
a6d30cb724
4 changed files with 51 additions and 8 deletions
|
@ -155,13 +155,22 @@ appropriate place (HTML comments don't appear in the final markup):
|
|||
|
||||
* Disable all rules: `<!-- markdownlint-disable -->`
|
||||
* Enable all rules: `<!-- markdownlint-enable -->`
|
||||
* Disable all rules for the next line only: `<!-- markdownlint-disable-next-line -->`
|
||||
* Disable one or more rules by name: `<!-- markdownlint-disable MD001 MD005 -->`
|
||||
* Enable one or more rules by name: `<!-- markdownlint-enable MD001 MD005 -->`
|
||||
* Disable one or more rules by name for the next line only: `<!-- markdownlint-disable-next-line MD001 MD005 -->`
|
||||
* Capture the current rule configuration: `<!-- markdownlint-capture -->`
|
||||
* Restore the captured rule configuration: `<!-- markdownlint-restore -->`
|
||||
|
||||
For example:
|
||||
|
||||
```markdown
|
||||
<!-- markdownlint-disable-next-line no-space-in-emphasis -->
|
||||
deliberate space * in * emphasis
|
||||
```
|
||||
|
||||
Or:
|
||||
|
||||
```markdown
|
||||
<!-- markdownlint-disable no-space-in-emphasis -->
|
||||
deliberate space * in * emphasis
|
||||
|
|
|
@ -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|disable-file|enable-file)((?:\s+[a-z0-9_-]+)*))|(?:(configure-file)\s+([\s\S]*?)))\s*-->/ig;
|
||||
/<!--\s*markdownlint-(?:(?:(disable|enable|capture|restore|disable-file|enable-file|disable-next-line)((?:\s+[a-z0-9_-]+)*))|(?:(configure-file)\s+([\s\S]*?)))\s*-->/ig;
|
||||
module.exports.inlineCommentRe = inlineCommentRe;
|
||||
|
||||
// Regular expressions for range matching
|
||||
|
|
|
@ -322,13 +322,13 @@ function getEnabledRulesPerLineNumber(
|
|||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function handleInlineConfig(perLine, forEachMatch, forEachLine) {
|
||||
const input = perLine ? lines : [ lines.join("\n") ];
|
||||
input.forEach((line) => {
|
||||
input.forEach((line, lineIndex) => {
|
||||
if (!noInlineConfig) {
|
||||
let match = null;
|
||||
while ((match = helpers.inlineCommentRe.exec(line))) {
|
||||
const action = (match[1] || match[3]).toUpperCase();
|
||||
const parameter = match[2] || match[4];
|
||||
forEachMatch(action, parameter);
|
||||
forEachMatch(action, parameter, lineIndex + 1);
|
||||
}
|
||||
}
|
||||
if (forEachLine) {
|
||||
|
@ -351,21 +351,21 @@ function getEnabledRulesPerLineNumber(
|
|||
}
|
||||
}
|
||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function applyEnableDisable(action, parameter) {
|
||||
function applyEnableDisable(action, parameter, state) {
|
||||
const enabled = (action.startsWith("ENABLE"));
|
||||
const items = parameter ?
|
||||
parameter.trim().toUpperCase().split(/\s+/) :
|
||||
allRuleNames;
|
||||
items.forEach((nameUpper) => {
|
||||
(aliasToRuleNames[nameUpper] || []).forEach((ruleName) => {
|
||||
enabledRules[ruleName] = enabled;
|
||||
state[ruleName] = enabled;
|
||||
});
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function enableDisableFile(action, parameter) {
|
||||
if ((action === "ENABLE-FILE") || (action === "DISABLE-FILE")) {
|
||||
applyEnableDisable(action, parameter);
|
||||
applyEnableDisable(action, parameter, enabledRules);
|
||||
}
|
||||
}
|
||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
|
@ -376,12 +376,22 @@ function getEnabledRulesPerLineNumber(
|
|||
enabledRules = { ...capturedRules };
|
||||
} else if ((action === "ENABLE") || (action === "DISABLE")) {
|
||||
enabledRules = { ...enabledRules };
|
||||
applyEnableDisable(action, parameter);
|
||||
applyEnableDisable(action, parameter, enabledRules);
|
||||
}
|
||||
}
|
||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function updateLineState() {
|
||||
enabledRulesPerLineNumber.push(enabledRules);
|
||||
enabledRulesPerLineNumber.push({ ...enabledRules });
|
||||
}
|
||||
// eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function disableNextLine(action, parameter, lineNumber) {
|
||||
if (action === "DISABLE-NEXT-LINE") {
|
||||
applyEnableDisable(
|
||||
action,
|
||||
parameter,
|
||||
enabledRulesPerLineNumber[lineNumber + 1] || {}
|
||||
);
|
||||
}
|
||||
}
|
||||
// Handle inline comments
|
||||
handleInlineConfig(false, configureFile);
|
||||
|
@ -395,6 +405,7 @@ function getEnabledRulesPerLineNumber(
|
|||
capturedRules = enabledRules;
|
||||
handleInlineConfig(true, enableDisableFile);
|
||||
handleInlineConfig(true, captureRestoreEnableDisable, updateLineState);
|
||||
handleInlineConfig(true, disableNextLine);
|
||||
// Return results
|
||||
return {
|
||||
effectiveConfig,
|
||||
|
|
|
@ -67,3 +67,26 @@ hard tab {MD010} / space *in * emphasis {MD037} / space `in ` code
|
|||
hard tab {MD010} / space *in * emphasis {MD037} / space `in ` code {MD038}
|
||||
|
||||
embedded <b>{MD033}</b> HTML
|
||||
|
||||
<!-- markdownlint-disable line-length -->
|
||||
|
||||
hard tab {MD010} / space *in * emphasis {MD037} / space `in ` code {MD038}
|
||||
<!-- markdownlint-disable-next-line -->
|
||||
hard tab / space *in * emphasis / space `in ` code
|
||||
hard tab {MD010} / space *in * emphasis {MD037} / space `in ` code {MD038}
|
||||
hard tab {MD010} / space *in * emphasis {MD037} / space `in ` code {MD038}<!-- markdownlint-disable-next-line -->
|
||||
hard tab / space *in * emphasis / space `in ` code
|
||||
hard tab {MD010} / space *in * emphasis {MD037} / space `in ` code {MD038}
|
||||
<!-- markdownlint-disable-next-line MD010 MD038 -->
|
||||
hard tab / space *in * emphasis {MD037} / space `in ` code
|
||||
hard tab {MD010} / space *in * emphasis {MD037} / space `in ` code {MD038}
|
||||
<!-- markdownlint-disable-next-line MD010 --><!-- markdownlint-disable-next-line MD038 -->
|
||||
hard tab / space *in * emphasis {MD037} / space `in ` code
|
||||
hard tab {MD010} / space *in * emphasis {MD037} / space `in ` code {MD038}
|
||||
<!-- markdownlint-disable MD010-->
|
||||
hard tab / space *in * emphasis {MD037} / space `in ` code {MD038}
|
||||
<!-- markdownlint-disable-next-line MD038 -->
|
||||
hard tab / space *in * emphasis {MD037} / space `in ` code
|
||||
hard tab / space *in * emphasis {MD037} / space `in ` code {MD038}
|
||||
|
||||
<!-- markdownlint-disable-next-line -->
|
Loading…
Add table
Add a link
Reference in a new issue