mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Update micromark parse to store flattened tokens on the returned tree via Symbol, use flattened tokens in filterByTypes for a ~14% elapsed time reduction.
This commit is contained in:
parent
87fda39df8
commit
73e7271188
2 changed files with 24 additions and 6 deletions
|
|
@ -10,6 +10,8 @@ const {
|
|||
} = require("markdownlint-micromark");
|
||||
const { newLineRe } = require("./shared.js");
|
||||
|
||||
const flatTokensSymbol = Symbol("flat-tokens");
|
||||
|
||||
/**
|
||||
* Markdown token.
|
||||
*
|
||||
|
|
@ -82,6 +84,7 @@ function micromarkParseWithOffset(
|
|||
|
||||
// Create Token objects
|
||||
const document = [];
|
||||
const flatTokens = [];
|
||||
let current = {
|
||||
"children": document
|
||||
};
|
||||
|
|
@ -131,6 +134,7 @@ function micromarkParseWithOffset(
|
|||
);
|
||||
}
|
||||
previous.children.push(current);
|
||||
flatTokens.push(current);
|
||||
} else if (kind === "exit") {
|
||||
Object.freeze(current.children);
|
||||
Object.freeze(current);
|
||||
|
|
@ -140,6 +144,7 @@ function micromarkParseWithOffset(
|
|||
}
|
||||
|
||||
// Return document
|
||||
Object.defineProperty(document, flatTokensSymbol, { "value": flatTokens });
|
||||
Object.freeze(document);
|
||||
return document;
|
||||
}
|
||||
|
|
@ -198,10 +203,12 @@ function filterByPredicate(tokens, allowed, transformChildren) {
|
|||
* @returns {Token[]} Filtered tokens.
|
||||
*/
|
||||
function filterByTypes(tokens, allowed) {
|
||||
return filterByPredicate(
|
||||
tokens,
|
||||
(token) => allowed.includes(token.type)
|
||||
);
|
||||
const predicate = (token) => allowed.includes(token.type);
|
||||
const flatTokens = tokens[flatTokensSymbol];
|
||||
if (flatTokens) {
|
||||
return flatTokens.filter(predicate);
|
||||
}
|
||||
return filterByPredicate(tokens, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue