mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Reimplement MD028/no-blanks-blockquote using micromark tokens.
This commit is contained in:
parent
89eef68dbd
commit
f5b5773b50
4 changed files with 94 additions and 30 deletions
|
@ -1473,6 +1473,28 @@ function filterByTypes(tokens, types) {
|
|||
return filterByPredicate(tokens, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all sibling token groups for a list of Micromark tokens.
|
||||
*
|
||||
* @param {Token[]} tokens Micromark tokens.
|
||||
* @returns {Token[][]} Sibling token groups.
|
||||
*/
|
||||
function getSiblingTokens(tokens) {
|
||||
const result = [];
|
||||
const queue = [ tokens ];
|
||||
// eslint-disable-next-line init-declarations
|
||||
let current;
|
||||
while ((current = queue.shift())) {
|
||||
result.push(current);
|
||||
for (const token of current) {
|
||||
if (token.children.length > 0) {
|
||||
queue.push(token.children);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the heading level of a Micromark heading tokan.
|
||||
*
|
||||
|
@ -1597,6 +1619,7 @@ module.exports = {
|
|||
getHeadingLevel,
|
||||
getHtmlTagInfo,
|
||||
getMicromarkEvents,
|
||||
getSiblingLists: getSiblingTokens,
|
||||
getTokenParentOfType,
|
||||
getTokenTextByType,
|
||||
inHtmlFlow,
|
||||
|
@ -4464,7 +4487,6 @@ const { addError, allPunctuationNoQuestion, endOfLineGemojiCodeRe,
|
|||
endOfLineHtmlEntityRe, escapeForRegExp } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||
const { filterByTypes } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD026", "no-trailing-punctuation" ],
|
||||
"description": "Trailing punctuation in heading",
|
||||
|
@ -4589,28 +4611,38 @@ module.exports = {
|
|||
|
||||
|
||||
const { addError } = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js");
|
||||
const { getSiblingLists } = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs");
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD028", "no-blanks-blockquote" ],
|
||||
"description": "Blank line inside blockquote",
|
||||
"tags": [ "blockquote", "whitespace" ],
|
||||
"function": function MD028(params, onError) {
|
||||
let prevToken = {};
|
||||
let prevLineNumber = null;
|
||||
for (const token of params.parsers.markdownit.tokens) {
|
||||
if ((token.type === "blockquote_open") &&
|
||||
(prevToken.type === "blockquote_close")) {
|
||||
for (
|
||||
let lineNumber = prevLineNumber;
|
||||
lineNumber < token.lineNumber;
|
||||
lineNumber++) {
|
||||
addError(onError, lineNumber);
|
||||
for (const siblings of getSiblingLists(params.parsers.micromark.tokens)) {
|
||||
let errorLineNumbers = null;
|
||||
for (const sibling of siblings) {
|
||||
switch (sibling.type) {
|
||||
case "blockQuote":
|
||||
for (const lineNumber of (errorLineNumbers || [])) {
|
||||
addError(onError, lineNumber);
|
||||
}
|
||||
errorLineNumbers = [];
|
||||
break;
|
||||
case "lineEnding":
|
||||
case "linePrefix":
|
||||
case "listItemIndent":
|
||||
// Ignore
|
||||
break;
|
||||
case "lineEndingBlank":
|
||||
if (errorLineNumbers) {
|
||||
errorLineNumbers.push(sibling.startLine);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
errorLineNumbers = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
prevToken = token;
|
||||
if (token.type === "blockquote_open") {
|
||||
prevLineNumber = token.map[1] + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -295,6 +295,28 @@ function filterByTypes(tokens, types) {
|
|||
return filterByPredicate(tokens, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all sibling token groups for a list of Micromark tokens.
|
||||
*
|
||||
* @param {Token[]} tokens Micromark tokens.
|
||||
* @returns {Token[][]} Sibling token groups.
|
||||
*/
|
||||
function getSiblingTokens(tokens) {
|
||||
const result = [];
|
||||
const queue = [ tokens ];
|
||||
// eslint-disable-next-line init-declarations
|
||||
let current;
|
||||
while ((current = queue.shift())) {
|
||||
result.push(current);
|
||||
for (const token of current) {
|
||||
if (token.children.length > 0) {
|
||||
queue.push(token.children);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the heading level of a Micromark heading tokan.
|
||||
*
|
||||
|
@ -419,6 +441,7 @@ module.exports = {
|
|||
getHeadingLevel,
|
||||
getHtmlTagInfo,
|
||||
getMicromarkEvents,
|
||||
getSiblingLists: getSiblingTokens,
|
||||
getTokenParentOfType,
|
||||
getTokenTextByType,
|
||||
inHtmlFlow,
|
||||
|
|
|
@ -6,7 +6,6 @@ const { addError, allPunctuationNoQuestion, endOfLineGemojiCodeRe,
|
|||
endOfLineHtmlEntityRe, escapeForRegExp } = require("../helpers");
|
||||
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD026", "no-trailing-punctuation" ],
|
||||
"description": "Trailing punctuation in heading",
|
||||
|
|
38
lib/md028.js
38
lib/md028.js
|
@ -3,28 +3,38 @@
|
|||
"use strict";
|
||||
|
||||
const { addError } = require("../helpers");
|
||||
const { getSiblingLists } = require("../helpers/micromark.cjs");
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD028", "no-blanks-blockquote" ],
|
||||
"description": "Blank line inside blockquote",
|
||||
"tags": [ "blockquote", "whitespace" ],
|
||||
"function": function MD028(params, onError) {
|
||||
let prevToken = {};
|
||||
let prevLineNumber = null;
|
||||
for (const token of params.parsers.markdownit.tokens) {
|
||||
if ((token.type === "blockquote_open") &&
|
||||
(prevToken.type === "blockquote_close")) {
|
||||
for (
|
||||
let lineNumber = prevLineNumber;
|
||||
lineNumber < token.lineNumber;
|
||||
lineNumber++) {
|
||||
addError(onError, lineNumber);
|
||||
for (const siblings of getSiblingLists(params.parsers.micromark.tokens)) {
|
||||
let errorLineNumbers = null;
|
||||
for (const sibling of siblings) {
|
||||
switch (sibling.type) {
|
||||
case "blockQuote":
|
||||
for (const lineNumber of (errorLineNumbers || [])) {
|
||||
addError(onError, lineNumber);
|
||||
}
|
||||
errorLineNumbers = [];
|
||||
break;
|
||||
case "lineEnding":
|
||||
case "linePrefix":
|
||||
case "listItemIndent":
|
||||
// Ignore
|
||||
break;
|
||||
case "lineEndingBlank":
|
||||
if (errorLineNumbers) {
|
||||
errorLineNumbers.push(sibling.startLine);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
errorLineNumbers = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
prevToken = token;
|
||||
if (token.type === "blockquote_open") {
|
||||
prevLineNumber = token.map[1] + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue