mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Reimplement MD032/blanks-around-lists using micromark tokens, add newly-detected violations to test snapshot.
This commit is contained in:
parent
900fb349ee
commit
9646590496
5 changed files with 143 additions and 51 deletions
|
|
@ -1517,6 +1517,23 @@ function filterByTypes(tokens, allowed) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of all nested child tokens.
|
||||||
|
*
|
||||||
|
* @param {Token} parent Micromark token.
|
||||||
|
* @returns {Token[]} Flattened children.
|
||||||
|
*/
|
||||||
|
function flattenedChildren(parent) {
|
||||||
|
var result = [];
|
||||||
|
var pending = _toConsumableArray(parent.children);
|
||||||
|
var token = null;
|
||||||
|
while (token = pending.shift()) {
|
||||||
|
result.push(token);
|
||||||
|
pending.unshift.apply(pending, _toConsumableArray(token.children));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets information about the tag in an HTML token.
|
* Gets information about the tag in an HTML token.
|
||||||
*
|
*
|
||||||
|
|
@ -1592,6 +1609,7 @@ module.exports = {
|
||||||
"parse": micromarkParse,
|
"parse": micromarkParse,
|
||||||
filterByPredicate: filterByPredicate,
|
filterByPredicate: filterByPredicate,
|
||||||
filterByTypes: filterByTypes,
|
filterByTypes: filterByTypes,
|
||||||
|
flattenedChildren: flattenedChildren,
|
||||||
getHtmlTagInfo: getHtmlTagInfo,
|
getHtmlTagInfo: getHtmlTagInfo,
|
||||||
getMicromarkEvents: getMicromarkEvents,
|
getMicromarkEvents: getMicromarkEvents,
|
||||||
getTokenTextByType: getTokenTextByType,
|
getTokenTextByType: getTokenTextByType,
|
||||||
|
|
@ -4922,38 +4940,66 @@ var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
|
||||||
addErrorContext = _require.addErrorContext,
|
addErrorContext = _require.addErrorContext,
|
||||||
blockquotePrefixRe = _require.blockquotePrefixRe,
|
blockquotePrefixRe = _require.blockquotePrefixRe,
|
||||||
isBlankLine = _require.isBlankLine;
|
isBlankLine = _require.isBlankLine;
|
||||||
var _require2 = __webpack_require__(/*! ./cache */ "../lib/cache.js"),
|
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
|
||||||
flattenedLists = _require2.flattenedLists;
|
filterByPredicate = _require2.filterByPredicate,
|
||||||
|
flattenedChildren = _require2.flattenedChildren;
|
||||||
|
var nonContentTokens = new Set(["blockQuoteMarker", "blockQuotePrefix", "blockQuotePrefixWhitespace", "lineEnding", "lineEndingBlank", "linePrefix", "listItemIndent"]);
|
||||||
|
var isList = function isList(token) {
|
||||||
|
return token.type === "listOrdered" || token.type === "listUnordered";
|
||||||
|
};
|
||||||
|
var addBlankLineError = function addBlankLineError(onError, lines, lineIndex, lineNumber) {
|
||||||
|
var line = lines[lineIndex];
|
||||||
|
var quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
|
||||||
|
addErrorContext(onError, lineIndex + 1, line.trim(), null, null, null, {
|
||||||
|
lineNumber: lineNumber,
|
||||||
|
"insertText": "".concat(quotePrefix, "\n")
|
||||||
|
});
|
||||||
|
};
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": ["MD032", "blanks-around-lists"],
|
"names": ["MD032", "blanks-around-lists"],
|
||||||
"description": "Lists should be surrounded by blank lines",
|
"description": "Lists should be surrounded by blank lines",
|
||||||
"tags": ["bullet", "ul", "ol", "blank_lines"],
|
"tags": ["bullet", "ul", "ol", "blank_lines"],
|
||||||
"function": function MD032(params, onError) {
|
"function": function MD032(params, onError) {
|
||||||
var lines = params.lines;
|
var lines = params.lines,
|
||||||
var filteredLists = flattenedLists().filter(function (list) {
|
parsers = params.parsers;
|
||||||
return !list.nesting;
|
|
||||||
|
// For every top-level list...
|
||||||
|
var topLevelLists = filterByPredicate(parsers.micromark.tokens, isList, function (token) {
|
||||||
|
return isList(token) ? [] : token.children;
|
||||||
});
|
});
|
||||||
var _iterator = _createForOfIteratorHelper(filteredLists),
|
var _iterator = _createForOfIteratorHelper(topLevelLists),
|
||||||
_step;
|
_step;
|
||||||
try {
|
try {
|
||||||
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
||||||
var list = _step.value;
|
var list = _step.value;
|
||||||
var firstIndex = list.open.map[0];
|
// Look for a blank line above the list
|
||||||
|
var firstIndex = list.startLine - 1;
|
||||||
if (!isBlankLine(lines[firstIndex - 1])) {
|
if (!isBlankLine(lines[firstIndex - 1])) {
|
||||||
var line = lines[firstIndex];
|
addBlankLineError(onError, lines, firstIndex);
|
||||||
var quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
|
|
||||||
addErrorContext(onError, firstIndex + 1, line.trim(), null, null, null, {
|
|
||||||
"insertText": "".concat(quotePrefix, "\n")
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
var lastIndex = list.lastLineIndex - 1;
|
|
||||||
|
// Find the "visual" end of the list
|
||||||
|
var endLine = list.endLine;
|
||||||
|
var _iterator2 = _createForOfIteratorHelper(flattenedChildren(list).reverse()),
|
||||||
|
_step2;
|
||||||
|
try {
|
||||||
|
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
||||||
|
var child = _step2.value;
|
||||||
|
if (!nonContentTokens.has(child.type)) {
|
||||||
|
endLine = child.endLine;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for a blank line below the list
|
||||||
|
} catch (err) {
|
||||||
|
_iterator2.e(err);
|
||||||
|
} finally {
|
||||||
|
_iterator2.f();
|
||||||
|
}
|
||||||
|
var lastIndex = endLine - 1;
|
||||||
if (!isBlankLine(lines[lastIndex + 1])) {
|
if (!isBlankLine(lines[lastIndex + 1])) {
|
||||||
var _line = lines[lastIndex];
|
addBlankLineError(onError, lines, lastIndex, lastIndex + 2);
|
||||||
var _quotePrefix = _line.match(blockquotePrefixRe)[0].trimEnd();
|
|
||||||
addErrorContext(onError, lastIndex + 1, _line.trim(), null, null, null, {
|
|
||||||
"lineNumber": lastIndex + 2,
|
|
||||||
"insertText": "".concat(_quotePrefix, "\n")
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
||||||
|
|
@ -145,6 +145,23 @@ function filterByTypes(tokens, allowed) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of all nested child tokens.
|
||||||
|
*
|
||||||
|
* @param {Token} parent Micromark token.
|
||||||
|
* @returns {Token[]} Flattened children.
|
||||||
|
*/
|
||||||
|
function flattenedChildren(parent) {
|
||||||
|
const result = [];
|
||||||
|
const pending = [ ...parent.children ];
|
||||||
|
let token = null;
|
||||||
|
while ((token = pending.shift())) {
|
||||||
|
result.push(token);
|
||||||
|
pending.unshift(...token.children);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets information about the tag in an HTML token.
|
* Gets information about the tag in an HTML token.
|
||||||
*
|
*
|
||||||
|
|
@ -219,6 +236,7 @@ module.exports = {
|
||||||
"parse": micromarkParse,
|
"parse": micromarkParse,
|
||||||
filterByPredicate,
|
filterByPredicate,
|
||||||
filterByTypes,
|
filterByTypes,
|
||||||
|
flattenedChildren,
|
||||||
getHtmlTagInfo,
|
getHtmlTagInfo,
|
||||||
getMicromarkEvents,
|
getMicromarkEvents,
|
||||||
getTokenTextByType,
|
getTokenTextByType,
|
||||||
|
|
|
||||||
88
lib/md032.js
88
lib/md032.js
|
|
@ -4,46 +4,72 @@
|
||||||
|
|
||||||
const { addErrorContext, blockquotePrefixRe, isBlankLine } =
|
const { addErrorContext, blockquotePrefixRe, isBlankLine } =
|
||||||
require("../helpers");
|
require("../helpers");
|
||||||
const { flattenedLists } = require("./cache");
|
const { filterByPredicate, flattenedChildren } =
|
||||||
|
require("../helpers/micromark.cjs");
|
||||||
|
|
||||||
|
const nonContentTokens = new Set([
|
||||||
|
"blockQuoteMarker",
|
||||||
|
"blockQuotePrefix",
|
||||||
|
"blockQuotePrefixWhitespace",
|
||||||
|
"lineEnding",
|
||||||
|
"lineEndingBlank",
|
||||||
|
"linePrefix",
|
||||||
|
"listItemIndent"
|
||||||
|
]);
|
||||||
|
const isList = (token) => (
|
||||||
|
(token.type === "listOrdered") || (token.type === "listUnordered")
|
||||||
|
);
|
||||||
|
const addBlankLineError = (onError, lines, lineIndex, lineNumber) => {
|
||||||
|
const line = lines[lineIndex];
|
||||||
|
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
|
||||||
|
addErrorContext(
|
||||||
|
onError,
|
||||||
|
lineIndex + 1,
|
||||||
|
line.trim(),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
{
|
||||||
|
lineNumber,
|
||||||
|
"insertText": `${quotePrefix}\n`
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
"names": [ "MD032", "blanks-around-lists" ],
|
"names": [ "MD032", "blanks-around-lists" ],
|
||||||
"description": "Lists should be surrounded by blank lines",
|
"description": "Lists should be surrounded by blank lines",
|
||||||
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
|
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
|
||||||
"function": function MD032(params, onError) {
|
"function": function MD032(params, onError) {
|
||||||
const { lines } = params;
|
const { lines, parsers } = params;
|
||||||
const filteredLists = flattenedLists().filter((list) => !list.nesting);
|
|
||||||
for (const list of filteredLists) {
|
// For every top-level list...
|
||||||
const firstIndex = list.open.map[0];
|
const topLevelLists = filterByPredicate(
|
||||||
|
parsers.micromark.tokens,
|
||||||
|
isList,
|
||||||
|
(token) => (isList(token) ? [] : token.children)
|
||||||
|
);
|
||||||
|
for (const list of topLevelLists) {
|
||||||
|
|
||||||
|
// Look for a blank line above the list
|
||||||
|
const firstIndex = list.startLine - 1;
|
||||||
if (!isBlankLine(lines[firstIndex - 1])) {
|
if (!isBlankLine(lines[firstIndex - 1])) {
|
||||||
const line = lines[firstIndex];
|
addBlankLineError(onError, lines, firstIndex);
|
||||||
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
|
|
||||||
addErrorContext(
|
|
||||||
onError,
|
|
||||||
firstIndex + 1,
|
|
||||||
line.trim(),
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
{
|
|
||||||
"insertText": `${quotePrefix}\n`
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
const lastIndex = list.lastLineIndex - 1;
|
|
||||||
|
// Find the "visual" end of the list
|
||||||
|
let endLine = list.endLine;
|
||||||
|
for (const child of flattenedChildren(list).reverse()) {
|
||||||
|
if (!nonContentTokens.has(child.type)) {
|
||||||
|
endLine = child.endLine;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for a blank line below the list
|
||||||
|
const lastIndex = endLine - 1;
|
||||||
if (!isBlankLine(lines[lastIndex + 1])) {
|
if (!isBlankLine(lines[lastIndex + 1])) {
|
||||||
const line = lines[lastIndex];
|
addBlankLineError(onError, lines, lastIndex, lastIndex + 2);
|
||||||
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
|
|
||||||
addErrorContext(
|
|
||||||
onError,
|
|
||||||
lastIndex + 1,
|
|
||||||
line.trim(),
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
{
|
|
||||||
"lineNumber": lastIndex + 2,
|
|
||||||
"insertText": `${quotePrefix}\n`
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,9 @@ Generated by [AVA](https://avajs.dev).
|
||||||
|
|
||||||
> Expected linting violations
|
> Expected linting violations
|
||||||
|
|
||||||
''
|
`test-repos/dotnet-docs/docs/azure/sdk/includes/assign-local-dev-group-to-role-azure-portal-4.md: 5: MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "<br>"]␊
|
||||||
|
test-repos/dotnet-docs/docs/azure/sdk/includes/assign-managed-identity-to-role-azure-portal-4.md: 5: MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "<br>"]␊
|
||||||
|
test-repos/dotnet-docs/docs/azure/sdk/includes/assign-service-principal-to-role-azure-portal-4.md: 5: MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "<br>"]`
|
||||||
|
|
||||||
## https://github.com/electron/electron
|
## https://github.com/electron/electron
|
||||||
|
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue