mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Reimplement MD048/code-fence-style using micromark tokens.
This commit is contained in:
parent
807fee01b1
commit
f0d691b1ed
3 changed files with 38 additions and 28 deletions
|
@ -1558,7 +1558,7 @@ function getTokenParentOfType(token, types) {
|
|||
* Get the text of the first match from a list of Micromark tokens by type.
|
||||
*
|
||||
* @param {Token[]} tokens Micromark tokens.
|
||||
* @param {string} type Types to match.
|
||||
* @param {string} type Type to match.
|
||||
* @returns {string | null} Text of token.
|
||||
*/
|
||||
function getTokenTextByType(tokens, type) {
|
||||
|
@ -5989,6 +5989,7 @@ module.exports = {
|
|||
|
||||
|
||||
const { addErrorDetailIf, fencedCodeBlockStyleFor } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||
const { filterByTypes, tokenIfType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD048", "code-fence-style" ],
|
||||
|
@ -5997,20 +5998,24 @@ module.exports = {
|
|||
"function": function MD048(params, onError) {
|
||||
const style = String(params.config.style || "consistent");
|
||||
let expectedStyle = style;
|
||||
const fenceTokens = params.parsers.markdownit.tokens.filter(
|
||||
(token) => token.type === "fence"
|
||||
);
|
||||
for (const fenceToken of fenceTokens) {
|
||||
const { lineNumber, markup } = fenceToken;
|
||||
if (expectedStyle === "consistent") {
|
||||
expectedStyle = fencedCodeBlockStyleFor(markup);
|
||||
const codeFenceds = filterByTypes(params.parsers.micromark.tokens, [ "codeFenced" ]);
|
||||
for (const codeFenced of codeFenceds) {
|
||||
const codeFencedFence = tokenIfType(codeFenced.children[0], "codeFencedFence");
|
||||
if (codeFencedFence) {
|
||||
const codeFencedFenceSequence = tokenIfType(codeFencedFence.children[0], "codeFencedFenceSequence");
|
||||
if (codeFencedFenceSequence) {
|
||||
const { startLine, text } = codeFencedFenceSequence;
|
||||
if (expectedStyle === "consistent") {
|
||||
expectedStyle = fencedCodeBlockStyleFor(text);
|
||||
}
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
startLine,
|
||||
expectedStyle,
|
||||
fencedCodeBlockStyleFor(text)
|
||||
);
|
||||
}
|
||||
}
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
lineNumber,
|
||||
expectedStyle,
|
||||
fencedCodeBlockStyleFor(markup)
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -380,7 +380,7 @@ function getTokenParentOfType(token, types) {
|
|||
* Get the text of the first match from a list of Micromark tokens by type.
|
||||
*
|
||||
* @param {Token[]} tokens Micromark tokens.
|
||||
* @param {string} type Types to match.
|
||||
* @param {string} type Type to match.
|
||||
* @returns {string | null} Text of token.
|
||||
*/
|
||||
function getTokenTextByType(tokens, type) {
|
||||
|
|
31
lib/md048.js
31
lib/md048.js
|
@ -3,6 +3,7 @@
|
|||
"use strict";
|
||||
|
||||
const { addErrorDetailIf, fencedCodeBlockStyleFor } = require("../helpers");
|
||||
const { filterByTypes, tokenIfType } = require("../helpers/micromark.cjs");
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD048", "code-fence-style" ],
|
||||
|
@ -11,20 +12,24 @@ module.exports = {
|
|||
"function": function MD048(params, onError) {
|
||||
const style = String(params.config.style || "consistent");
|
||||
let expectedStyle = style;
|
||||
const fenceTokens = params.parsers.markdownit.tokens.filter(
|
||||
(token) => token.type === "fence"
|
||||
);
|
||||
for (const fenceToken of fenceTokens) {
|
||||
const { lineNumber, markup } = fenceToken;
|
||||
if (expectedStyle === "consistent") {
|
||||
expectedStyle = fencedCodeBlockStyleFor(markup);
|
||||
const codeFenceds = filterByTypes(params.parsers.micromark.tokens, [ "codeFenced" ]);
|
||||
for (const codeFenced of codeFenceds) {
|
||||
const codeFencedFence = tokenIfType(codeFenced.children[0], "codeFencedFence");
|
||||
if (codeFencedFence) {
|
||||
const codeFencedFenceSequence = tokenIfType(codeFencedFence.children[0], "codeFencedFenceSequence");
|
||||
if (codeFencedFenceSequence) {
|
||||
const { startLine, text } = codeFencedFenceSequence;
|
||||
if (expectedStyle === "consistent") {
|
||||
expectedStyle = fencedCodeBlockStyleFor(text);
|
||||
}
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
startLine,
|
||||
expectedStyle,
|
||||
fencedCodeBlockStyleFor(text)
|
||||
);
|
||||
}
|
||||
}
|
||||
addErrorDetailIf(
|
||||
onError,
|
||||
lineNumber,
|
||||
expectedStyle,
|
||||
fencedCodeBlockStyleFor(markup)
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue