-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.js
256 lines (231 loc) · 7.72 KB
/
rule.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// @ts-check
"use strict";
const helpers = require("markdownlint-rule-helpers");
const escapeForRegExp = (str) => str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
/**
* Converts string "/abc/ig" to new RegEx("abc", "ig").
* Or
* Converts string "abc" to new RegEx("abc", "g").
*
* @param {string} str A regex string.
* @returns {RegExp} A RegExp object.
*/
const stringToRegex = (str) => {
const pattern = (str.match(/\/(.+)\/.*/) || ["", "a^"])[1];
const flags = (str.match(/\/.+\/(.*)/) || ["", "g"])[1];
return new RegExp(pattern, flags);
};
/**
* Given position in the document returns line and column number.
*
* @param {number} pos Position in the document.
* @param {string[]} lines An array of lines.
* @returns {number[]} Line number and column of the position.
*/
const getLocation = (pos, lines) => {
let lineNo = 0;
for (const line of lines) {
pos -= line.length + 1;
if (pos < 0) {
return [lineNo, line.length + 1 + pos];
}
lineNo++;
}
return [lines.length, 0];
};
/**
* Collect start and end position data of all code fences and html comments.
*
* @param {string} content Entire document text.
* @param {string[]} lines An array of lines in the document.
* @returns {number[][]} An array of tuple [lineNo, column, length].
*/
const gatHtmlCommentRanges = (content, lines) => {
const regex = /<!--[.\n\s]*-->/gm;
const ranges = [];
let match = null;
while ((match = regex.exec(content)) !== null) {
const pos = getLocation(match.index, lines);
if (match[0].includes("\n")) {
const parts = match[0].split("\n");
for (const [i, part] of parts.entries()) {
if (i === 0) {
ranges.push([...pos, part.length]);
} else {
ranges.push([pos[0] + i, 0, part.length]);
}
}
} else {
ranges.push([...pos, match[0].length]);
}
}
return ranges;
};
/**
* Tells if the position lies inside any range.
*
* @param {number} pos Position in the document.
* @param {number[][]} ranges Array of tuple [lineNo, column, length].
* @param {string[]} lines Array of lines.
* @returns {boolean} Whether the position lies in any range.
*/
const isPartOf = (pos, ranges, lines) => {
for (const [rLine, rColumn, rLength] of ranges) {
const [line, column] = getLocation(pos, lines);
if (rLine === line && rColumn <= column && column <= rColumn + rLength) {
return true;
}
}
return false;
};
/**
* Tells if the position lies inside a code block.
*
* @param {number} pos Position in the document.
* @param {number[][]} ranges Array of tuple [lineNo, column, length].
* @param {string[]} lines Array of lines.
* @returns {boolean} Whether the position lies in any range.
*/
const isCode = (pos, ranges, lines) => isPartOf(pos, ranges, lines);
/**
* Tells if the position lies inside an HTML comment.
*
* @param {number} pos Position in the document.
* @param {number[][]} ranges Array of tuple [lineNo, column, length].
* @param {string[]} lines Array of lines.
* @returns {boolean} Whether the position lies in any range.
*/
const isHTMLComment = (pos, ranges, lines) => isPartOf(pos, ranges, lines);
/**
* Check rule definition.
*
* @param {Object} rule A rule object.
* @throws {Error} The error in rule definition.
*/
const validateAndUpdateRule = (rule) => {
if (!rule.search && !rule.searchPattern) {
throw new Error("Provide either `search` or `searchPattern` option.");
}
if (rule.searchScope !== undefined) {
if (rule.skipCode !== undefined) {
throw new Error(
"Both `searchScope` and `skipCode` specified, `skipCode` is deprecated, use `searchScope` instead.",
);
}
if (!["all", "code", "text"].includes(rule.searchScope)) {
throw new Error(
`Invalid value \`${rule.searchScope}\` provided for \`searchScope\`, must be one of \`all\`, \`code\` or \`text\`.`,
);
}
}
if (rule.information !== undefined) {
try {
rule.information = new URL(rule.information);
} catch {
throw new Error(`Provide valid 'information' URL: ${rule.information}`);
}
}
};
module.exports = {
names: ["search-replace"],
description: "Custom rule",
information: new URL(
"https://github.com/OnkarRuikar/markdownlint-rule-search-replace",
),
tags: ["replace"],
function: (params, onError) => {
const report = (rule, match, lineNo, columnNo, replacement) => {
const options = {
lineNumber: lineNo + 1,
detail: rule.name + ": " + rule.message,
context: `column: ${columnNo + 1} text:'${match}'`,
range: [columnNo + 1, match.length],
information: rule.information,
};
if (replacement !== undefined && replacement !== null) {
options.fixInfo = {
lineNumber: lineNo + 1,
editColumn: columnNo + 1,
deleteCount: match.length,
insertText: replacement,
};
}
onError(options);
};
if (!params.config.rules) {
return;
}
let rules = params.config.rules;
const content = params.lines.join("\n");
const lineMetadata = helpers.getLineMetadata(params);
const codeRanges = helpers.codeBlockAndSpanRanges(params, lineMetadata);
const htmlCommentRanges = gatHtmlCommentRanges(content, params.lines);
// expand rules with multiple values
const listRules = [];
for (const rule of rules) {
if (
(rule.search && Array.isArray(rule.search)) ||
(rule.searchPattern && Array.isArray(rule.searchPattern))
) {
listRules.push(rule);
}
}
rules = rules.filter((r) => !listRules.includes(r));
for (const rule of listRules) {
if (rule.search) {
for (const [index, word] of rule.search.entries()) {
const clone = { ...rule };
clone.search = word;
clone.replace = rule.replace[index];
rules.push(clone);
}
} else if (rule.searchPattern) {
for (const [index, pattern] of rule.searchPattern.entries()) {
const clone = { ...rule };
clone.searchPattern = pattern;
clone.replace = rule.replace[index];
rules.push(clone);
}
}
}
for (const rule of rules) {
validateAndUpdateRule(rule);
const regex = rule.search
? new RegExp(escapeForRegExp(rule.search), "g")
: stringToRegex(rule.searchPattern);
let result = null;
while ((result = regex.exec(content)) !== null) {
if (isCode(result.index, codeRanges, params.lines)) {
if (rule.skipCode || rule.searchScope === "text") continue;
} else if (rule.searchScope === "code") continue;
if (isHTMLComment(result.index, htmlCommentRanges, params.lines)) {
continue;
}
const match = result[0];
const [lineNo, columnNo] = getLocation(result.index, params.lines);
// The parent project 'markdownlint' processes markdown line by line.
// It doesn't allow multiline fixes. The line ending('\n') can't be removed from plugins.
// That is why when the 'match' is multiline we can't suggest fix for it.
// However, we can report error for each line.
if (match.includes("\n")) {
const parts = match.split("\n");
for (const [i, part] of parts.entries()) {
if (i === 0) {
report(rule, part, lineNo, columnNo);
} else {
report(rule, part, lineNo + i, 0);
}
}
} else {
let replacement = null;
if (rule.replace !== undefined && rule.replace !== null) {
replacement = rule.search
? rule.replace
: match.replace(new RegExp(regex), rule.replace);
}
report(rule, match, lineNo, columnNo, replacement);
}
}
}
},
};