-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserialisers.ts
75 lines (73 loc) · 1.92 KB
/
serialisers.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
import type { Serialiser, Node } from "./types.ts";
export const HTML: Serialiser = (match: string, node?: Node) => {
switch (node?.type) {
case "bold":
return `<b>${match}</b>`;
case "italic":
return `<i>${match}</i>`;
case "underline":
return `<u>${match}</u>`;
case "strikethrough":
return `<del>${match}</del>`;
case "code":
return `<code>${match}</code>`;
case "pre":
if (node.language) return `<pre><code class="language-${node.language}">${match}</code></pre>`;
return `<pre>${match}</pre>`;
case "spoiler":
return `<span class="tg-spoiler">${match}</span>`;
case "url":
return `<a href="${node.text}">${match}</a>`;
case "text_link":
return `<a href="${node.url}">${match}</a>`;
case "text_mention":
return `<a href="tg://user?id=${node.user.id}">${match}</a>`;
case "blockquote":
return `<blockquote>${match}</blockquote>`;
case "mention":
case "custom_emoji":
case "hashtag":
case "cashtag":
case "bot_command":
case "phone_number":
case "email":
default:
return match;
}
};
export function MarkdownV2(match: string, node?: Node): string {
switch (node?.type) {
case "bold":
return `*${match}*`;
case "italic":
return `_${match}_`;
case "underline":
return `__${match}__`;
case "strikethrough":
return `~${match}~`;
case "code":
return `\`${match}\``;
case "pre":
if (node.language) return "```" + node.language + "\n" + match + "\n```";
return "```\n" + match + "\n```";
case "spoiler":
return `||${match}||`;
case "url":
return match;
case "text_link":
return `[${match}](${node.url})`;
case "text_mention":
return `[${match}](tg://user?id=${node.user.id})`;
case "blockquote":
return `${match.split("\n").map((line) => `>${line}`).join("\n")}`;
case "mention":
case "custom_emoji":
case "hashtag":
case "cashtag":
case "bot_command":
case "phone_number":
case "email":
default:
return match;
}
}