mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +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;
|
preprocess = _require.preprocess;
|
||||||
var _require2 = __webpack_require__(/*! ./shared.js */ "../helpers/shared.js"),
|
var _require2 = __webpack_require__(/*! ./shared.js */ "../helpers/shared.js"),
|
||||||
newLineRe = _require2.newLineRe;
|
newLineRe = _require2.newLineRe;
|
||||||
|
var flatTokensSymbol = Symbol("flat-tokens");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Markdown token.
|
* Markdown token.
|
||||||
|
|
@ -1197,6 +1198,7 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
|
||||||
|
|
||||||
// Create Token objects
|
// Create Token objects
|
||||||
var document = [];
|
var document = [];
|
||||||
|
var flatTokens = [];
|
||||||
var current = {
|
var current = {
|
||||||
"children": document
|
"children": document
|
||||||
};
|
};
|
||||||
|
|
@ -1247,6 +1249,7 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
|
||||||
current.htmlFlowChildren = micromarkParseWithOffset(reparseMarkdown, reparseOptions, referencesDefined, current.startLine - 1);
|
current.htmlFlowChildren = micromarkParseWithOffset(reparseMarkdown, reparseOptions, referencesDefined, current.startLine - 1);
|
||||||
}
|
}
|
||||||
previous.children.push(current);
|
previous.children.push(current);
|
||||||
|
flatTokens.push(current);
|
||||||
} else if (kind === "exit") {
|
} else if (kind === "exit") {
|
||||||
Object.freeze(current.children);
|
Object.freeze(current.children);
|
||||||
Object.freeze(current);
|
Object.freeze(current);
|
||||||
|
|
@ -1261,6 +1264,9 @@ function micromarkParseWithOffset(markdown, micromarkOptions, referencesDefined,
|
||||||
} finally {
|
} finally {
|
||||||
_iterator.f();
|
_iterator.f();
|
||||||
}
|
}
|
||||||
|
Object.defineProperty(document, flatTokensSymbol, {
|
||||||
|
"value": flatTokens
|
||||||
|
});
|
||||||
Object.freeze(document);
|
Object.freeze(document);
|
||||||
return document;
|
return document;
|
||||||
}
|
}
|
||||||
|
|
@ -1311,9 +1317,14 @@ function filterByPredicate(tokens, allowed, transformChildren) {
|
||||||
* @returns {Token[]} Filtered tokens.
|
* @returns {Token[]} Filtered tokens.
|
||||||
*/
|
*/
|
||||||
function filterByTypes(tokens, allowed) {
|
function filterByTypes(tokens, allowed) {
|
||||||
return filterByPredicate(tokens, function (token) {
|
var predicate = function predicate(token) {
|
||||||
return allowed.includes(token.type);
|
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");
|
} = require("markdownlint-micromark");
|
||||||
const { newLineRe } = require("./shared.js");
|
const { newLineRe } = require("./shared.js");
|
||||||
|
|
||||||
|
const flatTokensSymbol = Symbol("flat-tokens");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Markdown token.
|
* Markdown token.
|
||||||
*
|
*
|
||||||
|
|
@ -82,6 +84,7 @@ function micromarkParseWithOffset(
|
||||||
|
|
||||||
// Create Token objects
|
// Create Token objects
|
||||||
const document = [];
|
const document = [];
|
||||||
|
const flatTokens = [];
|
||||||
let current = {
|
let current = {
|
||||||
"children": document
|
"children": document
|
||||||
};
|
};
|
||||||
|
|
@ -131,6 +134,7 @@ function micromarkParseWithOffset(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
previous.children.push(current);
|
previous.children.push(current);
|
||||||
|
flatTokens.push(current);
|
||||||
} else if (kind === "exit") {
|
} else if (kind === "exit") {
|
||||||
Object.freeze(current.children);
|
Object.freeze(current.children);
|
||||||
Object.freeze(current);
|
Object.freeze(current);
|
||||||
|
|
@ -140,6 +144,7 @@ function micromarkParseWithOffset(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return document
|
// Return document
|
||||||
|
Object.defineProperty(document, flatTokensSymbol, { "value": flatTokens });
|
||||||
Object.freeze(document);
|
Object.freeze(document);
|
||||||
return document;
|
return document;
|
||||||
}
|
}
|
||||||
|
|
@ -198,10 +203,12 @@ function filterByPredicate(tokens, allowed, transformChildren) {
|
||||||
* @returns {Token[]} Filtered tokens.
|
* @returns {Token[]} Filtered tokens.
|
||||||
*/
|
*/
|
||||||
function filterByTypes(tokens, allowed) {
|
function filterByTypes(tokens, allowed) {
|
||||||
return filterByPredicate(
|
const predicate = (token) => allowed.includes(token.type);
|
||||||
tokens,
|
const flatTokens = tokens[flatTokensSymbol];
|
||||||
(token) => allowed.includes(token.type)
|
if (flatTokens) {
|
||||||
);
|
return flatTokens.filter(predicate);
|
||||||
|
}
|
||||||
|
return filterByPredicate(tokens, predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue