-
Notifications
You must be signed in to change notification settings - Fork 30
/
markdown_heading_capitalization.js
29 lines (26 loc) · 1.3 KB
/
markdown_heading_capitalization.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
module.exports = [{
"names": [ "custom/capitalize-headings" ],
"description": "Heading words longer than 4 characters should be capitalized",
"tags": [ "formatting" ],
"function": function rule(params, onError) {
params.tokens.filter(function filterToken(token) {
return token.type === "heading_open";
}).forEach(function forToken(heading) {
var headingTokenContent = heading.line.trim();
var wordsInHeading = headingTokenContent.split(' ');
for (var i = 0; i < wordsInHeading.length; i++) {
if (wordsInHeading[i].length > 4 && wordsInHeading[i] &&
wordsInHeading[i].charAt(0) !== wordsInHeading[i].charAt(0).toUpperCase()) {
var capitalizedWord = wordsInHeading[i].charAt(0).toUpperCase() + wordsInHeading[i].slice(1);
var detailMessage = "Change " + "'" + wordsInHeading[i] + "'" + " to " + "'" + capitalizedWord + "'";
onError({
"lineNumber": heading.lineNumber,
"detail": detailMessage,
"context": headingTokenContent, // Show the whole heading as context
"range": [headingTokenContent.indexOf(wordsInHeading[i]), wordsInHeading[i].length] // Underline the word which needs a change
});
}
}
});
}
}];