-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (126 loc) · 3.36 KB
/
index.js
File metadata and controls
144 lines (126 loc) · 3.36 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict'
const keywords = require('./keywords')
const escapeHtml = require('./escapeHtml')
const DEFAULT_OPTIONS = {
html: false,
htmlEscaper: escapeHtml,
classPrefix: 'sql-hl-',
colors: {
keyword: '\x1b[35m',
function: '\x1b[31m',
number: '\x1b[32m',
string: '\x1b[32m',
special: '\x1b[33m',
bracket: '\x1b[33m',
clear: '\x1b[0m'
}
}
const SPLIT_CHARS = '[^a-zA-Z_]'
const DEFAULT_KEYWORD = 'default'
const highlighters = [
{
name: 'keyword',
group: 1,
regex: new RegExp(`(^|${SPLIT_CHARS})(${keywords.join('|')})(?=${SPLIT_CHARS}|$)`, 'gi')
},
{
name: 'special',
regex: /(=|!=|%|\/|\*|-|,|;|:|\+|<|>)/g
},
{
name: 'function',
regex: /(\w+?)\(/g,
trimEnd: 1
},
{
name: 'number',
regex: /(\b\d+(?:\.\d+)?)/g
},
{
name: 'string',
regex: /(['](?:\\'|.)*?[']|["](?:\\"|.)*?["]|[`](?:\\`|.)*?[`])/g
},
{
name: 'bracket',
regex: /([()])/g
}
]
function getSegments (sqlString) {
const matches = []
for (const hl of highlighters) {
let match
// This is probably the one time when an assignment inside a condition makes sense
// eslint-disable-next-line no-cond-assign
while (match = hl.regex.exec(sqlString)) {
let text = match[0]
let boringLength = 0
// If a specific group is requested, use that group instead, and make sure
// we offset the index by the length of the preceding groups
if (hl.group) {
text = match[hl.group + 1]
for (let i = 1; i <= hl.group; i++) {
boringLength += match[i].length
}
}
const trimmedText = hl.trimEnd
? text.substring(0, text.length - hl.trimEnd)
: text
matches.push({
name: hl.name,
start: match.index + boringLength,
length: trimmedText.length
})
}
}
const sortedMatches = matches.slice().sort((a, b) => a.start - b.start)
// filter/exclude nested matches (matches within the last match)
const segments = []
let upperBound = 0
for (let i = 0; i < sortedMatches.length; i++) {
if (sortedMatches[i].start < upperBound) { continue }
// If no match, add a default segment
if (sortedMatches[i].start > upperBound) {
segments.push({
name: DEFAULT_KEYWORD,
content: sqlString.substring(upperBound, sortedMatches[i].start)
})
}
segments.push({
name: sortedMatches[i].name,
content: sqlString.substring(
sortedMatches[i].start,
sortedMatches[i].start + sortedMatches[i].length
)
})
upperBound = sortedMatches[i].start + sortedMatches[i].length
}
if (upperBound < sqlString.length - 1) {
segments.push({
name: DEFAULT_KEYWORD,
content: sqlString.substring(
upperBound,
upperBound + sqlString.length + 1
)
})
}
return segments
}
function highlight (sqlString, options) {
options = Object.assign({}, DEFAULT_OPTIONS, options)
return getSegments(sqlString)
.map(({ name, content }) => {
if (name === DEFAULT_KEYWORD) {
return content
}
if (options.html) {
const escapedContent = options.htmlEscaper(content)
return `<span class="${options.classPrefix}${name}">${escapedContent}</span>`
}
return options.colors[name] + content + options.colors.clear
})
.join('')
}
module.exports = {
getSegments,
highlight
}