-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
182 lines (147 loc) · 4.69 KB
/
index.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { escapeMarkdown } from "discord.js";
import XRegExp from "xregexp";
import compileKey, { CompiledReplacer, Replacer } from "./compile.js";
export interface IReplaceSource {
format?: string;
help: string;
name: string;
insensitive?: boolean;
prefix: string;
escape?: string;
replacers: Replacer[];
}
interface IMatcher {
keys: CompiledReplacer[];
join(parts: string[]): string;
}
export default class X2IMatcher {
/**
* The alphabet list, for the x/alphabets command.
*/
public alphabetList: string;
/**
* Create a callback that joins a result based on "template" string ($0 for global, $1, $2, ...)
*/
private static joinResult(format: string) {
return (parts: string[]) => format.replace(/\$(\d+)/g, (m, indexStr) => {
const index = parseInt(indexStr, 10);
if (index === 0) {
return parts.join("");
}
return parts[index];
});
}
private inlineRegex?: RegExp;
private tableRegex?: RegExp;
private replacers: { [k: string]: IMatcher; } = {};
constructor(matcherSources: IReplaceSource[] = []) {
this.alphabetList = "";
for (const source of matcherSources) {
// an empty file ends up being `undefined`
if (!source) continue;
this.register(source);
this.alphabetList += source.name + ": `" + source.prefix + "/text/` or `" + source.prefix +
"[text]`: <" + source.help + ">\n";
}
this.alphabetList = this.alphabetList.trimEnd();
}
private decode(key: string, match: string, left: string, right: string) {
if (!key) return;
const lowerKey = key.toLowerCase();
const { keys, join } = this.replacers[lowerKey];
const parts = [left, XRegExp.replaceEach(match, keys), right];
return join(parts);
}
private decodeTable(key: string, match: string) {
if (!key) return;
const lowerKey = key.toLowerCase();
const { keys } = this.replacers[lowerKey];
return "```\n" + XRegExp.replaceEach(match, keys) + "\n```";
}
public register(notation: IReplaceSource) {
const matcher: IMatcher = {
join: notation.format ? X2IMatcher.joinResult(notation.format) : parts => parts.join(""),
keys: notation.replacers.map(el => compileKey(el, notation.insensitive, notation.escape)),
};
// remove the escape character if applicable
if (notation.escape) {
const escapedEscape = XRegExp.escape(notation.escape);
matcher.keys.push([
XRegExp(`(?<!${escapedEscape})${escapedEscape}`),
"",
"all",
], [
XRegExp(`${escapedEscape}${escapedEscape}`),
notation.escape,
"all",
]);
}
this.replacers[notation.prefix] = matcher;
}
public search(content: string) {
const results: string[] = [];
if (!this.inlineRegex) {
this.inlineRegex = XRegExp(`
# must be preceded by whitespace or surrounded by markdown punctuation, or on its own line
(?<=(^|\\p{White_Space}|(?:^|[^\\\\])[\`*~_]))
# ($2) key
(${Object.keys(this.replacers).map(XRegExp.escape).join("|")})?
# consumes non-tagged brackets to avoid reading the insides accidentally
# ($3) bracket left
([/[])
# ($4) body
(
\\S # single character (eg x/t/)
|\\S.*?[^_\\p{White_Space}] # any characters not surrounded by whitespace, ignores _/
)
# ($5) bracket right
([/\\]])
# must be followed by a white space or punctuation (lookahead), not including a bracket
(?=$|[^\\pL\\pN/[\\]])
`, "gimux");
}
if (!this.tableRegex) {
this.tableRegex = XRegExp(`
# must be preceded by whitespace or surrounded by markdown punctuation, or starting the message
(?<=(^|\\p{White_Space}|(?:^|[^\\\\])[\`*~_]))
# ($2) key
(${Object.keys(this.replacers).map(XRegExp.escape).join("|")})?
# consumes non-tagged brackets to avoid reading the insides accidentally
# bracket left
[/[]
# start table
\\n?\`\`\`\\n
# ($3) body
((?:\`{0,2}[^\`])*)
# don't allow three backticks in a row
# ending table
\\n?\`\`\`\\n?
# bracket right
[/\\]]
# must be followed by a white space or punctuation (lookahead)
(?=$|[^\\pL\\pN])
`, "giusx");
}
XRegExp.forEach(content, this.inlineRegex, match => {
const parts = match.slice(2, 6);
if (parts.length === 4) {
const [k, l, m, r] = parts;
const converted = this.decode(k, m, l, r); // eg x, text, [, ]
if (converted) {
results.push(escapeMarkdown(converted));
}
}
});
XRegExp.forEach(content, this.tableRegex, match => {
const parts = match.slice(2, 4);
if (parts.length === 2) {
const [k, m] = parts;
const converted = this.decodeTable(k, m); // eg x, text
if (converted) {
results.push(converted);
}
}
});
return results;
}
}