mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Refactor to remove micromark helper getSiblingTokens for simpler code and better performance.
This commit is contained in:
parent
4436d84b55
commit
996d88a9b4
4 changed files with 88 additions and 140 deletions
48
lib/md028.js
48
lib/md028.js
|
|
@ -3,36 +3,36 @@
|
|||
"use strict";
|
||||
|
||||
const { addError } = require("../helpers");
|
||||
const { getSiblingTokens } = require("../helpers/micromark.cjs");
|
||||
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||
|
||||
const ignoreTypes = new Set([ "lineEnding", "listItemIndent", "linePrefix" ]);
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD028", "no-blanks-blockquote" ],
|
||||
"description": "Blank line inside blockquote",
|
||||
"tags": [ "blockquote", "whitespace" ],
|
||||
"function": function MD028(params, onError) {
|
||||
for (const siblings of getSiblingTokens(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;
|
||||
const { tokens } = params.parsers.micromark;
|
||||
for (const token of filterByTypes(tokens, [ "blockQuote" ])) {
|
||||
const errorLineNumbers = [];
|
||||
const siblings = token.parent?.children || tokens;
|
||||
for (let i = siblings.indexOf(token) + 1; i < siblings.length; i++) {
|
||||
const sibling = siblings[i];
|
||||
const { startLine, type } = sibling;
|
||||
if (type === "lineEndingBlank") {
|
||||
// Possible blank between blockquotes
|
||||
errorLineNumbers.push(startLine);
|
||||
} else if (ignoreTypes.has(type)) {
|
||||
// Ignore invisible formatting
|
||||
} else if (type === "blockQuote") {
|
||||
// Blockquote followed by blockquote
|
||||
for (const lineNumber of errorLineNumbers) {
|
||||
addError(onError, lineNumber);
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
// Blockquote not followed by blockquote
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue