Skip to content

Commit 1154ab4

Browse files
committed
Update MD037/no-space-in-emphasis to ignore the content of HTML attributes (fixes #540).
1 parent 48f47b5 commit 1154ab4

File tree

5 files changed

+209
-39
lines changed

5 files changed

+209
-39
lines changed

demo/markdownlint-browser.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3841,8 +3841,8 @@ module.exports = {
38413841
"use strict";
38423842
// @ts-check
38433843

3844-
const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
3845-
const { lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
3844+
const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
3845+
const { htmlElementRanges, lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
38463846
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
38473847
const embeddedUnderscoreRe = /([A-Za-z0-9])_([A-Za-z0-9])/g;
38483848
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
@@ -3854,6 +3854,7 @@ module.exports = {
38543854
"description": "Spaces inside emphasis markers",
38553855
"tags": ["whitespace", "emphasis"],
38563856
"function": function MD037(params, onError) {
3857+
const exclusions = htmlElementRanges();
38573858
// eslint-disable-next-line init-declarations
38583859
let effectiveEmphasisLength, emphasisIndex, emphasisKind, emphasisLength, pendingError = null;
38593860
// eslint-disable-next-line jsdoc/require-jsdoc
@@ -3881,25 +3882,27 @@ module.exports = {
38813882
// Report the violation
38823883
const contextStart = emphasisIndex - emphasisLength;
38833884
const contextEnd = matchIndex + contextLength;
3884-
const context = line.substring(contextStart, contextEnd);
38853885
const column = contextStart + 1;
38863886
const length = contextEnd - contextStart;
3887-
const leftMarker = line.substring(contextStart, emphasisIndex);
3888-
const rightMarker = match ? (match[2] || match[3]) : "";
3889-
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
3890-
return [
3891-
onError,
3892-
lineIndex + 1,
3893-
context,
3894-
leftSpace,
3895-
rightSpace,
3896-
[column, length],
3897-
{
3898-
"editColumn": column,
3899-
"deleteCount": length,
3900-
"insertText": fixedText
3901-
}
3902-
];
3887+
if (!withinAnyRange(exclusions, lineIndex, column, length)) {
3888+
const context = line.substring(contextStart, contextEnd);
3889+
const leftMarker = line.substring(contextStart, emphasisIndex);
3890+
const rightMarker = match ? (match[2] || match[3]) : "";
3891+
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
3892+
return [
3893+
onError,
3894+
lineIndex + 1,
3895+
context,
3896+
leftSpace,
3897+
rightSpace,
3898+
[column, length],
3899+
{
3900+
"editColumn": column,
3901+
"deleteCount": length,
3902+
"insertText": fixedText
3903+
}
3904+
];
3905+
}
39033906
}
39043907
return null;
39053908
}

lib/md037.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
"use strict";
44

5-
const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine } =
6-
require("../helpers");
7-
const { lineMetadata } = require("./cache");
5+
const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine,
6+
withinAnyRange } = require("../helpers");
7+
const { htmlElementRanges, lineMetadata } = require("./cache");
88

99
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
1010
const embeddedUnderscoreRe = /([A-Za-z0-9])_([A-Za-z0-9])/g;
@@ -18,6 +18,7 @@ module.exports = {
1818
"description": "Spaces inside emphasis markers",
1919
"tags": [ "whitespace", "emphasis" ],
2020
"function": function MD037(params, onError) {
21+
const exclusions = htmlElementRanges();
2122
// eslint-disable-next-line init-declarations
2223
let effectiveEmphasisLength, emphasisIndex, emphasisKind, emphasisLength,
2324
pendingError = null;
@@ -50,25 +51,27 @@ module.exports = {
5051
// Report the violation
5152
const contextStart = emphasisIndex - emphasisLength;
5253
const contextEnd = matchIndex + contextLength;
53-
const context = line.substring(contextStart, contextEnd);
5454
const column = contextStart + 1;
5555
const length = contextEnd - contextStart;
56-
const leftMarker = line.substring(contextStart, emphasisIndex);
57-
const rightMarker = match ? (match[2] || match[3]) : "";
58-
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
59-
return [
60-
onError,
61-
lineIndex + 1,
62-
context,
63-
leftSpace,
64-
rightSpace,
65-
[ column, length ],
66-
{
67-
"editColumn": column,
68-
"deleteCount": length,
69-
"insertText": fixedText
70-
}
71-
];
56+
if (!withinAnyRange(exclusions, lineIndex, column, length)) {
57+
const context = line.substring(contextStart, contextEnd);
58+
const leftMarker = line.substring(contextStart, emphasisIndex);
59+
const rightMarker = match ? (match[2] || match[3]) : "";
60+
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
61+
return [
62+
onError,
63+
lineIndex + 1,
64+
context,
65+
leftSpace,
66+
rightSpace,
67+
[ column, length ],
68+
{
69+
"editColumn": column,
70+
"deleteCount": length,
71+
"insertText": fixedText
72+
}
73+
];
74+
}
7275
}
7376
return null;
7477
}

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

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37062,6 +37062,86 @@ Generated by [AVA](https://avajs.dev).
3706237062

3706337063
{
3706437064
errors: [
37065+
{
37066+
errorContext: null,
37067+
errorDetail: 'Element: b',
37068+
errorRange: [
37069+
10,
37070+
3,
37071+
],
37072+
fixInfo: null,
37073+
lineNumber: 361,
37074+
ruleDescription: 'Inline HTML',
37075+
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
37076+
ruleNames: [
37077+
'MD033',
37078+
'no-inline-html',
37079+
],
37080+
},
37081+
{
37082+
errorContext: null,
37083+
errorDetail: 'Element: p',
37084+
errorRange: [
37085+
1,
37086+
3,
37087+
],
37088+
fixInfo: null,
37089+
lineNumber: 363,
37090+
ruleDescription: 'Inline HTML',
37091+
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
37092+
ruleNames: [
37093+
'MD033',
37094+
'no-inline-html',
37095+
],
37096+
},
37097+
{
37098+
errorContext: null,
37099+
errorDetail: 'Element: p',
37100+
errorRange: [
37101+
10,
37102+
39,
37103+
],
37104+
fixInfo: null,
37105+
lineNumber: 367,
37106+
ruleDescription: 'Inline HTML',
37107+
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
37108+
ruleNames: [
37109+
'MD033',
37110+
'no-inline-html',
37111+
],
37112+
},
37113+
{
37114+
errorContext: null,
37115+
errorDetail: 'Element: img',
37116+
errorRange: [
37117+
10,
37118+
41,
37119+
],
37120+
fixInfo: null,
37121+
lineNumber: 369,
37122+
ruleDescription: 'Inline HTML',
37123+
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
37124+
ruleNames: [
37125+
'MD033',
37126+
'no-inline-html',
37127+
],
37128+
},
37129+
{
37130+
errorContext: null,
37131+
errorDetail: 'Element: p',
37132+
errorRange: [
37133+
10,
37134+
24,
37135+
],
37136+
fixInfo: null,
37137+
lineNumber: 371,
37138+
ruleDescription: 'Inline HTML',
37139+
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
37140+
ruleNames: [
37141+
'MD033',
37142+
'no-inline-html',
37143+
],
37144+
},
3706537145
{
3706637146
errorContext: '* emphasis *',
3706737147
errorDetail: null,
@@ -38582,6 +38662,66 @@ Generated by [AVA](https://avajs.dev).
3858238662
'no-space-in-emphasis',
3858338663
],
3858438664
},
38665+
{
38666+
errorContext: '* HTML *',
38667+
errorDetail: null,
38668+
errorRange: [
38669+
20,
38670+
8,
38671+
],
38672+
fixInfo: {
38673+
deleteCount: 8,
38674+
editColumn: 20,
38675+
insertText: '*HTML*',
38676+
},
38677+
lineNumber: 361,
38678+
ruleDescription: 'Spaces inside emphasis markers',
38679+
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md037',
38680+
ruleNames: [
38681+
'MD037',
38682+
'no-space-in-emphasis',
38683+
],
38684+
},
38685+
{
38686+
errorContext: '* HTML *',
38687+
errorDetail: null,
38688+
errorRange: [
38689+
17,
38690+
8,
38691+
],
38692+
fixInfo: {
38693+
deleteCount: 8,
38694+
editColumn: 17,
38695+
insertText: '*HTML*',
38696+
},
38697+
lineNumber: 364,
38698+
ruleDescription: 'Spaces inside emphasis markers',
38699+
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md037',
38700+
ruleNames: [
38701+
'MD037',
38702+
'no-space-in-emphasis',
38703+
],
38704+
},
38705+
{
38706+
errorContext: '* HTML *',
38707+
errorDetail: null,
38708+
errorRange: [
38709+
34,
38710+
8,
38711+
],
38712+
fixInfo: {
38713+
deleteCount: 8,
38714+
editColumn: 34,
38715+
insertText: '*HTML*',
38716+
},
38717+
lineNumber: 371,
38718+
ruleDescription: 'Spaces inside emphasis markers',
38719+
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md037',
38720+
ruleNames: [
38721+
'MD037',
38722+
'no-space-in-emphasis',
38723+
],
38724+
},
3858538725
],
3858638726
fixed: `# Heading␊
3858738727
@@ -38942,6 +39082,18 @@ Generated by [AVA](https://avajs.dev).
3894239082
_~/.ssh/id_rsa_ and _emphasis_␊
3894339083
3894439084
Partial *em*phasis of a *wo*rd.␊
39085+
39086+
Emphasis <b>inside *HTML* content</b> {MD033} {MD037}␊
39087+
39088+
<p> {MD033}␊
39089+
Emphasis inside *HTML* content {MD037}␊
39090+
</p>␊
39091+
39092+
Emphasis <p data="inside * attribute * content"></p> {MD033}␊
39093+
39094+
Emphasis <img alt="inside * attribute * content"/> {MD033}␊
39095+
39096+
Emphasis <p data="* attribute *">*HTML*</p> {MD033} {MD037}␊
3894539097
`,
3894639098
}
3894739099

757 Bytes
Binary file not shown.

test/spaces_inside_emphasis_markers.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,15 @@ some_snake_case_function() is _called_
357357
_~/.ssh/id_rsa_ and _emphasis_
358358

359359
Partial *em*phasis of a *wo*rd.
360+
361+
Emphasis <b>inside * HTML * content</b> {MD033} {MD037}
362+
363+
<p> {MD033}
364+
Emphasis inside * HTML * content {MD037}
365+
</p>
366+
367+
Emphasis <p data="inside * attribute * content"></p> {MD033}
368+
369+
Emphasis <img alt="inside * attribute * content"/> {MD033}
370+
371+
Emphasis <p data="* attribute *">* HTML *</p> {MD033} {MD037}

0 commit comments

Comments
 (0)