Refactor micromark parse code to stop using micromark's TokenizeContext.sliceSerialize (less well supported) in favor of Token.start/end.offset.

This commit is contained in:
David Anson 2025-02-08 14:43:38 -08:00
parent b23fc96ab2
commit 3cbe1cb6c5
6 changed files with 71 additions and 25 deletions

View file

@ -20,6 +20,17 @@ import { flatTokensSymbol, htmlFlowSymbol, newLineRe } from "../helpers/shared.c
/** @typedef {import("markdownlint").MicromarkToken} MicromarkToken */
/** @typedef {import("./micromark-types.d.mts")} */
/**
* Gets the Markdown text for a Micromark token.
*
* @param {string} markdown Markdown content.
* @param {Token} token Micromark token.
* @returns {string} Token text.
*/
function getText(markdown, token) {
return markdown.slice(token.start.offset, token.end.offset);
}
/**
* Parse options.
*
@ -126,7 +137,7 @@ export function getEvents(
// Create artificial event list and replicate content
const text = eventsToReplicate
.filter((event) => event[0] === "enter")
.map((event) => tokenizeContext.sliceSerialize(event[1]))
.map((event) => getText(markdown, event[1]))
.join("")
.trim();
if ((text.length > 0) && !text.includes("]")) {
@ -221,11 +232,11 @@ function parseInternal(
let lines = null;
let skipHtmlFlowChildren = false;
for (const event of events) {
const [ kind, token, context ] = event;
const [ kind, token ] = event;
const { type, start, end } = token;
const { "column": startColumn, "line": startLine } = start;
const { "column": endColumn, "line": endLine } = end;
const text = context.sliceSerialize(token);
const text = getText(markdown, token);
if ((kind === "enter") && !skipHtmlFlowChildren) {
const previous = current;
history.push(previous);