Skip to content

Commit f079df1

Browse files
committed
Reimplement MD022/blanks-around-headings using micromark tokens.
1 parent 1eb40d3 commit f079df1

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

demo/markdownlint-browser.js

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,17 +4143,15 @@ module.exports = {
41434143

41444144

41454145

4146-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
4147-
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."); }
4146+
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; } } }; }
41484147
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); }
41494148
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; }
4150-
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; } }
4151-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
41524149
var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
41534150
addErrorDetailIf = _require.addErrorDetailIf,
41544151
blockquotePrefixRe = _require.blockquotePrefixRe,
4155-
filterTokens = _require.filterTokens,
41564152
isBlankLine = _require.isBlankLine;
4153+
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
4154+
filterByTypes = _require2.filterByTypes;
41574155
var getBlockQuote = function getBlockQuote(str, count) {
41584156
return (str || "").match(blockquotePrefixRe)[0].trimEnd()
41594157
// eslint-disable-next-line unicorn/prefer-spread
@@ -4168,31 +4166,42 @@ module.exports = {
41684166
linesAbove = Number(linesAbove === undefined ? 1 : linesAbove);
41694167
var linesBelow = params.config.lines_below;
41704168
linesBelow = Number(linesBelow === undefined ? 1 : linesBelow);
4171-
var lines = params.lines;
4172-
filterTokens(params, "heading_open", function (token) {
4173-
var _token$map = _slicedToArray(token.map, 2),
4174-
topIndex = _token$map[0],
4175-
nextIndex = _token$map[1];
4176-
var actualAbove = 0;
4177-
for (var i = 0; i < linesAbove; i++) {
4178-
if (isBlankLine(lines[topIndex - i - 1])) {
4179-
actualAbove++;
4169+
var lines = params.lines,
4170+
parsers = params.parsers;
4171+
var headings = filterByTypes(parsers.micromark.tokens, ["atxHeading", "setextHeading"]);
4172+
var _iterator = _createForOfIteratorHelper(headings),
4173+
_step;
4174+
try {
4175+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
4176+
var heading = _step.value;
4177+
var startLine = heading.startLine,
4178+
endLine = heading.endLine;
4179+
var line = lines[startLine - 1].trim();
4180+
var actualAbove = 0;
4181+
for (var i = 0; i < linesAbove; i++) {
4182+
if (isBlankLine(lines[startLine - 2 - i])) {
4183+
actualAbove++;
4184+
}
41804185
}
4181-
}
4182-
addErrorDetailIf(onError, topIndex + 1, linesAbove, actualAbove, "Above", lines[topIndex].trim(), null, {
4183-
"insertText": getBlockQuote(lines[topIndex - 1], linesAbove - actualAbove)
4184-
});
4185-
var actualBelow = 0;
4186-
for (var _i2 = 0; _i2 < linesBelow; _i2++) {
4187-
if (isBlankLine(lines[nextIndex + _i2])) {
4188-
actualBelow++;
4186+
addErrorDetailIf(onError, startLine, linesAbove, actualAbove, "Above", line, null, {
4187+
"insertText": getBlockQuote(lines[startLine - 2], linesAbove - actualAbove)
4188+
});
4189+
var actualBelow = 0;
4190+
for (var _i = 0; _i < linesBelow; _i++) {
4191+
if (isBlankLine(lines[endLine + _i])) {
4192+
actualBelow++;
4193+
}
41894194
}
4195+
addErrorDetailIf(onError, startLine, linesBelow, actualBelow, "Below", line, null, {
4196+
"lineNumber": endLine + 1,
4197+
"insertText": getBlockQuote(lines[endLine], linesBelow - actualBelow)
4198+
});
41904199
}
4191-
addErrorDetailIf(onError, topIndex + 1, linesBelow, actualBelow, "Below", lines[topIndex].trim(), null, {
4192-
"lineNumber": nextIndex + 1,
4193-
"insertText": getBlockQuote(lines[nextIndex], linesBelow - actualBelow)
4194-
});
4195-
});
4200+
} catch (err) {
4201+
_iterator.e(err);
4202+
} finally {
4203+
_iterator.f();
4204+
}
41964205
}
41974206
};
41984207

lib/md022.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
"use strict";
44

5-
const { addErrorDetailIf, blockquotePrefixRe, filterTokens, isBlankLine } =
5+
const { addErrorDetailIf, blockquotePrefixRe, isBlankLine } =
66
require("../helpers");
7+
const { filterByTypes } = require("../helpers/micromark.cjs");
78

89
const getBlockQuote = (str, count) => (
910
(str || "")
@@ -23,46 +24,53 @@ module.exports = {
2324
linesAbove = Number((linesAbove === undefined) ? 1 : linesAbove);
2425
let linesBelow = params.config.lines_below;
2526
linesBelow = Number((linesBelow === undefined) ? 1 : linesBelow);
26-
const { lines } = params;
27-
filterTokens(params, "heading_open", (token) => {
28-
const [ topIndex, nextIndex ] = token.map;
27+
const { lines, parsers } = params;
28+
const headings = filterByTypes(
29+
parsers.micromark.tokens,
30+
[ "atxHeading", "setextHeading" ]
31+
);
32+
for (const heading of headings) {
33+
const { startLine, endLine } = heading;
34+
const line = lines[startLine - 1].trim();
2935
let actualAbove = 0;
3036
for (let i = 0; i < linesAbove; i++) {
31-
if (isBlankLine(lines[topIndex - i - 1])) {
37+
if (isBlankLine(lines[startLine - 2 - i])) {
3238
actualAbove++;
3339
}
3440
}
3541
addErrorDetailIf(
3642
onError,
37-
topIndex + 1,
43+
startLine,
3844
linesAbove,
3945
actualAbove,
4046
"Above",
41-
lines[topIndex].trim(),
47+
line,
4248
null,
4349
{
4450
"insertText":
45-
getBlockQuote(lines[topIndex - 1], linesAbove - actualAbove)
46-
});
51+
getBlockQuote(lines[startLine - 2], linesAbove - actualAbove)
52+
}
53+
);
4754
let actualBelow = 0;
4855
for (let i = 0; i < linesBelow; i++) {
49-
if (isBlankLine(lines[nextIndex + i])) {
56+
if (isBlankLine(lines[endLine + i])) {
5057
actualBelow++;
5158
}
5259
}
5360
addErrorDetailIf(
5461
onError,
55-
topIndex + 1,
62+
startLine,
5663
linesBelow,
5764
actualBelow,
5865
"Below",
59-
lines[topIndex].trim(),
66+
line,
6067
null,
6168
{
62-
"lineNumber": nextIndex + 1,
69+
"lineNumber": endLine + 1,
6370
"insertText":
64-
getBlockQuote(lines[nextIndex], linesBelow - actualBelow)
65-
});
66-
});
71+
getBlockQuote(lines[endLine], linesBelow - actualBelow)
72+
}
73+
);
74+
}
6775
}
6876
};

0 commit comments

Comments
 (0)