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
|
|
@ -1137,6 +1137,7 @@ var _require = __webpack_require__(/*! markdownlint-micromark */ "markdownlint-m
|
|||
preprocess = _require.preprocess;
|
||||
var _require2 = __webpack_require__(/*! ./shared.js */ "../helpers/shared.js"),
|
||||
newLineRe = _require2.newLineRe;
|
||||
var flatTokensSymbol = Symbol("flat-tokens");
|
||||
|
||||
/**
|
||||
* Markdown token.
|
||||
|
|
@ -1197,6 +1198,7 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
|
|||
|
||||
// Create Token objects
|
||||
var document = [];
|
||||
var flatTokens = [];
|
||||
var current = {
|
||||
"children": document
|
||||
};
|
||||
|
|
@ -1247,6 +1249,7 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
|
|||
current.htmlFlowChildren = micromarkParseWithOffset(reparseMarkdown, reparseOptions, referencesDefined, current.startLine - 1);
|
||||
}
|
||||
previous.children.push(current);
|
||||
flatTokens.push(current);
|
||||
} else if (kind === "exit") {
|
||||
Object.freeze(current.children);
|
||||
Object.freeze(current);
|
||||
|
|
@ -1261,6 +1264,9 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
|
|||
} finally {
|
||||
_iterator.f();
|
||||
}
|
||||
Object.defineProperty(document, flatTokensSymbol, {
|
||||
"value": flatTokens
|
||||
});
|
||||
Object.freeze(document);
|
||||
return document;
|
||||
}
|
||||
|
|
@ -1311,9 +1317,14 @@ function filterByPredicate(tokens, allowed, transformChildren) {
|
|||
* @returns {Token[]} Filtered tokens.
|
||||
*/
|
||||
function filterByTypes(tokens, allowed) {
|
||||
return filterByPredicate(tokens, function (token) {
|
||||
var predicate = function predicate(token) {
|
||||
return allowed.includes(token.type);
|
||||
});
|
||||
};
|
||||
var flatTokens = tokens[flatTokensSymbol];
|
||||
if (flatTokens) {
|
||||
return flatTokens.filter(predicate);
|
||||
}
|
||||
return filterByPredicate(tokens, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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