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
91
lib/md037.mjs
Normal file
91
lib/md037.mjs
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
// @ts-check
|
||||
|
||||
import { addError } from "../helpers/helpers.cjs";
|
||||
import { filterByPredicate, inHtmlFlow } from "../helpers/micromark-helpers.cjs";
|
||||
|
||||
/** @type {import("./markdownlint.mjs").Rule} */
|
||||
export default {
|
||||
"names": [ "MD037", "no-space-in-emphasis" ],
|
||||
"description": "Spaces inside emphasis markers",
|
||||
"tags": [ "whitespace", "emphasis" ],
|
||||
"parser": "micromark",
|
||||
"function": function MD037(params, onError) {
|
||||
|
||||
// Initialize variables
|
||||
const { lines, parsers } = params;
|
||||
const emphasisTokensByMarker = new Map();
|
||||
for (const marker of [ "_", "__", "___", "*", "**", "***" ]) {
|
||||
emphasisTokensByMarker.set(marker, []);
|
||||
}
|
||||
const tokens = filterByPredicate(
|
||||
parsers.micromark.tokens,
|
||||
(token) => token.children.some((child) => child.type === "data")
|
||||
);
|
||||
for (const token of tokens) {
|
||||
|
||||
// Build lists of bare tokens for each emphasis marker type
|
||||
for (const emphasisTokens of emphasisTokensByMarker.values()) {
|
||||
emphasisTokens.length = 0;
|
||||
}
|
||||
for (const child of token.children) {
|
||||
const { text, type } = child;
|
||||
if ((type === "data") && (text.length <= 3)) {
|
||||
const emphasisTokens = emphasisTokensByMarker.get(text);
|
||||
if (emphasisTokens && !inHtmlFlow(child)) {
|
||||
emphasisTokens.push(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process bare tokens for each emphasis marker type
|
||||
for (const entry of emphasisTokensByMarker.entries()) {
|
||||
const [ marker, emphasisTokens ] = entry;
|
||||
for (let i = 0; i + 1 < emphasisTokens.length; i += 2) {
|
||||
|
||||
// Process start token of start/end pair
|
||||
const startToken = emphasisTokens[i];
|
||||
const startLine = lines[startToken.startLine - 1];
|
||||
const startSlice = startLine.slice(startToken.endColumn - 1);
|
||||
const startMatch = startSlice.match(/^\s+\S/);
|
||||
if (startMatch) {
|
||||
const [ startSpaceCharacter ] = startMatch;
|
||||
const startContext = `${marker}${startSpaceCharacter}`;
|
||||
addError(
|
||||
onError,
|
||||
startToken.startLine,
|
||||
undefined,
|
||||
startContext,
|
||||
[ startToken.startColumn, startContext.length ],
|
||||
{
|
||||
"editColumn": startToken.endColumn,
|
||||
"deleteCount": startSpaceCharacter.length - 1
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Process end token of start/end pair
|
||||
const endToken = emphasisTokens[i + 1];
|
||||
const endLine = lines[endToken.startLine - 1];
|
||||
const endSlice = endLine.slice(0, endToken.startColumn - 1);
|
||||
const endMatch = endSlice.match(/\S\s+$/);
|
||||
if (endMatch) {
|
||||
const [ endSpaceCharacter ] = endMatch;
|
||||
const endContext = `${endSpaceCharacter}${marker}`;
|
||||
addError(
|
||||
onError,
|
||||
endToken.startLine,
|
||||
undefined,
|
||||
endContext,
|
||||
[ endToken.endColumn - endContext.length, endContext.length ],
|
||||
{
|
||||
"editColumn":
|
||||
endToken.startColumn - (endSpaceCharacter.length - 1),
|
||||
"deleteCount": endSpaceCharacter.length - 1
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue