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 This rule is triggered for code spans that have content with spaces next to the
backticks: beginning or ending backticks:
```markdown ```markdown
`some text ` `some text `
@ -7,14 +7,21 @@ backticks:
` some text` ` some text`
``` ```
To fix this, remove any spaces adjacent to the backticks: To fix this, remove any spaces at the beginning or ending:
```markdown ```markdown
`some text` `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 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 ```markdown
`` `backticks` `` `` `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 Rationale: Violations of this rule are usually unintentional and may lead to
improperly-rendered content. If spaces beside backticks are intentional, this improperly-rendered content.
rule can be disabled for that line or file.

View file

@ -1528,8 +1528,8 @@ Aliases: `no-space-in-code`
Fixable: Some violations can be fixed by tooling Fixable: Some violations can be fixed by tooling
This rule is triggered for code span elements that have spaces adjacent to the This rule is triggered for code spans that have content with spaces next to the
backticks: beginning or ending backticks:
```markdown ```markdown
`some text ` `some text `
@ -1537,14 +1537,21 @@ backticks:
` some text` ` some text`
``` ```
To fix this, remove any spaces adjacent to the backticks: To fix this, remove any spaces at the beginning or ending:
```markdown ```markdown
`some text` `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 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 ```markdown
`` `backticks` `` `` `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 Rationale: Violations of this rule are usually unintentional and may lead to
improperly-rendered content. If spaces beside backticks are intentional, this improperly-rendered content.
rule can be disabled for that line or file.
<a name="md039"></a> <a name="md039"></a>

View file

@ -6,8 +6,8 @@ Aliases: `no-space-in-code`
Fixable: Some violations can be fixed by tooling Fixable: Some violations can be fixed by tooling
This rule is triggered for code span elements that have spaces adjacent to the This rule is triggered for code spans that have content with spaces next to the
backticks: beginning or ending backticks:
```markdown ```markdown
`some text ` `some text `
@ -15,14 +15,21 @@ backticks:
` some text` ` some text`
``` ```
To fix this, remove any spaces adjacent to the backticks: To fix this, remove any spaces at the beginning or ending:
```markdown ```markdown
`some text` `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 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 ```markdown
`` `backticks` `` `` `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 Rationale: Violations of this rule are usually unintentional and may lead to
improperly-rendered content. If spaces beside backticks are intentional, this improperly-rendered content.
rule can be disabled for that line or file.

View file

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

View file

@ -50127,26 +50127,6 @@ Generated by [AVA](https://avajs.dev).
'no-space-in-code', '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``', errorContext: '`` code``',
errorDetail: null, errorDetail: null,
@ -50347,26 +50327,6 @@ Generated by [AVA](https://avajs.dev).
'no-space-in-code', '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 `', errorContext: '` code `',
errorDetail: null, errorDetail: null,
@ -50379,7 +50339,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 37, editColumn: 37,
insertText: 'code', insertText: 'code',
}, },
lineNumber: 111, lineNumber: 114,
ruleDescription: 'Spaces inside code span elements', ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [ ruleNames: [
@ -50399,7 +50359,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 38, editColumn: 38,
insertText: 'code', insertText: 'code',
}, },
lineNumber: 113, lineNumber: 116,
ruleDescription: 'Spaces inside code span elements', ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [ ruleNames: [
@ -50419,7 +50379,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 31, editColumn: 31,
insertText: 'code', insertText: 'code',
}, },
lineNumber: 115, lineNumber: 118,
ruleDescription: 'Spaces inside code span elements', ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [ ruleNames: [
@ -50439,7 +50399,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 9, editColumn: 9,
insertText: ' ` multiple leading {MD038}', insertText: ' ` multiple leading {MD038}',
}, },
lineNumber: 129, lineNumber: 132,
ruleDescription: 'Spaces inside code span elements', ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [ ruleNames: [
@ -50459,7 +50419,7 @@ Generated by [AVA](https://avajs.dev).
editColumn: 1, editColumn: 1,
insertText: 'not allowed ` ', insertText: 'not allowed ` ',
}, },
lineNumber: 136, lineNumber: 139,
ruleDescription: 'Spaces inside code span elements', ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md', ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [ ruleNames: [
@ -50576,7 +50536,7 @@ Generated by [AVA](https://avajs.dev).
empty \`\` codespan element␊ 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)␊ 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}␊ Double start and single end space: \`code\` {MD038}␊

View file

@ -10,7 +10,7 @@
empty `` codespan element 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) 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} Double start and single end space: ` code ` {MD038}