mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 22:40:13 +01:00
Convert markdownlint library to an ECMAScript module, replace markdownlint-micromark with micromark, stop publishing (large) markdownlint-browser.js, see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c for guidance.
This commit is contained in:
parent
191226f070
commit
1e71f6f44e
140 changed files with 1087 additions and 10428 deletions
75
lib/md032.mjs
Normal file
75
lib/md032.mjs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// @ts-check
|
||||
|
||||
import { addErrorContext, isBlankLine } from "../helpers/helpers.cjs";
|
||||
import { filterByPredicate, getBlockQuotePrefixText, nonContentTokens } from "../helpers/micromark-helpers.cjs";
|
||||
import { filterByTypesCached } from "./cache.mjs";
|
||||
|
||||
const isList = (token) => (
|
||||
(token.type === "listOrdered") || (token.type === "listUnordered")
|
||||
);
|
||||
|
||||
/** @type {import("./markdownlint.mjs").Rule} */
|
||||
export default {
|
||||
"names": [ "MD032", "blanks-around-lists" ],
|
||||
"description": "Lists should be surrounded by blank lines",
|
||||
"tags": [ "bullet", "ul", "ol", "blank_lines" ],
|
||||
"parser": "micromark",
|
||||
"function": function MD032(params, onError) {
|
||||
const { lines, parsers } = params;
|
||||
const blockQuotePrefixes = filterByTypesCached([ "blockQuotePrefix", "linePrefix" ]);
|
||||
|
||||
// For every top-level list...
|
||||
const topLevelLists = filterByPredicate(
|
||||
parsers.micromark.tokens,
|
||||
isList,
|
||||
(token) => (
|
||||
(isList(token) || (token.type === "htmlFlow")) ? [] : token.children
|
||||
)
|
||||
);
|
||||
for (const list of topLevelLists) {
|
||||
|
||||
// Look for a blank line above the list
|
||||
const firstLineNumber = list.startLine;
|
||||
if (!isBlankLine(lines[firstLineNumber - 2])) {
|
||||
addErrorContext(
|
||||
onError,
|
||||
firstLineNumber,
|
||||
lines[firstLineNumber - 1].trim(),
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
{
|
||||
"insertText": getBlockQuotePrefixText(blockQuotePrefixes, firstLineNumber)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Find the "visual" end of the list
|
||||
let endLine = list.endLine;
|
||||
const flattenedChildren = filterByPredicate(list.children);
|
||||
for (const child of flattenedChildren.reverse()) {
|
||||
if (!nonContentTokens.has(child.type)) {
|
||||
endLine = child.endLine;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Look for a blank line below the list
|
||||
const lastLineNumber = endLine;
|
||||
if (!isBlankLine(lines[lastLineNumber])) {
|
||||
addErrorContext(
|
||||
onError,
|
||||
lastLineNumber,
|
||||
lines[lastLineNumber - 1].trim(),
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
{
|
||||
"lineNumber": lastLineNumber + 1,
|
||||
"insertText": getBlockQuotePrefixText(blockQuotePrefixes, lastLineNumber)
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue