Refactor to remove micromark helper getTokenTextByType, update getDescendantsByType to allow sub-arrays.

This commit is contained in:
David Anson 2024-09-24 22:48:14 -07:00
parent 0f210d5c1a
commit 8cbbed8e79
7 changed files with 44 additions and 94 deletions

View file

@ -3,7 +3,7 @@
"use strict";
const { addErrorContext, nextLinesRe } = require("../helpers");
const { filterByPredicate, getTokenTextByType } = require("../helpers/micromark.cjs");
const { getDescendantsByType } = require("../helpers/micromark.cjs");
const { getReferenceLinkImageData, filterByTypesCached } = require("./cache");
const backslashEscapeRe = /\\([!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~])/g;
@ -45,24 +45,23 @@ module.exports = {
let label = null;
let destination = null;
const {
children, endColumn, endLine, startColumn, startLine, text, type
endColumn, endLine, startColumn, startLine, text, type
} = link;
const image = (type === "image");
let isError = false;
if (type === "autolink") {
// link kind is an autolink
destination = getTokenTextByType(children, "autolinkProtocol");
destination = getDescendantsByType(link, [ "autolinkProtocol" ])[0].text;
label = destination;
isError = !autolink;
} else {
// link type is "image" or "link"
const descendents = filterByPredicate(children);
label = getTokenTextByType(descendents, "labelText");
label = getDescendantsByType(link, [ "label", "labelText" ])[0].text;
destination =
getTokenTextByType(descendents, "resourceDestinationString");
getDescendantsByType(link, [ "resource", "resourceDestination", [ "resourceDestinationLiteral", "resourceDestinationRaw" ], "resourceDestinationString" ])[0]?.text;
if (destination) {
// link kind is an inline link
const title = getTokenTextByType(descendents, "resourceTitleString");
const title = getDescendantsByType(link, [ "resource", "resourceTitle", "resourceTitleString" ])[0]?.text;
isError = !inline || (
!urlInline &&
autolink &&
@ -73,9 +72,9 @@ module.exports = {
);
} else {
// link kind is a full/collapsed/shortcut reference link
const isShortcut = !children.some((t) => t.type === "reference");
const referenceString = getTokenTextByType(descendents, "referenceString");
const isCollapsed = (referenceString === null);
const isShortcut = getDescendantsByType(link, [ "reference" ]).length === 0;
const referenceString = getDescendantsByType(link, [ "reference", "referenceString" ])[0]?.text;
const isCollapsed = (referenceString === undefined);
const definition = definitions.get(referenceString || label);
destination = definition && definition[1];
isError = destination &&