forked from svenslaggare/BBCodeParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbCodeParser.js
More file actions
134 lines (132 loc) · 4.33 KB
/
bbCodeParser.js
File metadata and controls
134 lines (132 loc) · 4.33 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
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
var BBCodeParseTree = require("./bbCodeParseTree")
var BBTag = require("./bbTag");
//Indicates if the first string ends with the second str
function endsWith(str, endStr) {
if (str.length == 0) {
return false;
}
if (endStr.length > str.length) {
return false;
}
var inStrEnd = str.substr(str.length - endStr.length, endStr.length);
return endStr == inStrEnd;
}
//Indicates if the first string starts with the second string
function startsWith(str, startStr) {
if (str.length == 0) {
return false;
}
if (startStr.length > str.length) {
return false;
}
var inStrStart = str.substr(0, startStr.length);
return startStr == inStrStart;
}
var tagsToReplace = {
'&': '&',
'<': '<',
'>': '>'
};
//Escapes the given html
function escapeHTML(html) {
return html.replace(/[&<>]/g, function (tag) {
return tagsToReplace[tag] || tag;
});
}
//Represents a BB code parser
var BBCodeParser = (function () {
//Creates a new parser with the given tags
function BBCodeParser(bbTags) {
this.bbTags = bbTags;
}
//Parses the given string
BBCodeParser.prototype.parseString = function (content, stripTags, insertLineBreak) {
if (stripTags === void 0) { stripTags = false; }
if (insertLineBreak === void 0) { insertLineBreak = true; }
//Create the parse tree
var parseTree = BBCodeParseTree.buildTree(content, this.bbTags);
//If the tree is invalid, return the input as text
if (parseTree == null || !parseTree.isValid()) {
return content;
}
//Convert it to HTML
return this.treeToHtml(parseTree.subTrees, insertLineBreak, stripTags);
};
//Converts the given subtrees into html
BBCodeParser.prototype.treeToHtml = function (subTrees, insertLineBreak, stripTags) {
var _this = this;
if (stripTags === void 0) { stripTags = false; }
var htmlString = "";
var suppressLineBreak = false;
subTrees.forEach(function (currentTree) {
if (currentTree.treeType == 1 /* Text */) {
var textContent = currentTree.content;
textContent = escapeHTML(textContent);
if (insertLineBreak && !suppressLineBreak) {
textContent = textContent.replace(/(\r\n|\n|\r)/gm, "<br>");
suppressLineBreak = false;
}
htmlString += textContent;
}
else {
//Get the tag
var bbTag = _this.bbTags[currentTree.content];
var content = _this.treeToHtml(currentTree.subTrees, bbTag.InsertLineBreaks, stripTags);
//Check if to strip the tags
if (!stripTags) {
htmlString += bbTag.markupGenerator(bbTag, content, currentTree.attributes);
}
else {
htmlString += content;
}
suppressLineBreak = bbTag.suppressLineBreaks;
}
});
return htmlString;
};
//Returns the default tags
BBCodeParser.defaultTags = function () {
var bbTags = new Array();
//Simple tags
bbTags["b"] = new BBTag("b", true, false, false);
bbTags["i"] = new BBTag("i", true, false, false);
bbTags["u"] = new BBTag("u", true, false, false);
bbTags["text"] = new BBTag("text", true, false, true, function (tag, content, attr) {
return content;
});
bbTags["img"] = new BBTag("img", true, false, false, function (tag, content, attr) {
return "<img src=\"" + content + "\" />";
});
bbTags["url"] = new BBTag("url", true, false, false, function (tag, content, attr) {
var link = content;
if (attr["url"] != undefined) {
link = escapeHTML(attr["url"]);
}
if (!startsWith(link, "http://") && !startsWith(link, "https://")) {
link = "http://" + link;
}
return "<a href=\"" + link + "\" target=\"_blank\">" + content + "</a>";
});
bbTags["code"] = new BBTag("code", true, false, true, function (tag, content, attr) {
var lang = attr["lang"];
if (lang !== undefined) {
return "<code class=\"" + escapeHTML(lang) + "\">" + content + "</code>";
}
else {
return "<code>" + content + "</code>";
}
});
return bbTags;
};
BBCodeParser.escapeHTML = function (content) {
return escapeHTML(content);
};
BBCodeParser.startsWith = function (str, startStr) {
return startsWith(str, startStr);
};
BBCodeParser.endsWith = function (str, endStr) {
return endsWith(str, endStr);
};
return BBCodeParser;
})();
module.exports = BBCodeParser;