forked from DavidAnson/markdownlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd049-md050.js
103 lines (98 loc) · 3.24 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// @ts-check
"use strict";
const { addError, emphasisOrStrongStyleFor } = require("../helpers");
const { filterByPredicate, tokenIfType } = require("../helpers/micromark.cjs");
const intrawordRe = /^\w$/;
/**
* @param {import("./markdownlint").RuleParams} params Rule parameters.
* @param {import("./markdownlint").RuleOnError} onError Error-reporting callback.
* @param {import("markdownlint-micromark").TokenType} type Token type.
* @param {import("markdownlint-micromark").TokenType} typeSequence Token sequence type.
* @param {"*" | "**"} asterisk Asterisk kind.
* @param {"_" | "__"} underline Underline kind.
* @param {"asterisk" | "consistent" | "underscore"} style Style string.
*/
const impl =
(params, onError, type, typeSequence, asterisk, underline, style = "consistent") => {
const { lines, parsers } = params;
const emphasisTokens = filterByPredicate(
parsers.micromark.tokens,
(token) => token.type === type,
(token) => ((token.type === "htmlFlow") ? [] : token.children)
);
for (const token of emphasisTokens) {
const { children } = token;
const startSequence = tokenIfType(children[0], typeSequence);
const endSequence = tokenIfType(children[children.length - 1], typeSequence);
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
}
);
}
}
}
}
}
};
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule[] */
module.exports = [
{
"names": [ "MD049", "emphasis-style" ],
"description": "Emphasis style",
"tags": [ "emphasis" ],
"parser": "micromark",
"function": function MD049(params, onError) {
return impl(
params,
onError,
"emphasis",
"emphasisSequence",
"*",
"_",
params.config.style || undefined
);
}
},
{
"names": [ "MD050", "strong-style" ],
"description": "Strong style",
"tags": [ "emphasis" ],
"parser": "micromark",
"function": function MD050(params, onError) {
return impl(
params,
onError,
"strong",
"strongSequence",
"**",
"__",
params.config.style || undefined
);
}
}
];