-
Notifications
You must be signed in to change notification settings - Fork 61.6k
/
Copy pathtable-liquid-versioning.js
83 lines (78 loc) · 3.03 KB
/
table-liquid-versioning.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
import { addError, filterTokens } from 'markdownlint-rule-helpers'
// Detects a Markdown table delimiter row
const delimiterRegexPure = /(\s)*(:)?(-+)(:)?(\s)*(\|)/
// Detects a Markdown table delimiter row with a Liquid tag
const delimiterRegex = /(\s)*(:)?(-+)(:)?(\s)*(\|).*({%.*(ifversion|else|endif).*%})/
// Detects a Liquid versioning tag
const liquidRegex = /^{%-?\s*(ifversion|else|endif).*-?%}/
// Detects a Markdown table row with a Liquid versioning tag
const liquidAfterRowRegex = /(\|{1}).*(\|{1}).*{%\s*(ifversion|else|endif).*%}$/
export const tableLiquidVersioning = {
names: ['GHD040', 'table-liquid-versioning'],
description: 'Tables must use the correct liquid versioning format',
severity: 'error',
tags: ['tables'],
information: new URL('https://github.com/github/docs/blob/main/src/content-linter/README.md'),
function: function GHD040(params, onError) {
const lines = params.lines
let inTable = false
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
if (inTable && (!line || isPreviousLineIndented(lines[i], lines[i - 1]))) {
inTable = false
continue
}
if (delimiterRegexPure.test(line)) {
// A table with rows is at least 3 lines
if (lines[i - 1] && lines[i + 1]) {
inTable = true
if (liquidAfterRowRegex.test(lines[i - 1])) {
addError(
onError,
i,
'Liquid conditionals that version rows of data should be placed on their own line in the format `| {% ifversion enterprise %} |`.',
lines[i - 1],
null,
)
}
if (delimiterRegex.test(line)) {
addError(
onError,
i + 1,
'Liquid conditionals that version rows of data should be placed on their own line in the format `| {% ifversion enterprise %} |`.',
line,
null,
)
}
continue
}
}
if (inTable) {
if (liquidRegex.test(line)) {
addError(
onError,
i + 1,
'Liquid conditionals that version rows of data should be placed on their own line in the format `| {% ifversion enterprise %} |`. If the conditional is on its own line but is not related to the table, ensure there is one new line beween a Liquid version tag and the table.',
line,
null,
)
}
if (liquidAfterRowRegex.test(line)) {
addError(
onError,
i + 1,
'Liquid conditionals that version rows of data should be placed on their own line in the format `| {% ifversion enterprise %} |`.',
line,
null,
)
}
}
}
},
}
function isPreviousLineIndented(line, previousLine) {
if (!line || !previousLine) return false
const numWhitespaceLine = line.length - line.trimLeft().length
const numWhitespacePrevLine = previousLine.length - previousLine.trimLeft().length
return numWhitespaceLine < numWhitespacePrevLine
}