forked from DavidAnson/markdownlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd037.js
94 lines (86 loc) · 3.25 KB
/
md037.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// @ts-check
"use strict";
const { addError } = require("../helpers");
const { filterByPredicate, inHtmlFlow } = require("../helpers/micromark.cjs");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
module.exports = {
"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
}
);
}
}
}
}
}
};