Skip to content

Commit a9251c5

Browse files
committed
Add markdownlint-disable-file/markdownlint-enable-file inline comments (fixes #215).
1 parent c0f040e commit a9251c5

File tree

5 files changed

+85
-27
lines changed

5 files changed

+85
-27
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ Two kinds of text are ignored:
141141

142142
Rules can be enabled, disabled, and configured via `options.config` (described
143143
below) to define the expected behavior for a set of inputs. To enable or disable
144-
rules within a file, add one of these markers to the appropriate place (HTML
145-
comments don't appear in the final markup):
144+
rules at a particular location within a file, add one of these markers to the
145+
appropriate place (HTML comments don't appear in the final markup):
146146

147-
* Disable all rules unconditionally: `<!-- markdownlint-disable -->`
148-
* Enable all rules unconditionally: `<!-- markdownlint-enable -->`
147+
* Disable all rules: `<!-- markdownlint-disable -->`
148+
* Enable all rules: `<!-- markdownlint-enable -->`
149149
* Disable one or more rules by name: `<!-- markdownlint-disable MD001 MD005 -->`
150150
* Enable one or more rules by name: `<!-- markdownlint-enable MD001 MD005 -->`
151151
* Capture the current rule configuration: `<!-- markdownlint-capture -->`
@@ -185,6 +185,16 @@ has no effect:
185185
space * in * emphasis <!-- markdownlint-disable --> <!-- markdownlint-enable -->
186186
```
187187

188+
To apply changes to an entire file regardless of where the comment is located,
189+
the following syntax is supported:
190+
191+
* Disable all rules: `<!-- markdownlint-disable-file -->`
192+
* Enable all rules: `<!-- markdownlint-enable-file -->`
193+
* Disable one or more rules by name: `<!-- markdownlint-disable-file MD001 -->`
194+
* Enable one or more rules by name: `<!-- markdownlint-enable-file MD001 -->`
195+
196+
This can be used to "hide" `markdownlint` comments at the bottom of a file.
197+
188198
## API
189199

190200
### Linting

helpers/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports.frontMatterRe =
1717
// Regular expression for matching inline disable/enable comments
1818
const inlineCommentRe =
1919
// eslint-disable-next-line max-len
20-
/<!--\s*markdownlint-(disable|enable|capture|restore)((?:\s+[a-z0-9_-]+)*)\s*-->/ig;
20+
/<!--\s*markdownlint-(disable|enable|capture|restore|disable-file|enable-file)((?:\s+[a-z0-9_-]+)*)\s*-->/ig;
2121
module.exports.inlineCommentRe = inlineCommentRe;
2222

2323
// Regular expressions for range matching

lib/markdownlint.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -259,43 +259,55 @@ function getEnabledRulesPerLineNumber(
259259
effectiveConfig, aliasToRuleNames) {
260260
let enabledRules = {};
261261
const allRuleNames = [];
262-
ruleList.forEach(function forRule(rule) {
262+
ruleList.forEach((rule) => {
263263
const ruleName = rule.names[0].toUpperCase();
264264
allRuleNames.push(ruleName);
265265
enabledRules[ruleName] = !!effectiveConfig[ruleName];
266266
});
267267
let capturedRules = enabledRules;
268-
function forMatch(match) {
268+
function forMatch(match, byLine) {
269269
const action = match[1].toUpperCase();
270270
if (action === "CAPTURE") {
271-
capturedRules = { ...enabledRules };
271+
if (byLine) {
272+
capturedRules = { ...enabledRules };
273+
}
272274
} else if (action === "RESTORE") {
273-
enabledRules = { ...capturedRules };
275+
if (byLine) {
276+
enabledRules = { ...capturedRules };
277+
}
274278
} else {
275-
const enabled = (action === "ENABLE");
276-
const items = match[2] ?
277-
match[2].trim().toUpperCase().split(/\s+/) :
278-
allRuleNames;
279-
items.forEach(function forItem(nameUpper) {
280-
(aliasToRuleNames[nameUpper] || []).forEach(function forRule(ruleName) {
281-
enabledRules[ruleName] = enabled;
279+
// action in [ENABLE, DISABLE, ENABLE-FILE, DISABLE-FILE]
280+
const isfile = action.endsWith("-FILE");
281+
if ((byLine && !isfile) || (!byLine && isfile)) {
282+
const enabled = (action.startsWith("ENABLE"));
283+
const items = match[2] ?
284+
match[2].trim().toUpperCase().split(/\s+/) :
285+
allRuleNames;
286+
items.forEach((nameUpper) => {
287+
(aliasToRuleNames[nameUpper] || []).forEach((ruleName) => {
288+
enabledRules[ruleName] = enabled;
289+
});
282290
});
283-
});
291+
}
284292
}
285293
}
286294
const enabledRulesPerLineNumber = new Array(1 + frontMatterLines.length);
287-
lines.forEach(function forLine(line) {
288-
if (!noInlineConfig) {
289-
let match = helpers.inlineCommentRe.exec(line);
290-
if (match) {
291-
enabledRules = { ...enabledRules };
292-
while (match) {
293-
forMatch(match);
294-
match = helpers.inlineCommentRe.exec(line);
295+
[ false, true ].forEach((byLine) => {
296+
lines.forEach((line) => {
297+
if (!noInlineConfig) {
298+
let match = helpers.inlineCommentRe.exec(line);
299+
if (match) {
300+
enabledRules = { ...enabledRules };
301+
while (match) {
302+
forMatch(match, byLine);
303+
match = helpers.inlineCommentRe.exec(line);
304+
}
295305
}
296306
}
297-
}
298-
enabledRulesPerLineNumber.push(enabledRules);
307+
if (byLine) {
308+
enabledRulesPerLineNumber.push(enabledRules);
309+
}
310+
});
299311
});
300312
return enabledRulesPerLineNumber;
301313
}

test/inline-disable-enable-file.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"default": true,
3+
"no-hard-tabs": false
4+
}

test/inline-disable-enable-file.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Heading
2+
3+
hard tab {MD010}
4+
5+
space * in * emphasis {MD037}
6+
7+
space ` in ` code
8+
9+
<!-- markdownlint-disable no-hard-tabs -->
10+
11+
hard tab
12+
13+
space * in * emphasis {MD037}
14+
15+
space ` in ` code
16+
17+
<!-- markdownlint-enable no-space-in-code -->
18+
19+
hard tab
20+
21+
space * in * emphasis {MD037}
22+
23+
space ` in ` code {MD038}
24+
25+
<!-- markdownlint-enable-file no-hard-tabs -->
26+
<!-- markdownlint-disable-file no-space-in-code -->
27+
28+
hard tab
29+
30+
space * in * emphasis {MD037}
31+
32+
space ` in ` code {MD038}

0 commit comments

Comments
 (0)