Add content parsing via micromark, publish frozen micromark tokens alongside markdown-it tokens, remove assert from micromark wrapper.

This commit is contained in:
David Anson 2023-01-15 21:41:22 -08:00
parent ed854f7092
commit 1461ad6272
7 changed files with 3955 additions and 32 deletions

View file

@ -4,7 +4,8 @@
const path = require("node:path");
const { promisify } = require("node:util");
const markdownIt = require("markdown-it");
const markdownit = require("markdown-it");
const micromark = require("./micromark.cjs");
const { deprecatedRuleNames } = require("./constants");
const rules = require("./rules");
const helpers = require("../helpers");
@ -549,16 +550,23 @@ function lintContent(
configParsers,
mapAliasToRuleNames(ruleList)
);
// Hide the content of HTML comments from rules, etc.
// Parse content into parser tokens
const markdownitTokens = md.parse(content, {});
const micromarkTokens = micromark.parse(content);
// Hide the content of HTML comments from rules
content = helpers.clearHtmlCommentText(content);
// Parse content into tokens and lines
const tokens = md.parse(content, {});
// Parse content into lines and update markdown-it tokens
const lines = content.split(helpers.newLineRe);
annotateAndFreezeTokens(tokens, lines);
annotateAndFreezeTokens(markdownitTokens, lines);
// Create (frozen) parameters for rules
const parsers = Object.freeze({
"markdownit": markdownitTokens,
"micromark": micromarkTokens
});
const paramsBase = {
name,
tokens,
parsers,
"tokens": markdownitTokens,
"lines": Object.freeze(lines),
"frontMatterLines": Object.freeze(frontMatterLines)
};
@ -868,7 +876,7 @@ function lintInput(options, synchronous, callback) {
const noInlineConfig = !!options.noInlineConfig;
const resultVersion = (options.resultVersion === undefined) ?
3 : options.resultVersion;
const md = markdownIt({ "html": true });
const md = markdownit({ "html": true });
const markdownItPlugins = options.markdownItPlugins || [];
for (const plugin of markdownItPlugins) {
// @ts-ignore

View file

@ -4,7 +4,6 @@
/* eslint-disable n/no-unpublished-require */
const assert = require("node:assert/strict");
// @ts-ignore
const { parse, postprocess, preprocess } = require("../micromark/micromark.cjs");
@ -22,10 +21,10 @@ const { parse, postprocess, preprocess } = require("../micromark/micromark.cjs")
*/
/**
* Parses a Markdown document and returns tokens.
* Parses a Markdown document and returns (frozen) tokens.
*
* @param {string} markdown Markdown document.
* @returns {Token[]} Markdown tokens.
* @returns {Token[]} Markdown tokens (frozen).
*/
function micromarkParse(markdown) {
@ -68,19 +67,15 @@ function micromarkParse(markdown) {
};
previous.tokens.push(current);
} else if (kind === "exit") {
assert.equal(type, current.type);
assert.equal(startLine, current.startLine);
assert.equal(startColumn, current.startColumn);
assert.equal(endLine, current.endLine);
assert.equal(endColumn, current.endColumn);
assert.equal(text, current.text);
Object.freeze(current.tokens);
Object.freeze(current);
// @ts-ignore
current = history.pop();
assert.ok(current, "Empty history");
}
}
// Return document
Object.freeze(document);
return document;
}