mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 14:00:13 +01:00
Reimplement MD022/blanks-around-headings using micromark tokens.
This commit is contained in:
parent
1eb40d3c4c
commit
f079df140c
2 changed files with 60 additions and 43 deletions
|
|
@ -4143,17 +4143,15 @@ module.exports = {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
||||||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
||||||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
||||||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
||||||
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
||||||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
||||||
var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
|
var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
|
||||||
addErrorDetailIf = _require.addErrorDetailIf,
|
addErrorDetailIf = _require.addErrorDetailIf,
|
||||||
blockquotePrefixRe = _require.blockquotePrefixRe,
|
blockquotePrefixRe = _require.blockquotePrefixRe,
|
||||||
filterTokens = _require.filterTokens,
|
|
||||||
isBlankLine = _require.isBlankLine;
|
isBlankLine = _require.isBlankLine;
|
||||||
|
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
|
||||||
|
filterByTypes = _require2.filterByTypes;
|
||||||
var getBlockQuote = function getBlockQuote(str, count) {
|
var getBlockQuote = function getBlockQuote(str, count) {
|
||||||
return (str || "").match(blockquotePrefixRe)[0].trimEnd()
|
return (str || "").match(blockquotePrefixRe)[0].trimEnd()
|
||||||
// eslint-disable-next-line unicorn/prefer-spread
|
// eslint-disable-next-line unicorn/prefer-spread
|
||||||
|
|
@ -4168,31 +4166,42 @@ module.exports = {
|
||||||
linesAbove = Number(linesAbove === undefined ? 1 : linesAbove);
|
linesAbove = Number(linesAbove === undefined ? 1 : linesAbove);
|
||||||
var linesBelow = params.config.lines_below;
|
var linesBelow = params.config.lines_below;
|
||||||
linesBelow = Number(linesBelow === undefined ? 1 : linesBelow);
|
linesBelow = Number(linesBelow === undefined ? 1 : linesBelow);
|
||||||
var lines = params.lines;
|
var lines = params.lines,
|
||||||
filterTokens(params, "heading_open", function (token) {
|
parsers = params.parsers;
|
||||||
var _token$map = _slicedToArray(token.map, 2),
|
var headings = filterByTypes(parsers.micromark.tokens, ["atxHeading", "setextHeading"]);
|
||||||
topIndex = _token$map[0],
|
var _iterator = _createForOfIteratorHelper(headings),
|
||||||
nextIndex = _token$map[1];
|
_step;
|
||||||
var actualAbove = 0;
|
try {
|
||||||
for (var i = 0; i < linesAbove; i++) {
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
||||||
if (isBlankLine(lines[topIndex - i - 1])) {
|
var heading = _step.value;
|
||||||
actualAbove++;
|
var startLine = heading.startLine,
|
||||||
|
endLine = heading.endLine;
|
||||||
|
var line = lines[startLine - 1].trim();
|
||||||
|
var actualAbove = 0;
|
||||||
|
for (var i = 0; i < linesAbove; i++) {
|
||||||
|
if (isBlankLine(lines[startLine - 2 - i])) {
|
||||||
|
actualAbove++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
addErrorDetailIf(onError, startLine, linesAbove, actualAbove, "Above", line, null, {
|
||||||
addErrorDetailIf(onError, topIndex + 1, linesAbove, actualAbove, "Above", lines[topIndex].trim(), null, {
|
"insertText": getBlockQuote(lines[startLine - 2], linesAbove - actualAbove)
|
||||||
"insertText": getBlockQuote(lines[topIndex - 1], linesAbove - actualAbove)
|
});
|
||||||
});
|
var actualBelow = 0;
|
||||||
var actualBelow = 0;
|
for (var _i = 0; _i < linesBelow; _i++) {
|
||||||
for (var _i2 = 0; _i2 < linesBelow; _i2++) {
|
if (isBlankLine(lines[endLine + _i])) {
|
||||||
if (isBlankLine(lines[nextIndex + _i2])) {
|
actualBelow++;
|
||||||
actualBelow++;
|
}
|
||||||
}
|
}
|
||||||
|
addErrorDetailIf(onError, startLine, linesBelow, actualBelow, "Below", line, null, {
|
||||||
|
"lineNumber": endLine + 1,
|
||||||
|
"insertText": getBlockQuote(lines[endLine], linesBelow - actualBelow)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
addErrorDetailIf(onError, topIndex + 1, linesBelow, actualBelow, "Below", lines[topIndex].trim(), null, {
|
} catch (err) {
|
||||||
"lineNumber": nextIndex + 1,
|
_iterator.e(err);
|
||||||
"insertText": getBlockQuote(lines[nextIndex], linesBelow - actualBelow)
|
} finally {
|
||||||
});
|
_iterator.f();
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
40
lib/md022.js
40
lib/md022.js
|
|
@ -2,8 +2,9 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addErrorDetailIf, blockquotePrefixRe, filterTokens, isBlankLine } =
|
const { addErrorDetailIf, blockquotePrefixRe, isBlankLine } =
|
||||||
require("../helpers");
|
require("../helpers");
|
||||||
|
const { filterByTypes } = require("../helpers/micromark.cjs");
|
||||||
|
|
||||||
const getBlockQuote = (str, count) => (
|
const getBlockQuote = (str, count) => (
|
||||||
(str || "")
|
(str || "")
|
||||||
|
|
@ -23,46 +24,53 @@ module.exports = {
|
||||||
linesAbove = Number((linesAbove === undefined) ? 1 : linesAbove);
|
linesAbove = Number((linesAbove === undefined) ? 1 : linesAbove);
|
||||||
let linesBelow = params.config.lines_below;
|
let linesBelow = params.config.lines_below;
|
||||||
linesBelow = Number((linesBelow === undefined) ? 1 : linesBelow);
|
linesBelow = Number((linesBelow === undefined) ? 1 : linesBelow);
|
||||||
const { lines } = params;
|
const { lines, parsers } = params;
|
||||||
filterTokens(params, "heading_open", (token) => {
|
const headings = filterByTypes(
|
||||||
const [ topIndex, nextIndex ] = token.map;
|
parsers.micromark.tokens,
|
||||||
|
[ "atxHeading", "setextHeading" ]
|
||||||
|
);
|
||||||
|
for (const heading of headings) {
|
||||||
|
const { startLine, endLine } = heading;
|
||||||
|
const line = lines[startLine - 1].trim();
|
||||||
let actualAbove = 0;
|
let actualAbove = 0;
|
||||||
for (let i = 0; i < linesAbove; i++) {
|
for (let i = 0; i < linesAbove; i++) {
|
||||||
if (isBlankLine(lines[topIndex - i - 1])) {
|
if (isBlankLine(lines[startLine - 2 - i])) {
|
||||||
actualAbove++;
|
actualAbove++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addErrorDetailIf(
|
addErrorDetailIf(
|
||||||
onError,
|
onError,
|
||||||
topIndex + 1,
|
startLine,
|
||||||
linesAbove,
|
linesAbove,
|
||||||
actualAbove,
|
actualAbove,
|
||||||
"Above",
|
"Above",
|
||||||
lines[topIndex].trim(),
|
line,
|
||||||
null,
|
null,
|
||||||
{
|
{
|
||||||
"insertText":
|
"insertText":
|
||||||
getBlockQuote(lines[topIndex - 1], linesAbove - actualAbove)
|
getBlockQuote(lines[startLine - 2], linesAbove - actualAbove)
|
||||||
});
|
}
|
||||||
|
);
|
||||||
let actualBelow = 0;
|
let actualBelow = 0;
|
||||||
for (let i = 0; i < linesBelow; i++) {
|
for (let i = 0; i < linesBelow; i++) {
|
||||||
if (isBlankLine(lines[nextIndex + i])) {
|
if (isBlankLine(lines[endLine + i])) {
|
||||||
actualBelow++;
|
actualBelow++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addErrorDetailIf(
|
addErrorDetailIf(
|
||||||
onError,
|
onError,
|
||||||
topIndex + 1,
|
startLine,
|
||||||
linesBelow,
|
linesBelow,
|
||||||
actualBelow,
|
actualBelow,
|
||||||
"Below",
|
"Below",
|
||||||
lines[topIndex].trim(),
|
line,
|
||||||
null,
|
null,
|
||||||
{
|
{
|
||||||
"lineNumber": nextIndex + 1,
|
"lineNumber": endLine + 1,
|
||||||
"insertText":
|
"insertText":
|
||||||
getBlockQuote(lines[nextIndex], linesBelow - actualBelow)
|
getBlockQuote(lines[endLine], linesBelow - actualBelow)
|
||||||
});
|
}
|
||||||
});
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue