Reimplement MD042/no-empty-links using micromark tokens.

This commit is contained in:
David Anson 2024-06-29 16:45:40 -07:00
parent 7377b75383
commit 964e4c80ab
6 changed files with 219 additions and 97 deletions

View file

@ -304,6 +304,21 @@ function filterByTypes(tokens, types, htmlFlow) {
return filterByPredicate(tokens, predicate);
}
/**
* Gets a list of nested Micromark token descendants by type path.
*
* @param {Token|Token[]} parent Micromark token parent or parents.
* @param {TokenType[]} typePath Micromark token type path.
* @returns {Token[]} Micromark token descendants.
*/
function getDescendantsByType(parent, typePath) {
let tokens = Array.isArray(parent) ? parent : [ parent ];
for (const type of typePath) {
tokens = tokens.flatMap((t) => t.children).filter((t) => t.type === type);
}
return tokens;
}
/**
* Gets the heading level of a Micromark heading tokan.
*
@ -472,6 +487,7 @@ module.exports = {
"parse": micromarkParse,
filterByPredicate,
filterByTypes,
getDescendantsByType,
getHeadingLevel,
getHeadingStyle,
getHeadingText,