Skip to content

Commit

Permalink
Update MD037/no-space-in-emphasis to ignore the content of HTML attri…
Browse files Browse the repository at this point in the history
…butes (fixes #540).
  • Loading branch information
DavidAnson committed Jul 31, 2022
1 parent 48f47b5 commit 1154ab4
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 39 deletions.
41 changes: 22 additions & 19 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3841,8 +3841,8 @@ module.exports = {
"use strict";
// @ts-check

const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine, withinAnyRange } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { htmlElementRanges, lineMetadata } = __webpack_require__(/*! ./cache */ "../lib/cache.js");
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
const embeddedUnderscoreRe = /([A-Za-z0-9])_([A-Za-z0-9])/g;
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
Expand All @@ -3854,6 +3854,7 @@ module.exports = {
"description": "Spaces inside emphasis markers",
"tags": ["whitespace", "emphasis"],
"function": function MD037(params, onError) {
const exclusions = htmlElementRanges();
// eslint-disable-next-line init-declarations
let effectiveEmphasisLength, emphasisIndex, emphasisKind, emphasisLength, pendingError = null;
// eslint-disable-next-line jsdoc/require-jsdoc
Expand Down Expand Up @@ -3881,25 +3882,27 @@ module.exports = {
// Report the violation
const contextStart = emphasisIndex - emphasisLength;
const contextEnd = matchIndex + contextLength;
const context = line.substring(contextStart, contextEnd);
const column = contextStart + 1;
const length = contextEnd - contextStart;
const leftMarker = line.substring(contextStart, emphasisIndex);
const rightMarker = match ? (match[2] || match[3]) : "";
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
return [
onError,
lineIndex + 1,
context,
leftSpace,
rightSpace,
[column, length],
{
"editColumn": column,
"deleteCount": length,
"insertText": fixedText
}
];
if (!withinAnyRange(exclusions, lineIndex, column, length)) {
const context = line.substring(contextStart, contextEnd);
const leftMarker = line.substring(contextStart, emphasisIndex);
const rightMarker = match ? (match[2] || match[3]) : "";
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
return [
onError,
lineIndex + 1,
context,
leftSpace,
rightSpace,
[column, length],
{
"editColumn": column,
"deleteCount": length,
"insertText": fixedText
}
];
}
}
return null;
}
Expand Down
43 changes: 23 additions & 20 deletions lib/md037.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

"use strict";

const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine } =
require("../helpers");
const { lineMetadata } = require("./cache");
const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine,
withinAnyRange } = require("../helpers");
const { htmlElementRanges, lineMetadata } = require("./cache");

