-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidateMarkdown.ts
More file actions
71 lines (65 loc) · 1.65 KB
/
Copy pathvalidateMarkdown.ts
File metadata and controls
71 lines (65 loc) · 1.65 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
type Validation = {
message: string
validate: (t: string) => boolean
}
const validations: Validation[] = [
{
message: 'should start with a title',
validate: t => !!t.match(/^#\s.+/)
},
{
message: 'should not have multiple `#` headers',
validate: t => !t.match(/[\n\r]#\s/)
},
{
message: 'should have a summary description under the title',
validate: t => {
const [summary] = t.split(/[\n\r]##/) || ['']
const description = summary
.split(/\n/)
.slice(1)
.filter(l => l.length)
return !!description.length
}
},
{
message: 'should have a level `##` with a format of `[0-9]+.`',
validate: t => {
const headers = t.match(/^#{2}\s(.+)$/gm) || []
for (const header of headers) {
if (!header.match(/^#{2}\s(\d+\.)\s(.+)$/)) {
return false
}
}
return true
}
},
{
message: 'should have a step `###` with a format of `[0-9].[0-9]+`',
validate: t => {
const headers = t.match(/^#{3}\s(.+)$/gm) || []
for (const header of headers) {
if (!header.match(/^#{3}\s(\d+\.\d+)/)) {
return false
}
}
return true
}
}
]
const codeBlockRegex = /```[a-z]*\n[\s\S]*?\n```/gm
export function validateMarkdown (md: string): boolean {
// remove codeblocks which might contain any valid combinations
// trim white space
const text = md.replace(codeBlockRegex, '').trim()
let valid = true
for (const v of validations) {
if (!v.validate(text)) {
valid = false
if (process.env.NODE_ENV !== 'test') {
console.warn(v.message)
}
}
}
return valid
}