Add new rule MD058/blanks-around-tables (fixes #132).

This commit is contained in:
David Anson 2024-06-01 21:32:10 -07:00
parent 5ecdb045a5
commit 26466108e9
27 changed files with 914 additions and 76 deletions

View file

@ -1078,6 +1078,14 @@ export interface Configuration {
* MD056/table-column-count : Table column count : https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md056.md
*/
"table-column-count"?: boolean;
/**
* MD058/blanks-around-tables : Tables should be surrounded by blank lines : https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md058.md
*/
MD058?: boolean;
/**
* MD058/blanks-around-tables : Tables should be surrounded by blank lines : https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md058.md
*/
"blanks-around-tables"?: boolean;
/**
* headings : MD001, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026, MD036, MD041, MD043
*/
@ -1171,7 +1179,7 @@ export interface Configuration {
*/
images?: boolean;
/**
* table : MD055, MD056
* table : MD055, MD056, MD058
*/
table?: boolean;
[k: string]: unknown;

View file

@ -8,7 +8,8 @@ module.exports.fixableRuleNames = [
"MD012", "MD014", "MD018", "MD019", "MD020", "MD021",
"MD022", "MD023", "MD026", "MD027", "MD030", "MD031",
"MD032", "MD034", "MD037", "MD038", "MD039", "MD044",
"MD047", "MD049", "MD050", "MD051", "MD053", "MD054"
"MD047", "MD049", "MD050", "MD051", "MD053", "MD054",
"MD058"
];
module.exports.homepage = "https://github.com/DavidAnson/markdownlint";
module.exports.version = "0.34.0";

View file

@ -40,6 +40,7 @@ module.exports = {
onError,
// @ts-ignore
i + 1,
// @ts-ignore
lineTrim,
null,
null,

View file

@ -31,7 +31,7 @@ module.exports = {
const leftHashLength = leftHash.length;
const rightHashLength = rightHash.length;
const left = !leftSpaceLength;
const right = !rightSpaceLength || rightEscape;
const right = !rightSpaceLength || !!rightEscape;
const rightEscapeReplacement = rightEscape ? `${rightEscape} ` : "";
if (left || right) {
const range = left ?

View file

@ -2,28 +2,12 @@
"use strict";
const { addErrorContext, blockquotePrefixRe, isBlankLine } = require("../helpers");
const { addErrorContextForLine, isBlankLine } = require("../helpers");
const { filterByPredicate, nonContentTokens } = require("../helpers/micromark.cjs");
const isList = (token) => (
(token.type === "listOrdered") || (token.type === "listUnordered")
);
const addBlankLineError = (onError, lines, lineIndex, lineNumber) => {
const line = lines[lineIndex];
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
addErrorContext(
onError,
lineIndex + 1,
line.trim(),
null,
null,
null,
{
lineNumber,
"insertText": `${quotePrefix}\n`
}
);
};
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
@ -53,7 +37,12 @@ module.exports = {
// Look for a blank line above the list
const firstIndex = list.startLine - 1;
if (!isBlankLine(lines[firstIndex - 1])) {
addBlankLineError(onError, lines, firstIndex);
addErrorContextForLine(
onError,
// @ts-ignore
lines,
firstIndex
);
}
// Find the "visual" end of the list
@ -69,7 +58,13 @@ module.exports = {
// Look for a blank line below the list
const lastIndex = endLine - 1;
if (!isBlankLine(lines[lastIndex + 1])) {
addBlankLineError(onError, lines, lastIndex, lastIndex + 2);
addErrorContextForLine(
onError,
// @ts-ignore
lines,
lastIndex,
lastIndex + 2
);
}
}
}

48
lib/md058.js Normal file
View file

@ -0,0 +1,48 @@
// @ts-check
"use strict";
const { addErrorContextForLine, isBlankLine } = require("../helpers");
const { filterByTypes } = require("../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
module.exports = {
"names": [ "MD058", "blanks-around-tables" ],
"description": "Tables should be surrounded by blank lines",
"tags": [ "table" ],
"parser": "micromark",
"function": function MD058(params, onError) {
// eslint-disable-next-line jsdoc/valid-types
/** @type import("../helpers/micromark.cjs").Token[] */
const micromarkTokens =
// @ts-ignore
params.parsers.micromark.tokens;
const { lines } = params;
// For every table...
const tables = filterByTypes(micromarkTokens, [ "table" ]);
for (const table of tables) {
// Look for a blank line above the table
const firstIndex = table.startLine - 1;
if (!isBlankLine(lines[firstIndex - 1])) {
addErrorContextForLine(
onError,
// @ts-ignore
lines,
firstIndex
);
}
// Look for a blank line below the table
const lastIndex = table.endLine - 1;
if (!isBlankLine(lines[lastIndex + 1])) {
addErrorContextForLine(
onError,
// @ts-ignore
lines,
lastIndex,
lastIndex + 2
);
}
}
}
};

View file

@ -55,8 +55,9 @@ const rules = [
require("./md053"),
require("./md054"),
require("./md055"),
require("./md056")
require("./md056"),
// md057: See https://github.com/markdownlint/markdownlint
require("./md058")
];
for (const rule of rules) {
const name = rule.names[0].toLowerCase();