Reimplement MD046/code-block-style using micromark tokens.

This commit is contained in:
David Anson 2024-02-19 20:30:35 -08:00
parent 4a7af239ce
commit 807fee01b1
2 changed files with 14 additions and 14 deletions

View file

@ -5906,10 +5906,11 @@ module.exports = {
const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"); const { addErrorDetailIf } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
const tokenTypeToStyle = { const tokenTypeToStyle = {
"fence": "fenced", "codeFenced": "fenced",
"code_block": "indented" "codeIndented": "indented"
}; };
module.exports = { module.exports = {
@ -5918,17 +5919,16 @@ module.exports = {
"tags": [ "code" ], "tags": [ "code" ],
"function": function MD046(params, onError) { "function": function MD046(params, onError) {
let expectedStyle = String(params.config.style || "consistent"); let expectedStyle = String(params.config.style || "consistent");
const codeBlocksAndFences = params.parsers.markdownit.tokens.filter( const codeBlocksAndFences =
(token) => (token.type === "code_block") || (token.type === "fence") filterByTypes(params.parsers.micromark.tokens, [ "codeFenced", "codeIndented" ]);
);
for (const token of codeBlocksAndFences) { for (const token of codeBlocksAndFences) {
const { lineNumber, type } = token; const { startLine, type } = token;
if (expectedStyle === "consistent") { if (expectedStyle === "consistent") {
expectedStyle = tokenTypeToStyle[type]; expectedStyle = tokenTypeToStyle[type];
} }
addErrorDetailIf( addErrorDetailIf(
onError, onError,
lineNumber, startLine,
expectedStyle, expectedStyle,
tokenTypeToStyle[type]); tokenTypeToStyle[type]);
} }

View file

@ -3,10 +3,11 @@
"use strict"; "use strict";
const { addErrorDetailIf } = require("../helpers"); const { addErrorDetailIf } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark.cjs");
const tokenTypeToStyle = { const tokenTypeToStyle = {
"fence": "fenced", "codeFenced": "fenced",
"code_block": "indented" "codeIndented": "indented"
}; };
module.exports = { module.exports = {
@ -15,17 +16,16 @@ module.exports = {
"tags": [ "code" ], "tags": [ "code" ],
"function": function MD046(params, onError) { "function": function MD046(params, onError) {
let expectedStyle = String(params.config.style || "consistent"); let expectedStyle = String(params.config.style || "consistent");
const codeBlocksAndFences = params.parsers.markdownit.tokens.filter( const codeBlocksAndFences =
(token) => (token.type === "code_block") || (token.type === "fence") filterByTypes(params.parsers.micromark.tokens, [ "codeFenced", "codeIndented" ]);
);
for (const token of codeBlocksAndFences) { for (const token of codeBlocksAndFences) {
const { lineNumber, type } = token; const { startLine, type } = token;
if (expectedStyle === "consistent") { if (expectedStyle === "consistent") {
expectedStyle = tokenTypeToStyle[type]; expectedStyle = tokenTypeToStyle[type];
} }
addErrorDetailIf( addErrorDetailIf(
onError, onError,
lineNumber, startLine,
expectedStyle, expectedStyle,
tokenTypeToStyle[type]); tokenTypeToStyle[type]);
} }