mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01: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.
|
* Get the text of the first match from a list of Micromark tokens by type.
|
||||||
*
|
*
|
||||||
* @param {Token[]} tokens Micromark tokens.
|
* @param {Token[]} tokens Micromark tokens.
|
||||||
* @param {string} type Types to match.
|
* @param {string} type Type to match.
|
||||||
* @returns {string | null} Text of token.
|
* @returns {string | null} Text of token.
|
||||||
*/
|
*/
|
||||||
function getTokenTextByType(tokens, type) {
|
function getTokenTextByType(tokens, type) {
|
||||||
|
|
@ -5989,6 +5989,7 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
const { addErrorDetailIf, fencedCodeBlockStyleFor } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
const { addErrorDetailIf, fencedCodeBlockStyleFor } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||||
|
const { filterByTypes, tokenIfType } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD048", "code-fence-style" ],
|
"names": [ "MD048", "code-fence-style" ],
|
||||||
|
|
@ -5997,22 +5998,26 @@ module.exports = {
|
||||||
"function": function MD048(params, onError) {
|
"function": function MD048(params, onError) {
|
||||||
const style = String(params.config.style || "consistent");
|
const style = String(params.config.style || "consistent");
|
||||||
let expectedStyle = style;
|
let expectedStyle = style;
|
||||||
const fenceTokens = params.parsers.markdownit.tokens.filter(
|
const codeFenceds = filterByTypes(params.parsers.micromark.tokens, [ "codeFenced" ]);
|
||||||
(token) => token.type === "fence"
|
for (const codeFenced of codeFenceds) {
|
||||||
);
|
const codeFencedFence = tokenIfType(codeFenced.children[0], "codeFencedFence");
|
||||||
for (const fenceToken of fenceTokens) {
|
if (codeFencedFence) {
|
||||||
const { lineNumber, markup } = fenceToken;
|
const codeFencedFenceSequence = tokenIfType(codeFencedFence.children[0], "codeFencedFenceSequence");
|
||||||
|
if (codeFencedFenceSequence) {
|
||||||
|
const { startLine, text } = codeFencedFenceSequence;
|
||||||
if (expectedStyle === "consistent") {
|
if (expectedStyle === "consistent") {
|
||||||
expectedStyle = fencedCodeBlockStyleFor(markup);
|
expectedStyle = fencedCodeBlockStyleFor(text);
|
||||||
}
|
}
|
||||||
addErrorDetailIf(
|
addErrorDetailIf(
|
||||||
onError,
|
onError,
|
||||||
lineNumber,
|
startLine,
|
||||||
expectedStyle,
|
expectedStyle,
|
||||||
fencedCodeBlockStyleFor(markup)
|
fencedCodeBlockStyleFor(text)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -380,7 +380,7 @@ function getTokenParentOfType(token, types) {
|
||||||
* Get the text of the first match from a list of Micromark tokens by type.
|
* Get the text of the first match from a list of Micromark tokens by type.
|
||||||
*
|
*
|
||||||
* @param {Token[]} tokens Micromark tokens.
|
* @param {Token[]} tokens Micromark tokens.
|
||||||
* @param {string} type Types to match.
|
* @param {string} type Type to match.
|
||||||
* @returns {string | null} Text of token.
|
* @returns {string | null} Text of token.
|
||||||
*/
|
*/
|
||||||
function getTokenTextByType(tokens, type) {
|
function getTokenTextByType(tokens, type) {
|
||||||
|
|
|
||||||
21
lib/md048.js
21
lib/md048.js
|
|
@ -3,6 +3,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorDetailIf, fencedCodeBlockStyleFor } = require("../helpers");
|
const { addErrorDetailIf, fencedCodeBlockStyleFor } = require("../helpers");
|
||||||
|
const { filterByTypes, tokenIfType } = require("../helpers/micromark.cjs");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD048", "code-fence-style" ],
|
"names": [ "MD048", "code-fence-style" ],
|
||||||
|
|
@ -11,20 +12,24 @@ module.exports = {
|
||||||
"function": function MD048(params, onError) {
|
"function": function MD048(params, onError) {
|
||||||
const style = String(params.config.style || "consistent");
|
const style = String(params.config.style || "consistent");
|
||||||
let expectedStyle = style;
|
let expectedStyle = style;
|
||||||
const fenceTokens = params.parsers.markdownit.tokens.filter(
|
const codeFenceds = filterByTypes(params.parsers.micromark.tokens, [ "codeFenced" ]);
|
||||||
(token) => token.type === "fence"
|
for (const codeFenced of codeFenceds) {
|
||||||
);
|
const codeFencedFence = tokenIfType(codeFenced.children[0], "codeFencedFence");
|
||||||
for (const fenceToken of fenceTokens) {
|
if (codeFencedFence) {
|
||||||
const { lineNumber, markup } = fenceToken;
|
const codeFencedFenceSequence = tokenIfType(codeFencedFence.children[0], "codeFencedFenceSequence");
|
||||||
|
if (codeFencedFenceSequence) {
|
||||||
|
const { startLine, text } = codeFencedFenceSequence;
|
||||||
if (expectedStyle === "consistent") {
|
if (expectedStyle === "consistent") {
|
||||||
expectedStyle = fencedCodeBlockStyleFor(markup);
|
expectedStyle = fencedCodeBlockStyleFor(text);
|
||||||
}
|
}
|
||||||
addErrorDetailIf(
|
addErrorDetailIf(
|
||||||
onError,
|
onError,
|
||||||
lineNumber,
|
startLine,
|
||||||
expectedStyle,
|
expectedStyle,
|
||||||
fencedCodeBlockStyleFor(markup)
|
fencedCodeBlockStyleFor(text)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue