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

@ -25,6 +25,8 @@ jobs:
run: npm install --no-package-lock run: npm install --no-package-lock
- name: Clone Test Repos - name: Clone Test Repos
run: npm run clone-test-repos run: npm run clone-test-repos
- name: Build markdownlint-micromark
run: npm run build-micromark
- name: Lint Test Repos - name: Lint Test Repos
run: npm run lint-test-repos run: npm run lint-test-repos
- name: Run Extra Tests - name: Run Extra Tests

2
.gitignore vendored
View file

@ -1,6 +1,8 @@
coverage coverage
demo/markdown-it.min.js demo/markdown-it.min.js
demo/markdownlint-browser.min.js demo/markdownlint-browser.min.js
micromark/micromark.cjs
micromark/micromark.dev.cjs
node_modules node_modules
!test/node_modules !test/node_modules
npm-debug.log npm-debug.log

File diff suppressed because it is too large Load diff

View file

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

View file

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

View file

@ -25,7 +25,7 @@
"build-config": "npm run build-config-schema && npm run build-config-example", "build-config": "npm run build-config-schema && npm run build-config-example",
"build-config-example": "node schema/build-config-example.js", "build-config-example": "node schema/build-config-example.js",
"build-config-schema": "node schema/build-config-schema.js", "build-config-schema": "node schema/build-config-schema.js",
"build-declaration": "tsc --allowJs --declaration --emitDeclarationOnly --module commonjs --resolveJsonModule --target es2015 lib/markdownlint.js && node scripts delete 'lib/{c,md,r}*.d.ts' 'helpers/*.d.ts'", "build-declaration": "tsc --allowJs --declaration --emitDeclarationOnly --module commonjs --resolveJsonModule --target es2015 lib/markdownlint.js && node scripts delete 'lib/{c,md,mi,r}*.d.{cts,ts}' 'micromark/*.d.cts' 'helpers/*.d.ts'",
"build-demo": "node scripts copy node_modules/markdown-it/dist/markdown-it.min.js demo/markdown-it.min.js && cd demo && webpack --no-stats", "build-demo": "node scripts copy node_modules/markdown-it/dist/markdown-it.min.js demo/markdown-it.min.js && cd demo && webpack --no-stats",
"build-docs": "node doc-build/build-rules.mjs", "build-docs": "node doc-build/build-rules.mjs",
"build-example": "npm install --no-save --ignore-scripts grunt grunt-cli gulp through2", "build-example": "npm install --no-save --ignore-scripts grunt grunt-cli gulp through2",

View file

@ -1223,13 +1223,7 @@ test("customRulesParamsAreFrozen", (t) => {
} }
} }
], ],
"files": [ "files": [ "README.md" ]
"CHANGELOG.md",
"CONTRIBUTING.md",
"README.md",
"doc/CustomRules.md",
"doc/Rules.md"
]
}; };
return markdownlint.promises.markdownlint(options).then(() => null); return markdownlint.promises.markdownlint(options).then(() => null);
}); });