2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2022-07-30 20:35:27 -07:00
|
|
|
const { addErrorContext, emphasisMarkersInContent, forEachLine, isBlankLine,
|
|
|
|
withinAnyRange } = require("../helpers");
|
|
|
|
const { htmlElementRanges, lineMetadata } = require("./cache");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2022-12-19 21:36:24 -08:00
|
|
|
const emphasisRe = /(^|[^\\]|\\\\)(?:(\*{1,3})|(_{1,3}))/g;
|
2023-05-27 11:05:17 -07:00
|
|
|
const embeddedUnderscoreRe = /([A-Za-z\d])(_([A-Za-z\d]))+/g;
|
2020-05-10 17:06:07 -07:00
|
|
|
const asteriskListItemMarkerRe = /^([\s>]*)\*(\s+)/;
|
2020-03-28 14:16:28 -07:00
|
|
|
const leftSpaceRe = /^\s+/;
|
|
|
|
const rightSpaceRe = /\s+$/;
|
2021-01-23 20:47:27 -08:00
|
|
|
const tablePipeRe = /\|/;
|
2023-05-27 11:05:17 -07:00
|
|
|
const allUnderscoresRe = /_/g;
|
2019-09-06 22:35:33 -07:00
|
|
|
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD037", "no-space-in-emphasis" ],
|
|
|
|
"description": "Spaces inside emphasis markers",
|
|
|
|
"tags": [ "whitespace", "emphasis" ],
|
|
|
|
"function": function MD037(params, onError) {
|
2022-07-30 20:35:27 -07:00
|
|
|
const exclusions = htmlElementRanges();
|
2020-04-12 20:40:05 -07:00
|
|
|
// eslint-disable-next-line init-declarations
|
2020-04-25 15:30:52 -07:00
|
|
|
let effectiveEmphasisLength, emphasisIndex, emphasisKind, emphasisLength,
|
|
|
|
pendingError = null;
|
2020-04-12 20:40:05 -07:00
|
|
|
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
|
|
function resetRunTracking() {
|
|
|
|
emphasisIndex = -1;
|
|
|
|
emphasisLength = 0;
|
2020-04-25 15:30:52 -07:00
|
|
|
emphasisKind = "";
|
2020-04-12 20:40:05 -07:00
|
|
|
effectiveEmphasisLength = 0;
|
|
|
|
pendingError = null;
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line jsdoc/require-jsdoc
|
2021-01-23 20:47:27 -08:00
|
|
|
function handleRunEnd(
|
|
|
|
line, lineIndex, contextLength, match, matchIndex, inTable
|
|
|
|
) {
|
2020-04-12 20:40:05 -07:00
|
|
|
// Close current run
|
|
|
|
let content = line.substring(emphasisIndex, matchIndex);
|
|
|
|
if (!emphasisLength) {
|
2021-02-06 19:55:22 -08:00
|
|
|
content = content.trimStart();
|
2020-04-12 20:40:05 -07:00
|
|
|
}
|
|
|
|
if (!match) {
|
2021-02-06 19:55:22 -08:00
|
|
|
content = content.trimEnd();
|
2020-04-12 20:40:05 -07:00
|
|
|
}
|
|
|
|
const leftSpace = leftSpaceRe.test(content);
|
|
|
|
const rightSpace = rightSpaceRe.test(content);
|
2021-01-23 20:47:27 -08:00
|
|
|
if (
|
|
|
|
(leftSpace || rightSpace) &&
|
|
|
|
(!inTable || !tablePipeRe.test(content))
|
|
|
|
) {
|
2020-04-12 20:40:05 -07:00
|
|
|
// Report the violation
|
|
|
|
const contextStart = emphasisIndex - emphasisLength;
|
|
|
|
const contextEnd = matchIndex + contextLength;
|
|
|
|
const column = contextStart + 1;
|
|
|
|
const length = contextEnd - contextStart;
|
2022-07-30 20:35:27 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
2020-04-12 20:40:05 -07:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Initialize
|
2020-05-08 16:01:42 -07:00
|
|
|
const ignoreMarkersByLine = emphasisMarkersInContent(params);
|
2020-04-12 20:40:05 -07:00
|
|
|
resetRunTracking();
|
2020-03-28 14:16:28 -07:00
|
|
|
forEachLine(
|
|
|
|
lineMetadata(),
|
2021-01-24 17:50:39 -08:00
|
|
|
(line, lineIndex, inCode, onFence, inTable, inItem, onBreak, inMath) => {
|
2020-04-12 20:40:05 -07:00
|
|
|
const onItemStart = (inItem === 1);
|
2021-08-22 15:28:28 -07:00
|
|
|
if (
|
|
|
|
inCode ||
|
|
|
|
onFence ||
|
|
|
|
inTable ||
|
|
|
|
onBreak ||
|
|
|
|
onItemStart ||
|
|
|
|
isBlankLine(line)
|
|
|
|
) {
|
2020-04-12 20:40:05 -07:00
|
|
|
// Emphasis resets when leaving a block
|
|
|
|
resetRunTracking();
|
|
|
|
}
|
2021-08-22 15:28:28 -07:00
|
|
|
if (
|
|
|
|
inCode ||
|
|
|
|
onFence ||
|
|
|
|
onBreak ||
|
|
|
|
inMath
|
|
|
|
) {
|
2020-03-28 14:16:28 -07:00
|
|
|
// Emphasis has no meaning here
|
|
|
|
return;
|
|
|
|
}
|
2023-05-27 11:05:17 -07:00
|
|
|
let patchedLine = line.replace(
|
|
|
|
embeddedUnderscoreRe,
|
|
|
|
(match) => match.replace(allUnderscoresRe, " ")
|
|
|
|
);
|
2020-04-12 20:40:05 -07:00
|
|
|
if (onItemStart) {
|
2020-03-28 14:16:28 -07:00
|
|
|
// Trim overlapping '*' list item marker
|
2021-12-21 22:21:28 +00:00
|
|
|
patchedLine = patchedLine.replace(asteriskListItemMarkerRe, "$1 $2");
|
2020-03-28 14:16:28 -07:00
|
|
|
}
|
2019-09-06 22:35:33 -07:00
|
|
|
let match = null;
|
2020-03-28 14:16:28 -07:00
|
|
|
// Match all emphasis-looking runs in the line...
|
2021-12-21 22:21:28 +00:00
|
|
|
while ((match = emphasisRe.exec(patchedLine))) {
|
2021-11-26 05:37:04 +00:00
|
|
|
const ignoreMarkersForLine = ignoreMarkersByLine[lineIndex];
|
2020-03-28 14:16:28 -07:00
|
|
|
const matchIndex = match.index + match[1].length;
|
2020-05-08 16:01:42 -07:00
|
|
|
if (ignoreMarkersForLine.includes(matchIndex)) {
|
|
|
|
// Ignore emphasis markers inside code spans and links
|
2020-04-25 15:10:07 -07:00
|
|
|
continue;
|
|
|
|
}
|
2020-03-28 14:16:28 -07:00
|
|
|
const matchLength = match[0].length - match[1].length;
|
2020-04-25 15:30:52 -07:00
|
|
|
const matchKind = (match[2] || match[3])[0];
|
2020-03-28 14:16:28 -07:00
|
|
|
if (emphasisIndex === -1) {
|
|
|
|
// New run
|
2020-04-11 12:33:28 -07:00
|
|
|
emphasisIndex = matchIndex + matchLength;
|
2020-03-28 14:16:28 -07:00
|
|
|
emphasisLength = matchLength;
|
2020-04-25 15:30:52 -07:00
|
|
|
emphasisKind = matchKind;
|
2020-04-11 12:33:28 -07:00
|
|
|
effectiveEmphasisLength = matchLength;
|
2020-05-10 17:06:07 -07:00
|
|
|
} else if (matchKind === emphasisKind) {
|
|
|
|
// Matching emphasis markers
|
|
|
|
if (matchLength === effectiveEmphasisLength) {
|
|
|
|
// Ending an existing run, report any pending error
|
|
|
|
if (pendingError) {
|
2021-01-06 20:20:18 -08:00
|
|
|
// @ts-ignore
|
2020-05-10 17:06:07 -07:00
|
|
|
addErrorContext(...pendingError);
|
|
|
|
pendingError = null;
|
|
|
|
}
|
|
|
|
const error = handleRunEnd(
|
2021-01-23 20:47:27 -08:00
|
|
|
line,
|
|
|
|
lineIndex,
|
|
|
|
effectiveEmphasisLength,
|
|
|
|
match,
|
|
|
|
matchIndex,
|
|
|
|
inTable
|
|
|
|
);
|
2020-05-10 17:06:07 -07:00
|
|
|
if (error) {
|
2021-01-06 20:20:18 -08:00
|
|
|
// @ts-ignore
|
2020-05-10 17:06:07 -07:00
|
|
|
addErrorContext(...error);
|
|
|
|
}
|
|
|
|
// Reset
|
|
|
|
resetRunTracking();
|
|
|
|
} else if (matchLength === 3) {
|
|
|
|
// Swap internal run length (1->2 or 2->1)
|
|
|
|
effectiveEmphasisLength = matchLength - effectiveEmphasisLength;
|
|
|
|
} else if (effectiveEmphasisLength === 3) {
|
|
|
|
// Downgrade internal run (3->1 or 3->2)
|
|
|
|
effectiveEmphasisLength -= matchLength;
|
|
|
|
} else {
|
|
|
|
// Upgrade to internal run (1->3 or 2->3)
|
|
|
|
effectiveEmphasisLength += matchLength;
|
2020-04-12 20:40:05 -07:00
|
|
|
}
|
2020-05-10 17:06:07 -07:00
|
|
|
// Back up one character so RegExp has a chance to match the
|
|
|
|
// next marker (ex: "**star**_underscore_")
|
2020-06-15 19:42:46 -07:00
|
|
|
if (emphasisRe.lastIndex > 1) {
|
|
|
|
emphasisRe.lastIndex--;
|
|
|
|
}
|
2020-05-12 20:13:51 -07:00
|
|
|
} else if (emphasisRe.lastIndex > 1) {
|
2020-05-10 17:06:07 -07:00
|
|
|
// Back up one character so RegExp has a chance to match the
|
|
|
|
// mis-matched marker (ex: "*text_*")
|
|
|
|
emphasisRe.lastIndex--;
|
2019-09-06 22:35:33 -07:00
|
|
|
}
|
2019-03-04 19:54:23 -08:00
|
|
|
}
|
2020-04-12 20:40:05 -07:00
|
|
|
if (emphasisIndex !== -1) {
|
|
|
|
pendingError = pendingError ||
|
2021-01-23 20:47:27 -08:00
|
|
|
handleRunEnd(line, lineIndex, 0, null, line.length, inTable);
|
2020-04-12 20:40:05 -07:00
|
|
|
// Adjust for pending run on new line
|
|
|
|
emphasisIndex = 0;
|
|
|
|
emphasisLength = 0;
|
|
|
|
}
|
2020-03-28 14:16:28 -07:00
|
|
|
}
|
|
|
|
);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|