2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-04-13 11:18:57 -07:00
|
|
|
const { addErrorContext, forEachLine, isBlankLine } = require("../helpers");
|
2019-04-10 21:26:59 -07:00
|
|
|
const { lineMetadata } = require("./cache");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2020-04-07 21:01:39 -07:00
|
|
|
const codeFencePrefixRe = /^(.*?)\s*[`~]/;
|
|
|
|
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD031", "blanks-around-fences" ],
|
|
|
|
"description": "Fenced code blocks should be surrounded by blank lines",
|
|
|
|
"tags": [ "code", "blank_lines" ],
|
|
|
|
"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;
|
2019-03-20 21:48:18 -07:00
|
|
|
const { lines } = params;
|
2019-08-02 22:58:41 -07:00
|
|
|
forEachLine(lineMetadata(), (line, i, inCode, onFence, inTable, inItem) => {
|
2019-08-28 21:47:07 -07:00
|
|
|
const onTopFence = (onFence > 0);
|
|
|
|
const onBottomFence = (onFence < 0);
|
|
|
|
if ((includeListItems || !inItem) &&
|
|
|
|
((onTopFence && !isBlankLine(lines[i - 1])) ||
|
|
|
|
(onBottomFence && !isBlankLine(lines[i + 1])))) {
|
2020-10-16 14:08:42 -07:00
|
|
|
const [ , prefix ] = line.match(codeFencePrefixRe) || [];
|
|
|
|
const fixInfo = (prefix === undefined) ? null : {
|
|
|
|
"lineNumber": i + (onTopFence ? 1 : 2),
|
|
|
|
"insertText": `${prefix}\n`
|
|
|
|
};
|
2019-08-28 21:47:07 -07:00
|
|
|
addErrorContext(
|
|
|
|
onError,
|
|
|
|
i + 1,
|
|
|
|
lines[i].trim(),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
2020-10-16 14:08:42 -07:00
|
|
|
fixInfo);
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|