This repository was archived by the owner on Sep 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathText.js
More file actions
70 lines (66 loc) · 1.85 KB
/
Copy pathText.js
File metadata and controls
70 lines (66 loc) · 1.85 KB
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
const ParagraphComponent = require('./ParagraphComponent');
/**
*/
const Text = class Text extends ParagraphComponent {
/**
* Represents text in a message.
* @extends {ParagraphComponent}
* @param {String} text - The url of the link.
* @param {Array.<'bold'|'spoiler'|'strikethrough'|'italic'|'underline'|'inline-code-v2'>} [content] - The content of the link.
* @see {@link Markdown} for simpler formatting.
*/
constructor(text, formatting = []) {
super();
if (!Array.isArray(formatting)) formatting = [formatting];
this.type = 'text';
this.text = text;
this.formatting = formatting;
}
/**
* Convert to a string.
* @returns {string}
*/
toString(options = {}) {
if (options.text || !this.formatting || !this.formatting[0])
return this.text;
else {
const list = {
bold: '**',
spoiler: '||',
strikethrough: '~~',
italic: '*',
underline: '__',
'inline-code-v2': '`'
};
let res = this.text;
this.formatting.forEach(f => {
let c = list[f];
if(!c) return;
res = c + res + c;
});
return res;
}
}
/**
* Convert to a JSON string like Guilded likes.
* @returns {object}
* @private
*/
toJSON() {
return {
object: 'text',
leaves: [{
object: 'leaf',
text: this.text,
marks: this.formatting.map(type => {
return {
object: 'mark',
type,
data: {}
};
})
}]
};
}
};
module.exports = Text;