-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathhelpers.ts
More file actions
166 lines (147 loc) · 3.7 KB
/
Copy pathhelpers.ts
File metadata and controls
166 lines (147 loc) · 3.7 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { other } from './rules.ts';
/**
* Helpers
*/
const escapeReplacements: { [index: string]: string } = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
const getEscapeReplacement = (ch: string) => escapeReplacements[ch];
export function escapeHtmlEntities(html: string, encode?: boolean) {
if (encode) {
if (other.escapeTest.test(html)) {
return html.replace(other.escapeReplace, getEscapeReplacement);
}
} else {
if (other.escapeTestNoEncode.test(html)) {
return html.replace(other.escapeReplaceNoEncode, getEscapeReplacement);
}
}
return html;
}
export function cleanUrl(href: string) {
try {
href = encodeURI(href).replace(other.percentDecode, '%');
} catch {
return null;
}
return href;
}
export function splitCells(tableRow: string, count?: number) {
// ensure that every cell-delimiting pipe has a space
// before it to distinguish it from an escaped pipe
const row = tableRow.replace(other.findPipe, (match, offset, str) => {
let escaped = false;
let curr = offset;
while (--curr >= 0 && str[curr] === '\\') escaped = !escaped;
if (escaped) {
// odd number of slashes means | is escaped
// so we leave it alone
return '|';
} else {
// add space before unescaped |
return ' |';
}
}),
cells = row.split(other.splitPipe);
let i = 0;
// First/last cell in a row cannot be empty if it has no leading/trailing pipe
if (!cells[0].trim()) {
cells.shift();
}
if (cells.length > 0 && !cells.at(-1)?.trim()) {
cells.pop();
}
if (count) {
if (cells.length > count) {
cells.splice(count);
} else {
while (cells.length < count) cells.push('');
}
}
for (; i < cells.length; i++) {
// leading or trailing whitespace is ignored per the gfm spec
cells[i] = cells[i].trim().replace(other.slashPipe, '|');
}
return cells;
}
/**
* Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
* /c*$/ is vulnerable to REDOS.
*
* @param str
* @param c
* @param invert Remove suffix of non-c chars instead. Default falsey.
*/
export function rtrim(str: string, c: string, invert?: boolean) {
const l = str.length;
if (l === 0) {
return '';
}
// Length of suffix matching the invert condition.
let suffLen = 0;
// Step left until we fail to match the invert condition.
while (suffLen < l) {
const currChar = str.charAt(l - suffLen - 1);
if (currChar === c && !invert) {
suffLen++;
} else if (currChar !== c && invert) {
suffLen++;
} else {
break;
}
}
return str.slice(0, l - suffLen);
}
export function trimTrailingBlankLines(str: string) {
const lines = str.split('\n');
let end = lines.length - 1;
while (end >= 0 && other.blankLine.test(lines[end])) {
end--;
}
if (lines.length - end <= 2) {
// we want to keep single trailing blank lines
return str;
}
return lines.slice(0, end + 1).join('\n');
}
export function findClosingBracket(str: string, b: string) {
if (str.indexOf(b[1]) === -1) {
return -1;
}
let level = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === '\\') {
i++;
} else if (str[i] === b[0]) {
level++;
} else if (str[i] === b[1]) {
level--;
if (level < 0) {
return i;
}
}
}
if (level > 0) {
return -2;
}
return -1;
}
export function expandTabs(line: string, indent = 0) {
let col = indent;
let expanded = '';
for (const char of line) {
if (char === '\t') {
const added = 4 - (col % 4);
expanded += ' '.repeat(added);
col += added;
} else {
expanded += char;
col++;
}
}
return expanded;
}