Skip to content

Commit a55d549

Browse files
committed
Update MD009/no-trailing-spaces with code_blocks parameter to allow reporting/fixing trailing spaces in indented/fenced code blocks (fixes #1181).
1 parent 7beb9fc commit a55d549

16 files changed

+590
-34
lines changed

doc-build/md009.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
This rule is triggered on any lines that end with unexpected whitespace. To fix
22
this, remove the trailing space from the end of the line.
33

4-
Note: Trailing space is allowed in indented and fenced code blocks because some
5-
languages require it.
6-
74
The `br_spaces` parameter allows an exception to this rule for a specific number
85
of trailing spaces, typically used to insert an explicit line break. The default
9-
value allows 2 spaces to indicate a hard break (\<br> element).
6+
value allows 2 spaces to indicate a hard break (\<br> element). (You must set
7+
`br_spaces` to a value >= 2 for this parameter to take effect. Setting
8+
`br_spaces` to 1 behaves the same as 0, disallowing any trailing spaces.)
109

11-
Note: You must set `br_spaces` to a value >= 2 for this parameter to take
12-
effect. Setting `br_spaces` to 1 behaves the same as 0, disallowing any trailing
13-
spaces.
10+
By default, trailing space is allowed in indented and fenced code blocks because
11+
some programming languages require that. To report such instances, set the
12+
`code_blocks` parameter to `true`.
1413

1514
By default, this rule will not trigger when the allowed number of spaces is
1615
used, even when it doesn't create a hard break (for example, at the end of a
17-
paragraph). To report such instances as well, set the `strict` parameter to
18-
`true`.
16+
paragraph). To report such instances, set the `strict` parameter to `true`.
1917

2018
```markdown
2119
Text text text

doc/Rules.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ Aliases: `no-trailing-spaces`
295295
Parameters:
296296

297297
- `br_spaces`: Spaces for line break (`integer`, default `2`)
298+
- `code_blocks`: Include code blocks (`boolean`, default `false`)
298299
- `list_item_empty_lines`: Allow spaces for empty lines in list items
299300
(`boolean`, default `false`)
300301
- `strict`: Include unnecessary breaks (`boolean`, default `false`)
@@ -304,21 +305,19 @@ Fixable: Some violations can be fixed by tooling
304305
This rule is triggered on any lines that end with unexpected whitespace. To fix
305306
this, remove the trailing space from the end of the line.
306307

307-
Note: Trailing space is allowed in indented and fenced code blocks because some
308-
languages require it.
309-
310308
The `br_spaces` parameter allows an exception to this rule for a specific number
311309
of trailing spaces, typically used to insert an explicit line break. The default
312-
value allows 2 spaces to indicate a hard break (\<br> element).
310+
value allows 2 spaces to indicate a hard break (\<br> element). (You must set
311+
`br_spaces` to a value >= 2 for this parameter to take effect. Setting
312+
`br_spaces` to 1 behaves the same as 0, disallowing any trailing spaces.)
313313

314-
Note: You must set `br_spaces` to a value >= 2 for this parameter to take
315-
effect. Setting `br_spaces` to 1 behaves the same as 0, disallowing any trailing
316-
spaces.
314+
By default, trailing space is allowed in indented and fenced code blocks because
315+
some programming languages require that. To report such instances, set the
316+
`code_blocks` parameter to `true`.
317317

318318
By default, this rule will not trigger when the allowed number of spaces is
319319
used, even when it doesn't create a hard break (for example, at the end of a
320-
paragraph). To report such instances as well, set the `strict` parameter to
321-
`true`.
320+
paragraph). To report such instances, set the `strict` parameter to `true`.
322321

323322
```markdown
324323
Text text text

doc/md009.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Aliases: `no-trailing-spaces`
77
Parameters:
88

99
- `br_spaces`: Spaces for line break (`integer`, default `2`)
10+
- `code_blocks`: Include code blocks (`boolean`, default `false`)
1011
- `list_item_empty_lines`: Allow spaces for empty lines in list items
1112
(`boolean`, default `false`)
1213
- `strict`: Include unnecessary breaks (`boolean`, default `false`)
@@ -16,21 +17,19 @@ Fixable: Some violations can be fixed by tooling
1617
This rule is triggered on any lines that end with unexpected whitespace. To fix
1718
this, remove the trailing space from the end of the line.
1819

19-
Note: Trailing space is allowed in indented and fenced code blocks because some
20-
languages require it.
21-
2220
The `br_spaces` parameter allows an exception to this rule for a specific number
2321
of trailing spaces, typically used to insert an explicit line break. The default
24-
value allows 2 spaces to indicate a hard break (\<br> element).
22+
value allows 2 spaces to indicate a hard break (\<br> element). (You must set
23+
`br_spaces` to a value >= 2 for this parameter to take effect. Setting
24+
`br_spaces` to 1 behaves the same as 0, disallowing any trailing spaces.)
2525

26-
Note: You must set `br_spaces` to a value >= 2 for this parameter to take
27-
effect. Setting `br_spaces` to 1 behaves the same as 0, disallowing any trailing
28-
spaces.
26+
By default, trailing space is allowed in indented and fenced code blocks because
27+
some programming languages require that. To report such instances, set the
28+
`code_blocks` parameter to `true`.
2929

3030
By default, this rule will not trigger when the allowed number of spaces is
3131
used, even when it doesn't create a hard break (for example, at the end of a
32-
paragraph). To report such instances as well, set the `strict` parameter to
33-
`true`.
32+
paragraph). To report such instances, set the `strict` parameter to `true`.
3433

3534
```markdown
3635
Text text text

lib/configuration-strict.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ export interface ConfigurationStrict {
245245
* Spaces for line break
246246
*/
247247
br_spaces?: number;
248+
/**
249+
* Include code blocks
250+
*/
251+
code_blocks?: boolean;
248252
/**
249253
* Allow spaces for empty lines in list items
250254
*/
@@ -273,6 +277,10 @@ export interface ConfigurationStrict {
273277
* Spaces for line break
274278
*/
275279
br_spaces?: number;
280+
/**
281+
* Include code blocks
282+
*/
283+
code_blocks?: boolean;
276284
/**
277285
* Allow spaces for empty lines in list items
278286
*/

lib/md009.mjs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ export default {
1313
"function": function MD009(params, onError) {
1414
let brSpaces = params.config.br_spaces;
1515
brSpaces = Number((brSpaces === undefined) ? 2 : brSpaces);
16+
const codeBlocks = params.config.code_blocks;
17+
const includeCode = (codeBlocks === undefined) ? false : !!codeBlocks;
1618
const listItemEmptyLines = !!params.config.list_item_empty_lines;
1719
const strict = !!params.config.strict;
1820
const codeBlockLineNumbers = new Set();
19-
for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
20-
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine + 1, codeBlock.endLine - 1);
21-
}
22-
for (const codeBlock of filterByTypesCached([ "codeIndented" ])) {
23-
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
21+
if (!includeCode) {
22+
for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
23+
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine + 1, codeBlock.endLine - 1);
24+
}
25+
for (const codeBlock of filterByTypesCached([ "codeIndented" ])) {
26+
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
27+
}
2428
}
2529
const listItemLineNumbers = new Set();
2630
if (listItemEmptyLines) {

schema/.markdownlint.jsonc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
"MD009": {
4343
// Spaces for line break
4444
"br_spaces": 2,
45+
// Include code blocks
46+
"code_blocks": false,
4547
// Allow spaces for empty lines in list items
4648
"list_item_empty_lines": false,
4749
// Include unnecessary breaks

schema/.markdownlint.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ MD007:
3737
MD009:
3838
# Spaces for line break
3939
br_spaces: 2
40+
# Include code blocks
41+
code_blocks: false
4042
# Allow spaces for empty lines in list items
4143
list_item_empty_lines: false
4244
# Include unnecessary breaks

schema/build-config-schema.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ for (const rule of rules) {
161161
"default": 2
162162
};
163163
// @ts-ignore
164+
subscheme.properties.code_blocks = {
165+
"description": "Include code blocks",
166+
"type": "boolean",
167+
"default": false
168+
};
169+
// @ts-ignore
164170
subscheme.properties.list_item_empty_lines = {
165171
"description": "Allow spaces for empty lines in list items",
166172
"type": "boolean",

schema/markdownlint-config-schema-strict.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,11 @@
512512
"minimum": 0,
513513
"default": 2
514514
},
515+
"code_blocks": {
516+
"description": "Include code blocks",
517+
"type": "boolean",
518+
"default": false
519+
},
515520
"list_item_empty_lines": {
516521
"description": "Allow spaces for empty lines in list items",
517522
"type": "boolean",
@@ -563,6 +568,11 @@
563568
"minimum": 0,
564569
"default": 2
565570
},
571+
"code_blocks": {
572+
"description": "Include code blocks",
573+
"type": "boolean",
574+
"default": false
575+
},
566576
"list_item_empty_lines": {
567577
"description": "Allow spaces for empty lines in list items",
568578
"type": "boolean",

schema/markdownlint-config-schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,11 @@
512512
"minimum": 0,
513513
"default": 2
514514
},
515+
"code_blocks": {
516+
"description": "Include code blocks",
517+
"type": "boolean",
518+
"default": false
519+
},
515520
"list_item_empty_lines": {
516521
"description": "Allow spaces for empty lines in list items",
517522
"type": "boolean",
@@ -563,6 +568,11 @@
563568
"minimum": 0,
564569
"default": 2
565570
},
571+
"code_blocks": {
572+
"description": "Include code blocks",
573+
"type": "boolean",
574+
"default": false
575+
},
566576
"list_item_empty_lines": {
567577
"description": "Allow spaces for empty lines in list items",
568578
"type": "boolean",

0 commit comments

Comments
 (0)