Refactor new MD054/link-image-style rule, add tests, update documentation (fixes #40, fixes #399).

This commit is contained in:
David Anson 2023-10-25 20:05:19 -07:00
parent 460836445c
commit 10f095c4fd
32 changed files with 3149 additions and 333 deletions

View file

@ -748,7 +748,19 @@ function getReferenceLinkImageData(params) {
if (definitions.has(reference)) {
duplicateDefinitions.push([ reference, token.startLine - 1 ]);
} else {
definitions.set(reference, token.startLine - 1);
let destinationString = null;
const parent =
micromark.getTokenParentOfType(token, [ "definition" ]);
if (parent) {
destinationString = micromark.getTokenTextByType(
micromark.filterByPredicate(parent.children),
"definitionDestinationString"
);
}
definitions.set(
reference,
[ token.startLine - 1, destinationString ]
);
}
}
break;

View file

@ -239,11 +239,12 @@ function micromarkParse(
* Filter a list of Micromark tokens by predicate.
*
* @param {Token[]} tokens Micromark tokens.
* @param {AllowedPredicate} allowed Allowed token predicate.
* @param {AllowedPredicate} [allowed] Allowed token predicate.
* @param {TransformPredicate} [transformChildren] Transform predicate.
* @returns {Token[]} Filtered tokens.
*/
function filterByPredicate(tokens, allowed, transformChildren) {
allowed = allowed || (() => true);
const result = [];
const queue = [
{
@ -353,7 +354,7 @@ function getTokenParentOfType(token, types) {
}
/**
* Get the text of a single token from a list of Micromark tokens by type.
* Get the text of the first match from a list of Micromark tokens by type.
*
* @param {Token[]} tokens Micromark tokens.
* @param {string} type Types to match.
@ -361,7 +362,7 @@ function getTokenParentOfType(token, types) {
*/
function getTokenTextByType(tokens, type) {
const filtered = tokens.filter((token) => token.type === type);
return (filtered.length === 1) ? filtered[0].text : null;
return (filtered.length > 0) ? filtered[0].text : null;
}
/**