Update MD038/no-space-in-code to allow code spans with only spaces (fixes #1481).

This commit is contained in:
David Anson 2025-02-02 21:36:35 -08:00
parent 2d2fafc58d
commit 90cf515ff0
7 changed files with 56 additions and 68 deletions

View file

@ -1,5 +1,5 @@
This rule is triggered for code span elements that have spaces adjacent to the
backticks:
This rule is triggered for code spans that have content with spaces next to the
beginning or ending backticks:
```markdown
`some text `
@ -7,14 +7,21 @@ backticks:
` some text`
```
To fix this, remove any spaces adjacent to the backticks:
To fix this, remove any spaces at the beginning or ending:
```markdown
`some text`
```
Note: Code spans containing only spaces are allowed by the specification:
```markdown
` ` or ` `
```
Note: A single leading and trailing space is allowed by the specification and
automatically trimmed (in order to allow for code spans that embed backticks):
automatically trimmed by the parser (in order to allow for code spans that embed
backticks):
```markdown
`` `backticks` ``
@ -28,5 +35,4 @@ span markers from an embedded backtick (though the space is not trimmed):
```
Rationale: Violations of this rule are usually unintentional and may lead to
improperly-rendered content. If spaces beside backticks are intentional, this
rule can be disabled for that line or file.
improperly-rendered content.

View file

@ -1528,8 +1528,8 @@ Aliases: `no-space-in-code`
Fixable: Some violations can be fixed by tooling
This rule is triggered for code span elements that have spaces adjacent to the
backticks:
This rule is triggered for code spans that have content with spaces next to the
beginning or ending backticks:
```markdown
`some text `
@ -1537,14 +1537,21 @@ backticks:
` some text`
```
To fix this, remove any spaces adjacent to the backticks:
To fix this, remove any spaces at the beginning or ending:
```markdown
`some text`
```
Note: Code spans containing only spaces are allowed by the specification:
```markdown
` ` or ` `
```
Note: A single leading and trailing space is allowed by the specification and
automatically trimmed (in order to allow for code spans that embed backticks):
automatically trimmed by the parser (in order to allow for code spans that embed
backticks):
```markdown
`` `backticks` ``
@ -1558,8 +1565,7 @@ span markers from an embedded backtick (though the space is not trimmed):
```
Rationale: Violations of this rule are usually unintentional and may lead to
improperly-rendered content. If spaces beside backticks are intentional, this
rule can be disabled for that line or file.
improperly-rendered content.
<a name="md039"></a>

View file

@ -6,8 +6,8 @@ Aliases: `no-space-in-code`
Fixable: Some violations can be fixed by tooling
This rule is triggered for code span elements that have spaces adjacent to the
backticks:
This rule is triggered for code spans that have content with spaces next to the
beginning or ending backticks:
```markdown
`some text `
@ -15,14 +15,21 @@ backticks:
` some text`
```
To fix this, remove any spaces adjacent to the backticks:
To fix this, remove any spaces at the beginning or ending:
```markdown
`some text`
```
Note: Code spans containing only spaces are allowed by the specification:
```markdown
` ` or ` `
```
Note: A single leading and trailing space is allowed by the specification and
automatically trimmed (in order to allow for code spans that embed backticks):
automatically trimmed by the parser (in order to allow for code spans that embed
backticks):
```markdown
`` `backticks` ``
@ -36,5 +43,4 @@ span markers from an embedded backtick (though the space is not trimmed):
```
Rationale: Violations of this rule are usually unintentional and may lead to
improperly-rendered content. If spaces beside backticks are intentional, this
rule can be disabled for that line or file.
improperly-rendered content.

View file

@ -6,6 +6,7 @@ import { filterByTypesCached } from "./cache.mjs";
const leftSpaceRe = /^\s(?:[^`]|$)/;
const rightSpaceRe = /[^`]\s$/;
const allSpaceRe = /^\s*$/;
const trimCodeText = (text, start, end) => {
text = text.replace(/^\s+$/, "");
if (start) {
@ -35,7 +36,10 @@ export default {
if (startSequence && endSequence && startData && endData) {
const spaceLeft = leftSpaceRe.test(startData.text);
const spaceRight = rightSpaceRe.test(endData.text);
if (spaceLeft || spaceRight) {
if (
(spaceLeft || spaceRight) &&
!datas.every((data) => allSpaceRe.test(data.text))
) {
let lineNumber = startSequence.startLine;
let range = undefined;
let fixInfo = undefined;

View file

@ -50127,26 +50127,6 @@ Generated by [AVA](https://avajs.dev).
'no-space-in-code',
],
},
{
errorContext: '` `',
errorDetail: null,
errorRange: [
14,
3,
],
fixInfo: {
deleteCount: 1,
editColumn: 15,
insertText: '',
},
lineNumber: 13,
ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [
'MD038',
'no-space-in-code',
],
},
{
errorContext: '`` code``',
errorDetail: null,
@ -50347,26 +50327,6 @@ Generated by [AVA](https://avajs.dev).
'no-space-in-code',
],
},
{
errorContext: '` `',
errorDetail: null,
errorRange: [
13,
6,
],
fixInfo: {
deleteCount: 4,
editColumn: 14,
insertText: '',
},
lineNumber: 109,
ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [
'MD038',
'no-space-in-code',
],
},
{
errorContext: '` code `',
errorDetail: null,
@ -50379,7 +50339,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 37,
insertText: 'code',
},
lineNumber: 111,
lineNumber: 114,
ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [
@ -50399,7 +50359,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 38,
insertText: 'code',
},
lineNumber: 113,
lineNumber: 116,
ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [
@ -50419,7 +50379,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 31,
insertText: 'code',
},
lineNumber: 115,
lineNumber: 118,
ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [
@ -50439,7 +50399,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 9,
insertText: ' ` multiple leading {MD038}',
},
lineNumber: 129,
lineNumber: 132,
ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [
@ -50459,7 +50419,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 1,
insertText: 'not allowed ` ',
},
lineNumber: 136,
lineNumber: 139,
ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [
@ -50576,7 +50536,7 @@ Generated by [AVA](https://avajs.dev).
empty \`\` codespan element␊
single space \`\` codespan element {MD038}
single space \` \` codespan element␊
\`,\`, \`.\`␊
@ -50672,7 +50632,10 @@ Generated by [AVA](https://avajs.dev).
Single start and end space: \` code \` (explicitly allowed/trimmed by the specification)␊
All spaces: \`\` {MD038}␊
All spaces: \` \` \` \` \` \` \` \`␊
All line endings: \`␊
\`␊
Double start and single end space: \`code\` {MD038}␊

View file

@ -10,7 +10,7 @@
empty `` codespan element
single space ` ` codespan element {MD038}
single space ` ` codespan element
`,`, `.`
@ -106,7 +106,10 @@ No start space, end space: `code ` {MD038}
Single start and end space: ` code ` (explicitly allowed/trimmed by the specification)
All spaces: ` ` {MD038}
All spaces: ` ` ` ` ` ` ` `
All line endings: `
`
Double start and single end space: ` code ` {MD038}