Skip to content

Commit

Permalink
Update MD037/no-space-in-emphasis to ignore embedded underscore empha…
Browse files Browse the repository at this point in the history
…sis markers (fixes #444, fixes #408, fixes #354, fixes #324).
  • Loading branch information
DavidAnson committed Dec 21, 2021
1 parent 3e8d332 commit 7cf9c2d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 4 additions & 2 deletions demo/markdownlint-browser.js
Expand Up @@ -3480,6 +3480,7 @@ module.exports = {
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addErrorContext = _a.addErrorContext, emphasisMarkersInContent = _a.emphasisMarkersInContent, forEachLine = _a.forEachLine, isBlankLine = _a.isBlankLine;
var lineMetadata = (__webpack_require__(/*! ./cache */ "../lib/cache.js").lineMetadata);
var emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
var embeddedUnderscoreRe = /([A-Za-z0-9])_([A-Za-z0-9])/g;
var asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
var leftSpaceRe = /^\s+/;
var rightSpaceRe = /\s+$/;
Expand Down Expand Up @@ -3559,13 +3560,14 @@ module.exports = {
// Emphasis has no meaning here
return;
}
var patchedLine = line.replace(embeddedUnderscoreRe, "$1 $2");
if (onItemStart) {
// Trim overlapping '*' list item marker
line = line.replace(asteriskListItemMarkerRe, "$1 $2");
patchedLine = patchedLine.replace(asteriskListItemMarkerRe, "$1 $2");
}
var match = null;
// Match all emphasis-looking runs in the line...
while ((match = emphasisRe.exec(line))) {
while ((match = emphasisRe.exec(patchedLine))) {
var ignoreMarkersForLine = ignoreMarkersByLine[lineIndex];
var matchIndex = match.index + match[1].length;
if (ignoreMarkersForLine.includes(matchIndex)) {
Expand Down
6 changes: 4 additions & 2 deletions lib/md037.js
Expand Up @@ -7,6 +7,7 @@ const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine } =
const { lineMetadata } = require("./cache");

const emphasisRe = /(^|[^\\]|\\\\)(?:(\*\*?\*?)|(__?_?))/g;
const embeddedUnderscoreRe = /([A-Za-z0-9])_([A-Za-z0-9])/g;
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
const leftSpaceRe = /^\s+/;
const rightSpaceRe = /\s+$/;
Expand Down Expand Up @@ -98,13 +99,14 @@ module.exports = {
// Emphasis has no meaning here
return;
}
let patchedLine = line.replace(embeddedUnderscoreRe, "$1 $2");
if (onItemStart) {
// Trim overlapping '*' list item marker
line = line.replace(asteriskListItemMarkerRe, "$1 $2");
patchedLine = patchedLine.replace(asteriskListItemMarkerRe, "$1 $2");
}
let match = null;
// Match all emphasis-looking runs in the line...
while ((match = emphasisRe.exec(line))) {
while ((match = emphasisRe.exec(patchedLine))) {
const ignoreMarkersForLine = ignoreMarkersByLine[lineIndex];
const matchIndex = match.index + match[1].length;
if (ignoreMarkersForLine.includes(matchIndex)) {
Expand Down
10 changes: 10 additions & 0 deletions test/spaces_inside_emphasis_markers.md
Expand Up @@ -347,3 +347,13 @@ text [reference*link] star * star text
```yaml /* autogenerated */
# YAML...
```

new_value from *old_value* and *older_value*.

:ballot_box_with_check: _Emoji syntax_

some_snake_case_function() is _called_

_~/.ssh/id_rsa_ and _emphasis_

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

0 comments on commit 7cf9c2d

Please sign in to comment.