-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
Copy pathmd049-md050.js
86 lines (81 loc) · 2.52 KB
/
md049-md050.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
// @ts-check
"use strict";
const { addError, emphasisOrStrongStyleFor } = require("../helpers");
const { filterByTypes, tokenIfType } = require("../helpers/micromark.cjs");
const intrawordRe = /\w/;
const impl =
(params, onError, type, asterisk, underline, style = "consistent") => {
const { lines, parsers } = params;
const emphasisTokens =
filterByTypes(parsers.micromark.tokens, [ type ]);
for (const token of emphasisTokens) {
const { children } = token;
const childType = `${type}Sequence`;
const startSequence = tokenIfType(children[0], childType);
const endSequence = tokenIfType(children[children.length - 1], childType);
if (startSequence && endSequence) {
const markupStyle = emphasisOrStrongStyleFor(startSequence.text);
if (style === "consistent") {
style = markupStyle;
}
if (style !== markupStyle) {
const underscoreIntraword = (style === "underscore") && (
intrawordRe.test(
lines[startSequence.startLine - 1][startSequence.startColumn - 2]
) ||
intrawordRe.test(
lines[endSequence.endLine - 1][endSequence.endColumn - 1]
)
);
if (!underscoreIntraword) {
for (const sequence of [ startSequence, endSequence ]) {
addError(
onError,
sequence.startLine,
`Expected: ${style}; Actual: ${markupStyle}`,
undefined,
[ sequence.startColumn, sequence.text.length ],
{
"editColumn": sequence.startColumn,
"deleteCount": sequence.text.length,
"insertText": (style === "asterisk") ? asterisk : underline
}
);
}
}
}
}
}
};
module.exports = [
{
"names": [ "MD049", "emphasis-style" ],
"description": "Emphasis style should be consistent",
"tags": [ "emphasis" ],
"function": function MD049(params, onError) {
return impl(
params,
onError,
"emphasis",
"*",
"_",
params.config.style || undefined
);
}
},
{
"names": [ "MD050", "strong-style" ],
"description": "Strong style should be consistent",
"tags": [ "emphasis" ],
"function": function MD050(params, onError) {
return impl(
params,
onError,
"strong",
"**",
"__",
params.config.style || undefined
);
}
}
];