-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
152 lines (138 loc) · 3.66 KB
/
mod.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
import type {
Context,
MessageEntity,
MiddlewareFn,
NextFunction,
} from "@mtkruto/mtkruto";
export interface Stringable {
toString(): string;
}
class FormattedString implements Stringable {
text: string;
entities: MessageEntity[];
constructor(text: string, entities: MessageEntity[]) {
this.text = text;
this.entities = entities;
}
toString(): string {
return this.text;
}
}
const unwrap = (stringLike: Stringable): FormattedString => {
if (stringLike instanceof FormattedString) {
return stringLike;
}
return new FormattedString(stringLike.toString(), []);
};
// deno-lint-ignore no-explicit-any
export type Formatter<T extends Array<any> = any> = (
stringLike: Stringable,
...formatArgs: T
) => FormattedString;
// deno-lint-ignore no-explicit-any
const buildFormatter = <T extends Array<any> = any>(
type: MessageEntity["type"],
...formatArgKeys: string[]
): Formatter<T> => {
return (stringLike: Stringable, ...formatArgs: T) => {
const formattedString = unwrap(stringLike);
const formatArgObj = Object.fromEntries(
formatArgKeys.map((formatArgKey, i) => [formatArgKey, formatArgs[i]]),
);
return new FormattedString(
formattedString.text,
[
{
...formatArgObj,
offset: 0,
length: formattedString.text.length,
type,
} as unknown as MessageEntity,
...formattedString.entities,
],
);
};
};
// Native entity functions
const bold: Formatter = buildFormatter("bold");
const italic: Formatter = buildFormatter("italic");
const code: Formatter = buildFormatter("code");
const pre: Formatter<[string]> = buildFormatter<[string]>("pre", "language");
const link: Formatter<[string]> = buildFormatter<[string]>("textLink", "url");
const underline: Formatter = buildFormatter("underline");
const strikethrough: Formatter = buildFormatter("strikethrough");
const blockquote: Formatter<[boolean]> = (
stringLike: Stringable,
collapsible = false,
) =>
buildFormatter<[boolean]>("blockquote", "collapsible")(
stringLike,
collapsible,
);
const spoiler: Formatter = buildFormatter("spoiler");
const emoji: Formatter = buildFormatter("customEmoji");
// Utility functions
const mentionUser: Formatter<[number]> = (
stringLike: Stringable,
userId: number,
) => {
return link(stringLike, `tg://user?id=${userId}`);
};
// Root format function
const fmt = (
rawStringParts: TemplateStringsArray | string[],
...stringLikes: Stringable[]
): FormattedString => {
let text = rawStringParts[0];
const entities = new Array<MessageEntity>();
for (let i = 0; i < stringLikes.length; i++) {
const stringLike = stringLikes[i];
if (stringLike instanceof FormattedString) {
entities.push(
...stringLike.entities.map((e) => {
e.offset += text.length;
return e;
}),
);
}
text += stringLike.toString();
text += rawStringParts[i + 1];
}
return new FormattedString(text, entities);
};
export const replyFmt: MiddlewareFn<Context & ReplyFmt> = (
ctx: Context & ReplyFmt,
next: NextFunction,
) => {
ctx.replyFmt = (stringLike, params) => {
const entities = stringLike instanceof FormattedString
? stringLike.entities
: [];
return ctx.reply(stringLike.toString(), {
...params,
entities: (params?.entities ?? []).concat(entities),
});
};
return next();
};
export interface ReplyFmt {
replyFmt(
text: Stringable,
params?: Parameters<Context["reply"]>[1],
): ReturnType<Context["reply"]>;
}
export {
blockquote,
bold,
code,
emoji,
fmt,
FormattedString,
italic,
link,
mentionUser,
pre,
spoiler,
strikethrough,
underline,
};