mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Reimplement MD027/no-multiple-space-blockquote using micromark tokens.
This commit is contained in:
parent
a2997f1595
commit
730ae9a96f
11 changed files with 248 additions and 167 deletions
65
lib/md027.js
65
lib/md027.js
|
|
@ -2,54 +2,35 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { addErrorContext, newLineRe } = require("../helpers");
|
||||
|
||||
const spaceAfterBlockQuoteRe = /^((?:\s*>)+)(\s{2,})\S/;
|
||||
const { addErrorContext } = require("../helpers");
|
||||
const { getSiblingTokens } = require("../helpers/micromark.cjs");
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD027", "no-multiple-space-blockquote" ],
|
||||
"names": ["MD027", "no-multiple-space-blockquote"],
|
||||
"description": "Multiple spaces after blockquote symbol",
|
||||
"tags": [ "blockquote", "whitespace", "indentation" ],
|
||||
"tags": ["blockquote", "whitespace", "indentation"],
|
||||
"function": function MD027(params, onError) {
|
||||
let blockquoteNesting = 0;
|
||||
let listItemNesting = 0;
|
||||
for (const token of params.parsers.markdownit.tokens) {
|
||||
const { content, lineNumber, type } = token;
|
||||
if (type === "blockquote_open") {
|
||||
blockquoteNesting++;
|
||||
} else if (type === "blockquote_close") {
|
||||
blockquoteNesting--;
|
||||
} else if (type === "list_item_open") {
|
||||
listItemNesting++;
|
||||
} else if (type === "list_item_close") {
|
||||
listItemNesting--;
|
||||
} else if ((type === "inline") && blockquoteNesting) {
|
||||
const lineCount = content.split(newLineRe).length;
|
||||
for (let i = 0; i < lineCount; i++) {
|
||||
const line = params.lines[lineNumber + i - 1];
|
||||
const match = line.match(spaceAfterBlockQuoteRe);
|
||||
if (match) {
|
||||
const [
|
||||
fullMatch,
|
||||
{ "length": blockquoteLength },
|
||||
{ "length": spaceLength }
|
||||
] = match;
|
||||
if (!listItemNesting || (fullMatch[fullMatch.length - 1] === ">")) {
|
||||
addErrorContext(
|
||||
onError,
|
||||
lineNumber + i,
|
||||
line,
|
||||
null,
|
||||
null,
|
||||
[ 1, fullMatch.length ],
|
||||
{
|
||||
"editColumn": blockquoteLength + 1,
|
||||
"deleteCount": spaceLength - 1
|
||||
}
|
||||
);
|
||||
for (const siblings of getSiblingTokens(params.parsers.micromark.tokens)) {
|
||||
let previousType = null;
|
||||
for (const token of siblings) {
|
||||
const { type } = token;
|
||||
if ((type === "linePrefix") && (previousType === "blockQuotePrefix")) {
|
||||
const { endColumn, startColumn, startLine, text } = token;
|
||||
const line = params.lines[startLine - 1];
|
||||
addErrorContext(
|
||||
onError,
|
||||
startLine,
|
||||
line,
|
||||
null,
|
||||
null,
|
||||
[ 1, Math.min(endColumn, line.length) ],
|
||||
{
|
||||
"editColumn": startColumn,
|
||||
"deleteCount": text.length
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
previousType = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,14 +3,14 @@
|
|||
"use strict";
|
||||
|
||||
const { addError } = require("../helpers");
|
||||
const { getSiblingLists } = require("../helpers/micromark.cjs");
|
||||
const { getSiblingTokens } = require("../helpers/micromark.cjs");
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD028", "no-blanks-blockquote" ],
|
||||
"description": "Blank line inside blockquote",
|
||||
"tags": [ "blockquote", "whitespace" ],
|
||||
"function": function MD028(params, onError) {
|
||||
for (const siblings of getSiblingLists(params.parsers.micromark.tokens)) {
|
||||
for (const siblings of getSiblingTokens(params.parsers.micromark.tokens)) {
|
||||
let errorLineNumbers = null;
|
||||
for (const sibling of siblings) {
|
||||
switch (sibling.type) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue