-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrule-validate-markdown.js
79 lines (72 loc) · 1.88 KB
/
rule-validate-markdown.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
const markdownlint = require("markdownlint");
const config = {
// the list is here https://github.com/DavidAnson/markdownlint#rules--aliases
MD013: { line_length: 120 },
MD041: false, // first line should be h1
MD047: false, // should end with newline
};
function checkString(description, ctx) {
let options = {
strings: {
desc: description,
},
config: config,
};
try {
const lintResults = markdownlint.sync(options);
if (lintResults.desc.length) {
// desc is the key in the options.strings object
let lines = description.split("\n");
for (const desc of lintResults.desc) {
// grab error message
let message = desc.ruleDescription;
// add line number context for longer entries
if (desc.lineNumber > 1) {
// computer counts from zero, humans count from 1
const charsByError = lines[desc.lineNumber - 1].substring(0, 20);
message = `${message} (near: ${charsByError} ...)`
}
ctx.report({
message: message,
location: ctx.location.child("description"),
});
}
}
} catch (error) {
console.log(error);
}
}
function ValidateMarkdown() {
console.log("OpenAPI Markdown: validate");
return {
Info: {
enter({ description }, ctx) {
if (description) {
return checkString(description, ctx);
}
},
},
Tag: {
enter({ description }, ctx) {
if (description) {
return checkString(description, ctx);
}
},
},
Operation: {
enter({ description }, ctx) {
if (description) {
return checkString(description, ctx);
}
},
},
Parameter: {
enter({ description }, ctx) {
if (description) {
return checkString(description, ctx);
}
},
},
};
}
module.exports = ValidateMarkdown;