Refactor various code to do shallow/constrained search instead of deep search for better performance, make cache key for filterByTypesCached unique.

This commit is contained in:
David Anson 2024-10-09 22:42:36 -07:00
parent 2fa7730a6b
commit d22c1f19ef
12 changed files with 171 additions and 220 deletions

View file

@ -176,12 +176,12 @@ function getDescendantsByType(parent, typePath) {
* @returns {number} Heading level.
*/
function getHeadingLevel(heading) {
const headingSequence = filterByTypes(
heading.children,
[ "atxHeadingSequence", "setextHeadingLineSequence" ]
);
let level = 1;
const { text } = headingSequence[0];
const headingSequence = heading.children.find(
(child) => [ "atxHeadingSequence", "setextHeadingLine" ].includes(child.type)
);
// @ts-ignore
const { text } = headingSequence;
if (text[0] === "#") {
level = Math.min(text.length, 6);
} else if (text[0] === "-") {
@ -200,9 +200,8 @@ function getHeadingStyle(heading) {
if (heading.type === "setextHeading") {
return "setext";
}
const atxHeadingSequenceLength = filterByTypes(
heading.children,
[ "atxHeadingSequence" ]
const atxHeadingSequenceLength = heading.children.filter(
(child) => child.type === "atxHeadingSequence"
).length;
if (atxHeadingSequenceLength === 1) {
return "atx";