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 { addError, addErrorContext } = require("../helpers");
const { getDescendantsByType, getTokenTextByType } = require("../helpers/micromark.cjs");
const { getDescendantsByType } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache");
// eslint-disable-next-line jsdoc/valid-types
@ -20,14 +20,14 @@ module.exports = {
const fencedCodes = filterByTypesCached([ "codeFenced" ]);
for (const fencedCode of fencedCodes) {
const openingFence = getDescendantsByType(fencedCode, [ "codeFencedFence" ])[0];
const { children, startLine, text } = openingFence;
const info = getTokenTextByType(children, "codeFencedFenceInfo");
const { startLine, text } = openingFence;
const info = getDescendantsByType(openingFence, [ "codeFencedFenceInfo" ])[0]?.text;
if (!info) {
addErrorContext(onError, startLine, text);
} else if ((allowed.length > 0) && !allowed.includes(info)) {
addError(onError, startLine, `"${info}" is not allowed`);
}
if (languageOnly && getTokenTextByType(children, "codeFencedFenceMeta")) {
if (languageOnly && getDescendantsByType(openingFence, [ "codeFencedFenceMeta" ]).length > 0) {
addError(onError, startLine, `Info string contains more than language: "${text}"`);
}
}