Refactor micromark token handling to remove optional Token.htmlFlowChildren property and make related code more efficient for a ~6% elapsed time reduction.

This commit is contained in:
David Anson 2023-09-02 12:07:14 -07:00
parent e282874fe3
commit 24c97a54fb
16 changed files with 274 additions and 283 deletions

View file

@ -29,27 +29,22 @@ module.exports = {
const htmlElements = params.config.html_elements;
const includeHtmlElements =
(htmlElements === undefined) ? true : !!htmlElements;
const scannedTypes = new Set([ "data", "htmlFlowData" ]);
const scannedTypes = new Set([ "data" ]);
if (includeCodeBlocks) {
scannedTypes.add("codeFlowValue");
scannedTypes.add("codeTextData");
}
if (includeHtmlElements) {
scannedTypes.add("htmlFlowData");
scannedTypes.add("htmlTextData");
}
const contentTokens =
filterByPredicate(
params.parsers.micromark.tokens,
(token) => scannedTypes.has(token.type),
(token) => {
let { children } = token;
const { htmlFlowChildren, text, type } = token;
if (!includeHtmlElements && (type === "htmlFlow")) {
children = text.startsWith("<!--") ?
// Remove comment content
[] :
// Examine htmlText content
htmlFlowChildren;
}
return children.filter((t) => !ignoredChildTypes.has(t.type));
}
(token) => (
token.children.filter((t) => !ignoredChildTypes.has(t.type))
)
);
const exclusions = [];
const autoLinked = new Set();