mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02: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
77
lib/md019-md021.mjs
Normal file
77
lib/md019-md021.mjs
Normal file
|
@ -0,0 +1,77 @@
|
|||
// @ts-check
|
||||
|
||||
import { addErrorContext } from "../helpers/helpers.cjs";
|
||||
import { getHeadingStyle } from "../helpers/micromark-helpers.cjs";
|
||||
import { filterByTypesCached } from "./cache.mjs";
|
||||
|
||||
/**
|
||||
* Validate heading sequence and whitespace length at start or end.
|
||||
*
|
||||
* @param {import("./markdownlint.mjs").RuleOnError} onError Error-reporting callback.
|
||||
* @param {import("./markdownlint.mjs").MicromarkToken} heading ATX heading token.
|
||||
* @param {number} delta Direction to scan.
|
||||
* @returns {void}
|
||||
*/
|
||||
function validateHeadingSpaces(onError, heading, delta) {
|
||||
const { children, startLine, text } = heading;
|
||||
let index = (delta > 0) ? 0 : (children.length - 1);
|
||||
while (
|
||||
children[index] &&
|
||||
(children[index].type !== "atxHeadingSequence")
|
||||
) {
|
||||
index += delta;
|
||||
}
|
||||
const headingSequence = children[index];
|
||||
const whitespace = children[index + delta];
|
||||
if (
|
||||
(headingSequence?.type === "atxHeadingSequence") &&
|
||||
(whitespace?.type === "whitespace") &&
|
||||
(whitespace.text.length > 1)
|
||||
) {
|
||||
const column = whitespace.startColumn + 1;
|
||||
const length = whitespace.endColumn - column;
|
||||
addErrorContext(
|
||||
onError,
|
||||
startLine,
|
||||
text.trim(),
|
||||
delta > 0,
|
||||
delta < 0,
|
||||
[ column, length ],
|
||||
{
|
||||
"editColumn": column,
|
||||
"deleteCount": length
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {import("./markdownlint.mjs").Rule[]} */
|
||||
export default [
|
||||
{
|
||||
"names": [ "MD019", "no-multiple-space-atx" ],
|
||||
"description": "Multiple spaces after hash on atx style heading",
|
||||
"tags": [ "headings", "atx", "spaces" ],
|
||||
"parser": "micromark",
|
||||
"function": function MD019(params, onError) {
|
||||
const atxHeadings = filterByTypesCached([ "atxHeading" ])
|
||||
.filter((heading) => getHeadingStyle(heading) === "atx");
|
||||
for (const atxHeading of atxHeadings) {
|
||||
validateHeadingSpaces(onError, atxHeading, 1);
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"names": [ "MD021", "no-multiple-space-closed-atx" ],
|
||||
"description": "Multiple spaces inside hashes on closed atx style heading",
|
||||
"tags": [ "headings", "atx_closed", "spaces" ],
|
||||
"parser": "micromark",
|
||||
"function": function MD021(params, onError) {
|
||||
const atxClosedHeadings = filterByTypesCached([ "atxHeading" ])
|
||||
.filter((heading) => getHeadingStyle(heading) === "atx_closed");
|
||||
for (const atxClosedHeading of atxClosedHeadings) {
|
||||
validateHeadingSpaces(onError, atxClosedHeading, 1);
|
||||
validateHeadingSpaces(onError, atxClosedHeading, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
Loading…
Add table
Add a link
Reference in a new issue