2024-06-01 21:32:10 -07:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-10-04 22:41:34 -07:00
|
|
|
const { addErrorContext, isBlankLine } = require("../helpers");
|
|
|
|
const { getBlockQuotePrefixText } = require("../helpers/micromark-helpers.cjs");
|
2024-08-24 22:05:16 -07:00
|
|
|
const { filterByTypesCached } = require("./cache");
|
2024-06-01 21:32:10 -07:00
|
|
|
|
|
|
|
// 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) {
|
2024-08-24 22:05:16 -07:00
|
|
|
const { lines } = params;
|
2024-10-04 22:41:34 -07:00
|
|
|
const blockQuotePrefixes = filterByTypesCached([ "blockQuotePrefix", "linePrefix" ]);
|
|
|
|
|
2024-06-01 21:32:10 -07:00
|
|
|
// For every table...
|
2024-08-24 22:05:16 -07:00
|
|
|
const tables = filterByTypesCached([ "table" ]);
|
2024-06-01 21:32:10 -07:00
|
|
|
for (const table of tables) {
|
2024-10-04 22:41:34 -07:00
|
|
|
|
2024-06-01 21:32:10 -07:00
|
|
|
// Look for a blank line above the table
|
2024-10-04 22:41:34 -07:00
|
|
|
const firstLineNumber = table.startLine;
|
|
|
|
if (!isBlankLine(lines[firstLineNumber - 2])) {
|
|
|
|
addErrorContext(
|
2024-06-01 21:32:10 -07:00
|
|
|
onError,
|
2024-10-04 22:41:34 -07:00
|
|
|
firstLineNumber,
|
|
|
|
lines[firstLineNumber - 1].trim(),
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
"insertText": getBlockQuotePrefixText(blockQuotePrefixes, firstLineNumber)
|
|
|
|
}
|
2024-06-01 21:32:10 -07:00
|
|
|
);
|
|
|
|
}
|
2024-10-04 22:41:34 -07:00
|
|
|
|
2024-06-01 21:32:10 -07:00
|
|
|
// Look for a blank line below the table
|
2024-10-04 22:41:34 -07:00
|
|
|
const lastLineNumber = table.endLine;
|
|
|
|
if (!isBlankLine(lines[lastLineNumber])) {
|
|
|
|
addErrorContext(
|
2024-06-01 21:32:10 -07:00
|
|
|
onError,
|
2024-10-04 22:41:34 -07:00
|
|
|
lastLineNumber,
|
|
|
|
lines[lastLineNumber - 1].trim(),
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
"lineNumber": lastLineNumber + 1,
|
|
|
|
"insertText": getBlockQuotePrefixText(blockQuotePrefixes, lastLineNumber)
|
|
|
|
}
|
2024-06-01 21:32:10 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|