2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addErrorContext, isBlankLine } from "../helpers/helpers.cjs";
|
|
|
|
import { getParentOfType } from "../helpers/micromark-helpers.cjs";
|
|
|
|
import { filterByTypesCached } from "./cache.mjs";
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2022-05-14 19:18:13 -07:00
|
|
|
const codeFencePrefixRe = /^(.*?)[`~]/;
|
2020-04-07 21:01:39 -07:00
|
|
|
|
2024-08-17 17:58:16 -07:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @typedef {readonly string[]} ReadonlyStringArray */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an error for the top or bottom of a code fence.
|
|
|
|
*
|
2024-12-03 19:58:28 -08:00
|
|
|
* @param {import("markdownlint").RuleOnError} onError Error-reporting callback.
|
2024-08-17 17:58:16 -07:00
|
|
|
* @param {ReadonlyStringArray} lines Lines of Markdown content.
|
|
|
|
* @param {number} lineNumber Line number.
|
|
|
|
* @param {boolean} top True iff top fence.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function addError(onError, lines, lineNumber, top) {
|
|
|
|
const line = lines[lineNumber - 1];
|
|
|
|
const [ , prefix ] = line.match(codeFencePrefixRe) || [];
|
2024-09-01 16:16:05 -07:00
|
|
|
const fixInfo = (prefix === undefined) ?
|
2024-11-28 20:36:44 -08:00
|
|
|
undefined :
|
2024-09-01 16:16:05 -07:00
|
|
|
{
|
|
|
|
"lineNumber": lineNumber + (top ? 0 : 1),
|
|
|
|
"insertText": `${prefix.replace(/[^>]/g, " ").trim()}\n`
|
|
|
|
};
|
2024-08-17 17:58:16 -07:00
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
lineNumber,
|
|
|
|
line.trim(),
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
fixInfo
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
2018-01-21 21:44:25 -08:00
|
|
|
"names": [ "MD031", "blanks-around-fences" ],
|
|
|
|
"description": "Fenced code blocks should be surrounded by blank lines",
|
|
|
|
"tags": [ "code", "blank_lines" ],
|
2024-08-17 17:58:16 -07:00
|
|
|
"parser": "micromark",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD031(params, onError) {
|
2019-08-02 22:58:41 -07:00
|
|
|
const listItems = params.config.list_items;
|
|
|
|
const includeListItems = (listItems === undefined) ? true : !!listItems;
|
2024-08-24 22:05:16 -07:00
|
|
|
const { lines } = params;
|
|
|
|
for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
|
2024-09-28 15:55:26 -07:00
|
|
|
if (includeListItems || !(getParentOfType(codeBlock, [ "listOrdered", "listUnordered" ]))) {
|
2024-08-17 17:58:16 -07:00
|
|
|
if (!isBlankLine(lines[codeBlock.startLine - 2])) {
|
|
|
|
addError(onError, lines, codeBlock.startLine, true);
|
|
|
|
}
|
2024-08-17 20:29:27 -07:00
|
|
|
if (!isBlankLine(lines[codeBlock.endLine]) && !isBlankLine(lines[codeBlock.endLine - 1])) {
|
2024-08-17 17:58:16 -07:00
|
|
|
addError(onError, lines, codeBlock.endLine, false);
|
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2024-08-17 17:58:16 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|