Skip to content

Commit

Permalink
Reimplement MD032/blanks-around-lists using micromark tokens, add new…
Browse files Browse the repository at this point in the history
…ly-detected violations to test snapshot.
  • Loading branch information
DavidAnson committed Jul 19, 2023
1 parent 900fb34 commit 9646590
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 51 deletions.
84 changes: 65 additions & 19 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -1592,6 +1609,7 @@ module.exports = {
"parse": micromarkParse,
filterByPredicate: filterByPredicate,
filterByTypes: filterByTypes,
flattenedChildren: flattenedChildren,
getHtmlTagInfo: getHtmlTagInfo,
getMicromarkEvents: getMicromarkEvents,
getTokenTextByType: getTokenTextByType,
Expand Down Expand Up @@ -4922,38 +4940,66 @@ var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
addErrorContext = _require.addErrorContext,
blockquotePrefixRe = _require.blockquotePrefixRe,
isBlankLine = _require.isBlankLine;
var _require2 = __webpack_require__(/*! ./cache */ "../lib/cache.js"),
flattenedLists = _require2.flattenedLists;
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
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 = {
"names": ["MD032", "blanks-around-lists"],
"description": "Lists should be surrounded by blank lines",
"tags": ["bullet", "ul", "ol", "blank_lines"],
"function": function MD032(params, onError) {
var lines = params.lines;
var filteredLists = flattenedLists().filter(function (list) {
return !list.nesting;
var lines = params.lines,
parsers = params.parsers;

// 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;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
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])) {
var line = lines[firstIndex];
var quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
addErrorContext(onError, firstIndex + 1, line.trim(), null, null, null, {
"insertText": "".concat(quotePrefix, "\n")
});
addBlankLineError(onError, lines, firstIndex);
}

// 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 = list.lastLineIndex - 1;
var lastIndex = endLine - 1;
if (!isBlankLine(lines[lastIndex + 1])) {
var _line = lines[lastIndex];
var _quotePrefix = _line.match(blockquotePrefixRe)[0].trimEnd();
addErrorContext(onError, lastIndex + 1, _line.trim(), null, null, null, {
"lineNumber": lastIndex + 2,
"insertText": "".concat(_quotePrefix, "\n")
});
addBlankLineError(onError, lines, lastIndex, lastIndex + 2);
}
}
} catch (err) {
Expand Down
18 changes: 18 additions & 0 deletions helpers/micromark.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -219,6 +236,7 @@ module.exports = {
"parse": micromarkParse,
filterByPredicate,
filterByTypes,
flattenedChildren,
getHtmlTagInfo,
getMicromarkEvents,
getTokenTextByType,
Expand Down
88 changes: 57 additions & 31 deletions lib/md032.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,72 @@

const { addErrorContext, blockquotePrefixRe, isBlankLine } =
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 = {
"names": [ "MD032", "blanks-around-lists" ],
"description": "Lists should be surrounded by blank lines",
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
"function": function MD032(params, onError) {
const { lines } = params;
const filteredLists = flattenedLists().filter((list) => !list.nesting);
for (const list of filteredLists) {
const firstIndex = list.open.map[0];
const { lines, parsers } = params;

// For every top-level list...
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])) {
const line = lines[firstIndex];
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
addErrorContext(
onError,
firstIndex + 1,
line.trim(),
null,
null,
null,
{
"insertText": `${quotePrefix}\n`
});
addBlankLineError(onError, lines, firstIndex);
}

// 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;
}
}
const lastIndex = list.lastLineIndex - 1;

// Look for a blank line below the list
const lastIndex = endLine - 1;
if (!isBlankLine(lines[lastIndex + 1])) {
const line = lines[lastIndex];
const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
addErrorContext(
onError,
lastIndex + 1,
line.trim(),
null,
null,
null,
{
"lineNumber": lastIndex + 2,
"insertText": `${quotePrefix}\n`
});
addBlankLineError(onError, lines, lastIndex, lastIndex + 2);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion test/snapshots/markdownlint-test-repos.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Generated by [AVA](https://avajs.dev).

> 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

Expand Down
Binary file modified test/snapshots/markdownlint-test-repos.js.snap
Binary file not shown.

0 comments on commit 9646590

Please sign in to comment.