Skip to content

Commit 90cf515

Browse files
committed
Update MD038/no-space-in-code to allow code spans with only spaces (fixes #1481).
1 parent 2d2fafc commit 90cf515

File tree

7 files changed

+56
-68
lines changed

7 files changed

+56
-68
lines changed

doc-build/md038.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
This rule is triggered for code span elements that have spaces adjacent to the
2-
backticks:
1+
This rule is triggered for code spans that have content with spaces next to the
2+
beginning or ending backticks:
33

44
```markdown
55
`some text `
66

77
` some text`
88
```
99

10-
To fix this, remove any spaces adjacent to the backticks:
10+
To fix this, remove any spaces at the beginning or ending:
1111

1212
```markdown
1313
`some text`
1414
```
1515

16+
Note: Code spans containing only spaces are allowed by the specification:
17+
18+
```markdown
19+
` ` or ` `
20+
```
21+
1622
Note: A single leading and trailing space is allowed by the specification and
17-
automatically trimmed (in order to allow for code spans that embed backticks):
23+
automatically trimmed by the parser (in order to allow for code spans that embed
24+
backticks):
1825

1926
```markdown
2027
`` `backticks` ``
@@ -28,5 +35,4 @@ span markers from an embedded backtick (though the space is not trimmed):
2835
```
2936

3037
Rationale: Violations of this rule are usually unintentional and may lead to
31-
improperly-rendered content. If spaces beside backticks are intentional, this
32-
rule can be disabled for that line or file.
38+
improperly-rendered content.

doc/Rules.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,23 +1528,30 @@ Aliases: `no-space-in-code`
15281528

15291529
Fixable: Some violations can be fixed by tooling
15301530

1531-
This rule is triggered for code span elements that have spaces adjacent to the
1532-
backticks:
1531+
This rule is triggered for code spans that have content with spaces next to the
1532+
beginning or ending backticks:
15331533

15341534
```markdown
15351535
`some text `
15361536

15371537
` some text`
15381538
```
15391539

1540-
To fix this, remove any spaces adjacent to the backticks:
1540+
To fix this, remove any spaces at the beginning or ending:
15411541

15421542
```markdown
15431543
`some text`
15441544
```
15451545

1546+
Note: Code spans containing only spaces are allowed by the specification:
1547+
1548+
```markdown
1549+
` ` or ` `
1550+
```
1551+
15461552
Note: A single leading and trailing space is allowed by the specification and
1547-
automatically trimmed (in order to allow for code spans that embed backticks):
1553+
automatically trimmed by the parser (in order to allow for code spans that embed
1554+
backticks):
15481555

15491556
```markdown
15501557
`` `backticks` ``
@@ -1558,8 +1565,7 @@ span markers from an embedded backtick (though the space is not trimmed):
15581565
```
15591566

15601567
Rationale: Violations of this rule are usually unintentional and may lead to
1561-
improperly-rendered content. If spaces beside backticks are intentional, this
1562-
rule can be disabled for that line or file.
1568+
improperly-rendered content.
15631569

15641570
<a name="md039"></a>
15651571

doc/md038.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,30 @@ Aliases: `no-space-in-code`
66

77
Fixable: Some violations can be fixed by tooling
88

9-
This rule is triggered for code span elements that have spaces adjacent to the
10-
backticks:
9+
This rule is triggered for code spans that have content with spaces next to the
10+
beginning or ending backticks:
1111

1212
```markdown
1313
`some text `
1414

1515
` some text`
1616
```
1717

18-
To fix this, remove any spaces adjacent to the backticks:
18+
To fix this, remove any spaces at the beginning or ending:
1919

2020
```markdown
2121
`some text`
2222
```
2323

24+
Note: Code spans containing only spaces are allowed by the specification:
25+
26+
```markdown
27+
` ` or ` `
28+
```
29+
2430
Note: A single leading and trailing space is allowed by the specification and
25-
automatically trimmed (in order to allow for code spans that embed backticks):
31+
automatically trimmed by the parser (in order to allow for code spans that embed
32+
backticks):
2633

2734
```markdown
2835
`` `backticks` ``
@@ -36,5 +43,4 @@ span markers from an embedded backtick (though the space is not trimmed):
3643
```
3744

3845
Rationale: Violations of this rule are usually unintentional and may lead to
39-
improperly-rendered content. If spaces beside backticks are intentional, this
40-
rule can be disabled for that line or file.
46+
improperly-rendered content.

lib/md038.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { filterByTypesCached } from "./cache.mjs";
66

77
const leftSpaceRe = /^\s(?:[^`]|$)/;
88
const rightSpaceRe = /[^`]\s$/;
9+
const allSpaceRe = /^\s*$/;
910
const trimCodeText = (text, start, end) => {
1011
text = text.replace(/^\s+$/, "");
1112
if (start) {
@@ -35,7 +36,10 @@ export default {
3536
if (startSequence && endSequence && startData && endData) {
3637
const spaceLeft = leftSpaceRe.test(startData.text);
3738
const spaceRight = rightSpaceRe.test(endData.text);
38-
if (spaceLeft || spaceRight) {
39+
if (
40+
(spaceLeft || spaceRight) &&
41+
!datas.every((data) => allSpaceRe.test(data.text))
42+
) {
3943
let lineNumber = startSequence.startLine;
4044
let range = undefined;
4145
let fixInfo = undefined;

test/snapshots/markdownlint-test-scenarios.mjs.md

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -50127,26 +50127,6 @@ Generated by [AVA](https://avajs.dev).
5012750127
'no-space-in-code',
5012850128
],
5012950129
},
50130-
{
50131-
errorContext: '` `',
50132-
errorDetail: null,
50133-
errorRange: [
50134-
14,
50135-
3,
50136-
],
50137-
fixInfo: {
50138-
deleteCount: 1,
50139-
editColumn: 15,
50140-
insertText: '',
50141-
},
50142-
lineNumber: 13,
50143-
ruleDescription: 'Spaces inside code span elements',
50144-
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
50145-
ruleNames: [
50146-
'MD038',
50147-
'no-space-in-code',
50148-
],
50149-
},
5015050130
{
5015150131
errorContext: '`` code``',
5015250132
errorDetail: null,
@@ -50347,26 +50327,6 @@ Generated by [AVA](https://avajs.dev).
5034750327
'no-space-in-code',
5034850328
],
5034950329
},
50350-
{
50351-
errorContext: '` `',
50352-
errorDetail: null,
50353-
errorRange: [
50354-
13,
50355-
6,
50356-
],
50357-
fixInfo: {
50358-
deleteCount: 4,
50359-
editColumn: 14,
50360-
insertText: '',
50361-
},
50362-
lineNumber: 109,
50363-
ruleDescription: 'Spaces inside code span elements',
50364-
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
50365-
ruleNames: [
50366-
'MD038',
50367-
'no-space-in-code',
50368-
],
50369-
},
5037050330
{
5037150331
errorContext: '` code `',
5037250332
errorDetail: null,
@@ -50379,7 +50339,7 @@ Generated by [AVA](https://avajs.dev).
5037950339
editColumn: 37,
5038050340
insertText: 'code',
5038150341
},
50382-
lineNumber: 111,
50342+
lineNumber: 114,
5038350343
ruleDescription: 'Spaces inside code span elements',
5038450344
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
5038550345
ruleNames: [
@@ -50399,7 +50359,7 @@ Generated by [AVA](https://avajs.dev).
5039950359
editColumn: 38,
5040050360
insertText: 'code',
5040150361
},
50402-
lineNumber: 113,
50362+
lineNumber: 116,
5040350363
ruleDescription: 'Spaces inside code span elements',
5040450364
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
5040550365
ruleNames: [
@@ -50419,7 +50379,7 @@ Generated by [AVA](https://avajs.dev).
5041950379
editColumn: 31,
5042050380
insertText: 'code',
5042150381
},
50422-
lineNumber: 115,
50382+
lineNumber: 118,
5042350383
ruleDescription: 'Spaces inside code span elements',
5042450384
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
5042550385
ruleNames: [
@@ -50439,7 +50399,7 @@ Generated by [AVA](https://avajs.dev).
5043950399
editColumn: 9,
5044050400
insertText: ' ` multiple leading {MD038}',
5044150401
},
50442-
lineNumber: 129,
50402+
lineNumber: 132,
5044350403
ruleDescription: 'Spaces inside code span elements',
5044450404
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
5044550405
ruleNames: [
@@ -50459,7 +50419,7 @@ Generated by [AVA](https://avajs.dev).
5045950419
editColumn: 1,
5046050420
insertText: 'not allowed ` ',
5046150421
},
50462-
lineNumber: 136,
50422+
lineNumber: 139,
5046350423
ruleDescription: 'Spaces inside code span elements',
5046450424
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
5046550425
ruleNames: [
@@ -50576,7 +50536,7 @@ Generated by [AVA](https://avajs.dev).
5057650536
5057750537
empty \`\` codespan element␊
5057850538
50579-
single space \`\` codespan element {MD038}
50539+
single space \` \` codespan element␊
5058050540
5058150541
\`,\`, \`.\`␊
5058250542
@@ -50672,7 +50632,10 @@ Generated by [AVA](https://avajs.dev).
5067250632
5067350633
Single start and end space: \` code \` (explicitly allowed/trimmed by the specification)␊
5067450634
50675-
All spaces: \`\` {MD038}␊
50635+
All spaces: \` \` \` \` \` \` \` \`␊
50636+
50637+
All line endings: \`␊
50638+
\`␊
5067650639
5067750640
Double start and single end space: \`code\` {MD038}␊
5067850641
-192 Bytes
Binary file not shown.

test/spaces_inside_codespan_elements.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
empty `` codespan element
1212

13-
single space ` ` codespan element {MD038}
13+
single space ` ` codespan element
1414

1515
`,`, `.`
1616

@@ -106,7 +106,10 @@ No start space, end space: `code ` {MD038}
106106

107107
Single start and end space: ` code ` (explicitly allowed/trimmed by the specification)
108108

109-
All spaces: ` ` {MD038}
109+
All spaces: ` ` ` ` ` ` ` `
110+
111+
All line endings: `
112+
`
110113

111114
Double start and single end space: ` code ` {MD038}
112115

0 commit comments

Comments
 (0)