Reimplement MD049/emphasis-style, MD050/strong-style to use micromark parser (with smaller ranges and handling of multi-line fixes).

This commit is contained in:
David Anson 2023-05-25 03:47:34 +00:00
parent e1233aad4b
commit 7005a8a438
9 changed files with 1211 additions and 348 deletions

View file

@ -1080,66 +1080,6 @@ function applyFixes(input, errors) {
}
module.exports.applyFixes = applyFixes;
/**
* Gets the range and fixInfo values for reporting an error if the expected
* text is found on the specified line.
*
* @param {string[]} lines Lines of Markdown content.
* @param {number} lineIndex Line index to check.
* @param {string} search Text to search for.
* @param {string} replace Text to replace with.
* @param {number} [instance] Instance on the line (1-based).
* @returns {Object} Range and fixInfo wrapper.
*/
module.exports.getRangeAndFixInfoIfFound =
(lines, lineIndex, search, replace, instance = 1) => {
let range = null;
let fixInfo = null;
let searchIndex = -1;
while (instance > 0) {
searchIndex = lines[lineIndex].indexOf(search, searchIndex + 1);
instance--;
}
if (searchIndex !== -1) {
const column = searchIndex + 1;
const length = search.length;
range = [ column, length ];
fixInfo = {
"editColumn": column,
"deleteCount": length,
"insertText": replace
};
}
return {
range,
fixInfo
};
};
/**
* Gets the next (subsequent) child token if it is of the expected type.
*
* @param {Object} parentToken Parent token.
* @param {Object} childToken Child token basis.
* @param {string} nextType Token type of next token.
* @param {string} nextNextType Token type of next-next token.
* @returns {Object} Next token.
*/
function getNextChildToken(parentToken, childToken, nextType, nextNextType) {
const { children } = parentToken;
const index = children.indexOf(childToken);
if (
(index !== -1) &&
(children.length > index + 2) &&
(children[index + 1].type === nextType) &&
(children[index + 2].type === nextNextType)
) {
return children[index + 1];
}
return null;
}
module.exports.getNextChildToken = getNextChildToken;
/**
* Expands a path with a tilde to an absolute path.
*

View file

@ -203,6 +203,17 @@ function matchAndGetTokensByType(tokens, matchTypes, resultTypes) {
return result;
}
/**
* Returns the specified token iff it is of the desired type.
*
* @param {Token} token Micromark token candidate.
* @param {string} type Desired type.
* @returns {Token | null} Token instance.
*/
function tokenIfType(token, type) {
return (token && (token.type === type)) ? token : null;
}
module.exports = {
"parse": micromarkParse,
filterByPredicate,
@ -210,5 +221,6 @@ module.exports = {
getHtmlTagInfo,
getMicromarkEvents,
getTokenTextByType,
matchAndGetTokensByType
matchAndGetTokensByType,
tokenIfType
};