-
Notifications
You must be signed in to change notification settings - Fork 4
/
escapers.ts
41 lines (35 loc) · 910 Bytes
/
escapers.ts
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
import { Escaper } from "./types.ts";
/** escapeHTML borrowed from https://github.com/feathers-studio/hyperactive/blob/bbd67beace6744c4b8b48637a96c2daed416ebde/hyper/util.ts */
export const HTML: Escaper = (() => {
const escapables = {
"<": "<",
">": ">",
"&": "&",
};
const toEscape = /<|>|&/g;
return s => s.replace(toEscape, r => escapables[r as keyof typeof escapables] || r);
})();
export const MarkdownV2: Escaper = (() => {
const escapables = {
"_": "\\_",
"*": "\\*",
"[": "\\[",
"]": "\\]",
"(": "\\(",
")": "\\)",
"~": "\\~",
"`": "\\`",
">": "\\>",
"#": "\\#",
"+": "\\+",
"-": "\\-",
"=": "\\=",
"|": "\\|",
"{": "\\{",
"}": "\\}",
".": "\\.",
"!": "\\!",
};
const toEscape = new RegExp("[" + Object.values(escapables).join("") + "]", "g");
return s => s.replace(toEscape, r => escapables[r as keyof typeof escapables] || r);
})();