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

@ -3,6 +3,7 @@
"use strict";
const { addError } = require("../helpers");
const { filterByPredicate } = require("../helpers/micromark.cjs");
module.exports = {
"names": [ "MD037", "no-space-in-emphasis" ],
@ -16,16 +17,11 @@ module.exports = {
for (const marker of [ "_", "__", "___", "*", "**", "***" ]) {
emphasisTokensByMarker.set(marker, []);
}
const pending = [ ...parsers.micromark.tokens ];
let token = null;
while ((token = pending.shift())) {
// Use reparsed children of htmlFlow tokens
if (token.type === "htmlFlow") {
pending.unshift(...token.htmlFlowChildren);
continue;
}
pending.push(...token.children);
const tokens = filterByPredicate(
parsers.micromark.tokens,
(token) => token.children.some((child) => child.type === "data")
);
for (const token of tokens) {
// Build lists of bare tokens for each emphasis marker type
for (const emphasisTokens of emphasisTokensByMarker.values()) {