-
-
Notifications
You must be signed in to change notification settings - Fork 758
/
Copy pathmd031.mjs
62 lines (57 loc) · 2.03 KB
/
md031.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// @ts-check
import { addErrorContext, isBlankLine } from "../helpers/helpers.cjs";
import { getParentOfType } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";
const codeFencePrefixRe = /^(.*?)[`~]/;
// eslint-disable-next-line jsdoc/valid-types
/** @typedef {readonly string[]} ReadonlyStringArray */
/**
* Adds an error for the top or bottom of a code fence.
*
* @param {import("markdownlint").RuleOnError} onError Error-reporting callback.
* @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) || [];
const fixInfo = (prefix === undefined) ?
undefined :
{
"lineNumber": lineNumber + (top ? 0 : 1),
"insertText": `${prefix.replace(/[^>]/g, " ").trim()}\n`
};
addErrorContext(
onError,
lineNumber,
line.trim(),
undefined,
undefined,
undefined,
fixInfo
);
}
/** @type {import("markdownlint").Rule} */
export default {
"names": [ "MD031", "blanks-around-fences" ],
"description": "Fenced code blocks should be surrounded by blank lines",
"tags": [ "code", "blank_lines" ],
"parser": "micromark",
"function": function MD031(params, onError) {
const listItems = params.config.list_items;
const includeListItems = (listItems === undefined) ? true : !!listItems;
const { lines } = params;
for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
if (includeListItems || !(getParentOfType(codeBlock, [ "listOrdered", "listUnordered" ]))) {
if (!isBlankLine(lines[codeBlock.startLine - 2])) {
addError(onError, lines, codeBlock.startLine, true);
}
if (!isBlankLine(lines[codeBlock.endLine]) && !isBlankLine(lines[codeBlock.endLine - 1])) {
addError(onError, lines, codeBlock.endLine, false);
}
}
}
}
};