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

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
);
}
}
}
};