mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10: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
62
lib/md031.mjs
Normal file
62
lib/md031.mjs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
// @ts-check
|
||||
|
||||
import { addErrorContext, isBlankLine } from "../helpers/helpers.cjs";
|
||||
import { getParentOfType } from "../helpers/micromark-helpers.cjs";
|
||||
import { filterByTypesCached } from "./cache.mjs";
|
||||
|
||||
const codeFencePrefixRe = /^(.*?)[`~]/;
|
||||
|
||||
// eslint-disable-next-line jsdoc/valid-types
|
||||
/** @typedef {readonly string[]} ReadonlyStringArray */
|
||||
|
||||
/**
|
||||
* Adds an error for the top or bottom of a code fence.
|
||||
*
|
||||
* @param {import("./markdownlint.mjs").RuleOnError} onError Error-reporting callback.
|
||||
* @param {ReadonlyStringArray} lines Lines of Markdown content.
|
||||
* @param {number} lineNumber Line number.
|
||||
* @param {boolean} top True iff top fence.
|
||||
* @returns {void}
|
||||
*/
|
||||
function addError(onError, lines, lineNumber, top) {
|
||||
const line = lines[lineNumber - 1];
|
||||
const [ , prefix ] = line.match(codeFencePrefixRe) || [];
|
||||
const fixInfo = (prefix === undefined) ?
|
||||
undefined :
|
||||
{
|
||||
"lineNumber": lineNumber + (top ? 0 : 1),
|
||||
"insertText": `${prefix.replace(/[^>]/g, " ").trim()}\n`
|
||||
};
|
||||
addErrorContext(
|
||||
onError,
|
||||
lineNumber,
|
||||
line.trim(),
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
fixInfo
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {import("./markdownlint.mjs").Rule} */
|
||||
export default {
|
||||
"names": [ "MD031", "blanks-around-fences" ],
|
||||
"description": "Fenced code blocks should be surrounded by blank lines",
|
||||
"tags": [ "code", "blank_lines" ],
|
||||
"parser": "micromark",
|
||||
"function": function MD031(params, onError) {
|
||||
const listItems = params.config.list_items;
|
||||
const includeListItems = (listItems === undefined) ? true : !!listItems;
|
||||
const { lines } = params;
|
||||
for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
|
||||
if (includeListItems || !(getParentOfType(codeBlock, [ "listOrdered", "listUnordered" ]))) {
|
||||
if (!isBlankLine(lines[codeBlock.startLine - 2])) {
|
||||
addError(onError, lines, codeBlock.startLine, true);
|
||||
}
|
||||
if (!isBlankLine(lines[codeBlock.endLine]) && !isBlankLine(lines[codeBlock.endLine - 1])) {
|
||||
addError(onError, lines, codeBlock.endLine, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue