2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-10-01 22:12:12 -07:00
|
|
|
const { addErrorContext, filterTokens } = require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2019-10-01 22:12:12 -07:00
|
|
|
const dollarCommandRe = /^(\s*)(\$\s+)/;
|
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD014", "commands-show-output" ],
|
|
|
|
"description": "Dollar signs used before commands without showing output",
|
|
|
|
"tags": [ "code" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "markdownit",
|
2018-01-21 21:44:25 -08:00
|
|
|
"function": function MD014(params, onError) {
|
2022-06-08 22:10:27 -07:00
|
|
|
for (const type of [ "code_block", "fence" ]) {
|
2019-10-01 22:12:12 -07:00
|
|
|
filterTokens(params, type, (token) => {
|
|
|
|
const margin = (token.type === "fence") ? 1 : 0;
|
2020-01-18 21:28:42 -08:00
|
|
|
const dollarInstances = [];
|
|
|
|
let allDollars = true;
|
2019-10-01 22:12:12 -07:00
|
|
|
for (let i = token.map[0] + margin; i < token.map[1] - margin; i++) {
|
|
|
|
const line = params.lines[i];
|
|
|
|
const lineTrim = line.trim();
|
2020-01-18 21:28:42 -08:00
|
|
|
if (lineTrim) {
|
|
|
|
const match = dollarCommandRe.exec(line);
|
|
|
|
if (match) {
|
|
|
|
const column = match[1].length + 1;
|
|
|
|
const length = match[2].length;
|
|
|
|
dollarInstances.push([ i, lineTrim, column, length ]);
|
|
|
|
} else {
|
|
|
|
allDollars = false;
|
|
|
|
}
|
2019-10-01 22:12:12 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
2020-01-18 21:28:42 -08:00
|
|
|
if (allDollars) {
|
2022-06-08 22:10:27 -07:00
|
|
|
for (const instance of dollarInstances) {
|
2020-01-18 21:28:42 -08:00
|
|
|
const [ i, lineTrim, column, length ] = instance;
|
|
|
|
addErrorContext(
|
|
|
|
onError,
|
2024-02-27 20:42:09 -08:00
|
|
|
// @ts-ignore
|
2020-01-18 21:28:42 -08:00
|
|
|
i + 1,
|
2024-06-01 21:32:10 -07:00
|
|
|
// @ts-ignore
|
2020-01-18 21:28:42 -08:00
|
|
|
lineTrim,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
[ column, length ],
|
|
|
|
{
|
|
|
|
"editColumn": column,
|
|
|
|
"deleteCount": length
|
|
|
|
}
|
|
|
|
);
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2020-01-18 21:28:42 -08:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
});
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
};
|