Refactor to remove codeBlockAndSpanRanges helper.

This commit is contained in:
David Anson 2024-08-15 21:34:27 -07:00
parent c5f4a93cc7
commit 7efc2605b1
7 changed files with 96 additions and 128 deletions

View file

@ -333,6 +333,31 @@ function getDescendantsByType(parent, typePath) {
return tokens;
}
// eslint-disable-next-line jsdoc/valid-types
/** @typedef {readonly string[]} ReadonlyStringArray */
/**
* Gets the line/column/length exclusions for a Micromark token.
*
* @param {ReadonlyStringArray} lines File/string lines.
* @param {Token} token Micromark token.
* @returns {number[][]} Exclusions (line number, start column, length).
*/
function getExclusionsForToken(lines, token) {
const exclusions = [];
const { endColumn, endLine, startColumn, startLine } = token;
for (let lineNumber = startLine; lineNumber <= endLine; lineNumber++) {
const start = (lineNumber === startLine) ? startColumn : 1;
const end = (lineNumber === endLine) ? endColumn : lines[lineNumber - 1].length;
exclusions.push([
lineNumber,
start,
end - start + 1
]);
}
return exclusions;
}
/**
* Gets the heading level of a Micromark heading tokan.
*
@ -503,6 +528,7 @@ module.exports = {
filterByPredicate,
filterByTypes,
getDescendantsByType,
getExclusionsForToken,
getHeadingLevel,
getHeadingStyle,
getHeadingText,