const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
const embeddedUnderscoreRe = /([A-Za-z0-9])_([A-Za-z0-9])/g;
Expand All @@ -18,6 +18,7 @@ module.exports = {
"description": "Spaces inside emphasis markers",
"tags": [ "whitespace", "emphasis" ],
"function": function MD037(params, onError) {
const exclusions = htmlElementRanges();
// eslint-disable-next-line init-declarations
let effectiveEmphasisLength, emphasisIndex, emphasisKind, emphasisLength,
pendingError = null;
Expand Down Expand Up @@ -50,25 +51,27 @@ module.exports = {
// Report the violation
const contextStart = emphasisIndex - emphasisLength;
const contextEnd = matchIndex + contextLength;
const context = line.substring(contextStart, contextEnd);
const column = contextStart + 1;
const length = contextEnd - contextStart;
const leftMarker = line.substring(contextStart, emphasisIndex);
const rightMarker = match ? (match[2] || match[3]) : "";
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
return [
onError,
lineIndex + 1,
context,
leftSpace,
rightSpace,
[ column, length ],
{
"editColumn": column,
"deleteCount": length,
"insertText": fixedText
}
];
if (!withinAnyRange(exclusions, lineIndex, column, length)) {
const context = line.substring(contextStart, contextEnd);
const leftMarker = line.substring(contextStart, emphasisIndex);
const rightMarker = match ? (match[2] || match[3]) : "";
const fixedText = `${leftMarker}${content.trim()}${rightMarker}`;
return [
onError,
lineIndex + 1,
context,
leftSpace,
rightSpace,
[ column, length ],
{
"editColumn": column,
"deleteCount": length,
"insertText": fixedText
}
];
}
}
return null;
}
Expand Down
152 changes: 152 additions & 0 deletions test/snapshots/markdownlint-test-scenarios.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -37062,6 +37062,86 @@ Generated by [AVA](https://avajs.dev).

{
errors: [
{
errorContext: null,
errorDetail: 'Element: b',
errorRange: [
10,
3,
],
fixInfo: null,
lineNumber: 361,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: p',
errorRange: [
1,
3,
],
fixInfo: null,
lineNumber: 363,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: p',
errorRange: [
10,
39,
],
fixInfo: null,
lineNumber: 367,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: img',
errorRange: [
10,
41,
],
fixInfo: null,
lineNumber: 369,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: p',
errorRange: [
10,
24,
],
fixInfo: null,
lineNumber: 371,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md033',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: '* emphasis *',
errorDetail: null,
Expand Down Expand Up @@ -38582,6 +38662,66 @@ Generated by [AVA](https://avajs.dev).
'no-space-in-emphasis',
],
},
{
errorContext: '* HTML *',
errorDetail: null,
errorRange: [
20,
8,
],
fixInfo: {
deleteCount: 8,
editColumn: 20,
insertText: '*HTML*',
},
lineNumber: 361,
ruleDescription: 'Spaces inside emphasis markers',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md037',
ruleNames: [
'MD037',
'no-space-in-emphasis',
],
},
{
errorContext: '* HTML *',
errorDetail: null,
errorRange: [
17,
8,
],
fixInfo: {
deleteCount: 8,
editColumn: 17,
insertText: '*HTML*',
},
lineNumber: 364,
ruleDescription: 'Spaces inside emphasis markers',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md037',
ruleNames: [
'MD037',
'no-space-in-emphasis',
],
},
{
errorContext: '* HTML *',
errorDetail: null,
errorRange: [
34,
8,
],
fixInfo: {
deleteCount: 8,
editColumn: 34,
insertText: '*HTML*',
},
lineNumber: 371,
ruleDescription: 'Spaces inside emphasis markers',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md037',
ruleNames: [
'MD037',
'no-space-in-emphasis',
],
},
],
fixed: `# Heading␊
Expand Down Expand Up @@ -38942,6 +39082,18 @@ Generated by [AVA](https://avajs.dev).
_~/.ssh/id_rsa_ and _emphasis_␊
Partial *em*phasis of a *wo*rd.␊
Emphasis <b>inside *HTML* content</b> {MD033} {MD037}␊
<p> {MD033}␊
Emphasis inside *HTML* content {MD037}␊
</p>␊
Emphasis <p data="inside * attribute * content"></p> {MD033}␊
Emphasis <img alt="inside * attribute * content"/> {MD033}␊
Emphasis <p data="* attribute *">*HTML*</p> {MD033} {MD037}␊
`,
}

Expand Down
Binary file modified test/snapshots/markdownlint-test-scenarios.js.snap
Binary file not shown.
12 changes: 12 additions & 0 deletions test/spaces_inside_emphasis_markers.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,15 @@ some_snake_case_function() is _called_
_~/.ssh/id_rsa_ and _emphasis_

Partial *em*phasis of a *wo*rd.

Emphasis <b>inside * HTML * content</b> {MD033} {MD037}

<p> {MD033}
Emphasis inside * HTML * content {MD037}
</p>

Emphasis <p data="inside * attribute * content"></p> {MD033}

Emphasis <img alt="inside * attribute * content"/> {MD033}

Emphasis <p data="* attribute *">* HTML *</p> {MD033} {MD037}

0 comments on commit 1154ab4

Please sign in to comment.