-
Notifications
You must be signed in to change notification settings - Fork 1
/
markdown.js
85 lines (73 loc) · 2.2 KB
/
markdown.js
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
const FS = require('fs');
const LOGGER = require('./logging');
const Util = require('./utility');
const PATTERN_USER = /@(\w+)/g;
const PATTERN_COMMENT = /<!--.*-->/g;
var MEMBERS = require('./require/member-binds.json');
class Markdown
{
static ConvertMarkdownToDiscord(sUrl, sInput) {
let sOutput = sInput.replace(PATTERN_USER, function(sMatch, sUsername) {
return getUser(sUrl, sUsername) || sMatch;
});
sOutput = sOutput.replace(PATTERN_COMMENT, () => { return ''; });
return sOutput;
}
static GetMarkdownUrlFiltered(sUrl, sText, bFiltered, sHoverText)
{
return `[${sText}](${(bFiltered === true ? '#' : sUrl)} ${(sHoverText != null && sHoverText.length > 0) ? "'" + sHoverText + "'" : ""})`;
}
static GetUserString(sUrl, sUser)
{
return this.getUser(sUrl, sUser) || Util.Truncate(sUser, Util.StringLimits.USERNAME);
}
static RemoveAllMemberBinds(sID)
{
let arrRemoved = [];
for (var k in MEMBERS) {
if (MEMBERS.hasOwnProperty(k) && MEMBERS[k] == sID) {
arrRemoved.push(k);
delete MEMBERS[k];
}
}
if(arrRemoved.length > 0)
{
this.saveMemberBinds();
}
return arrRemoved;
}
static RemoveMemberBind(sUrl, sID)
{
if (MEMBERS.hasOwnProperty(sUrl)) {
if (MEMBERS[sUrl] == sID) {
delete MEMBERS[sUrl];
this.saveMemberBinds();
return true;
}
}
return false;
}
static getUser(sUrl, sUser)
{
if(sUrl == null || sUser == null)
return;
sUrl = (sUrl+"/"+sUser).toLowerCase();
if(MEMBERS.hasOwnProperty(sUrl))
{
return `<@${MEMBERS[sUrl]}>`;
}
}
static saveMemberBinds()
{
FS.writeFile('./require/member-binds.json', JSON.stringify(MEMBERS), (err) => {
if(err)
{
LOGGER.error("Error occured when saving member binds:", err);
return;
}
LOGGER.log(0, "Member binds have been saved to disk.");
});
}
}
module.exports = Markdown;
module.exports.Members = MEMBERS;