-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathmarkdown_heading_capitalization.js
More file actions
51 lines (49 loc) · 1.97 KB
/
markdown_heading_capitalization.js
File metadata and controls
51 lines (49 loc) · 1.97 KB
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
// This file is used to trigger the custom rule that checks if all markdown headings (words longer than 4 characters) are written in the title case. To run this check, you must include the check in the markdownlint command.
// For example, if you want to run the check on the `docs` folder, run the following command: `markdownlint -r ./markdown_heading_capitalization.js docs/`.
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
});
}
}
});
},
},
];