aboutsummaryrefslogtreecommitdiff
path: root/src/lib/common/util/Format.ts
blob: debaf4b5b8e2662c2a88e16a7699bde159a2498b (plain)
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
import { type CodeBlockLang } from '#lib';
import {
	bold as discordBold,
	codeBlock as discordCodeBlock,
	escapeBold as discordEscapeBold,
	escapeCodeBlock as discordEscapeCodeBlock,
	escapeInlineCode as discordEscapeInlineCode,
	escapeItalic as discordEscapeItalic,
	escapeMarkdown,
	escapeSpoiler as discordEscapeSpoiler,
	escapeStrikethrough as discordEscapeStrikethrough,
	escapeUnderline as discordEscapeUnderline,
	inlineCode as discordInlineCode,
	italic as discordItalic,
	spoiler as discordSpoiler,
	strikethrough as discordStrikethrough,
	underscore as discordUnderscore
} from 'discord.js';

/**
 * Wraps the content inside a codeblock with no language.
 * @param content The content to wrap.
 */
export function codeBlock(content: string): string;

/**
 * Wraps the content inside a codeblock with the specified language.
 * @param language The language for the codeblock.
 * @param content The content to wrap.
 */
export function codeBlock(language: CodeBlockLang, content: string): string;
export function codeBlock(languageOrContent: string, content?: string): string {
	return typeof content === 'undefined'
		? discordCodeBlock(discordEscapeCodeBlock(`${languageOrContent}`))
		: discordCodeBlock(`${languageOrContent}`, discordEscapeCodeBlock(`${content}`));
}

/**
 * Wraps the content inside \`backticks\`, which formats it as inline code.
 * @param content The content to wrap.
 */
export function inlineCode(content: string): string {
	return discordInlineCode(discordEscapeInlineCode(`${content}`));
}

/**
 * Formats the content into italic text.
 * @param content The content to wrap.
 */
export function italic(content: string): string {
	return discordItalic(discordEscapeItalic(`${content}`));
}

/**
 * Formats the content into bold text.
 * @param content The content to wrap.
 */
export function bold(content: string): string {
	return discordBold(discordEscapeBold(`${content}`));
}

/**
 * Formats the content into underscored text.
 * @param content The content to wrap.
 */
export function underscore(content: string): string {
	return discordUnderscore(discordEscapeUnderline(`${content}`));
}

/**
 * Formats the content into strike-through text.
 * @param content The content to wrap.
 */
export function strikethrough(content: string): string {
	return discordStrikethrough(discordEscapeStrikethrough(`${content}`));
}

/**
 * Wraps the content inside spoiler (hidden text).
 * @param content The content to wrap.
 */
export function spoiler(content: string): string {
	return discordSpoiler(discordEscapeSpoiler(`${content}`));
}

/**
 * Formats input: makes it bold and escapes any other markdown
 * @param text The input
 */
export function input(text: string): string {
	return bold(sanitizeInputForDiscord(`${text}`));
}

/**
 * Formats input for logs: makes it highlighted
 * @param text The input
 */
export function inputLog(text: string): string {
	return `<<${sanitizeWtlAndControl(`${text}`)}>>`;
}

/**
 * Removes all characters in a string that are either control characters or change the direction of text etc.
 * @param str The string you would like sanitized
 */
export function sanitizeWtlAndControl(str: string) {
	// eslint-disable-next-line no-control-regex
	return `${str}`.replace(/[\u0000-\u001F\u007F-\u009F\u200B]/g, '');
}

/**
 * Removed wtl and control characters and escapes any other markdown
 * @param text The input
 */
export function sanitizeInputForDiscord(text: string): string {
	return escapeMarkdown(sanitizeWtlAndControl(`${text}`));
}

export { escapeMarkdown } from 'discord.js';