UNPKG

@panzer1119/bbcode-parser

Version:
160 lines 7.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BBCodeParser = exports.escapeHTML = exports.tagsToReplace = exports.startsWith = exports.endsWith = void 0; //Indicates if the first string ends with the second str var bbtag_1 = require("./bbtag"); var bbcodeparsetree_1 = require("./bbcodeparsetree"); 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; } exports.endsWith = endsWith; //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; } exports.startsWith = startsWith; exports.tagsToReplace = { "&": "&amp;", "<": "&lt;", ">": "&gt;", }; //Escapes the given html function escapeHTML(html) { return html.replace(/[&<>]/g, function (tag) { return exports.tagsToReplace[tag] || tag; }); } exports.escapeHTML = escapeHTML; var BBCodeParser = /** @class */ (function () { function BBCodeParser(bbTags, options) { if (options === void 0) { options = { escapeHTML: false }; } this.bbTags = bbTags; this.options = options; } //Parses the given string BBCodeParser.prototype.parseString = function (content, stripTags, insertLineBreak, escapingHtml) { if (stripTags === void 0) { stripTags = false; } if (insertLineBreak === void 0) { insertLineBreak = true; } if (escapingHtml === void 0) { escapingHtml = true; } //Create the parse tree var parseTree = bbcodeparsetree_1.BBCodeParseTree.buildTree(content, this.bbTags); //If the tree is invalid, return the input as text if (!parseTree || !parseTree.isValid()) { return content; } //Convert it to HTML return this.treeToHtml(parseTree.subTrees, insertLineBreak, escapingHtml, stripTags); }; //Converts the given subtrees into html BBCodeParser.prototype.treeToHtml = function (subTrees, insertLineBreak, escapingHtml, stripTags) { var _this = this; if (stripTags === void 0) { stripTags = false; } var htmlString = ""; var suppressLineBreak = false; subTrees.forEach(function (currentTree) { if (currentTree.treeType === bbcodeparsetree_1.TreeType.Text) { var textContent = currentTree.content; if (escapingHtml) { textContent = _this.options.escapeHTML ? escapeHTML(textContent) : 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, escapingHtml, 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 = {}; //Simple tags bbTags["b"] = new bbtag_1.BBTag("b", true, false, false); bbTags["i"] = new bbtag_1.BBTag("i", true, false, false); bbTags["u"] = new bbtag_1.BBTag("u", true, false, false); bbTags["h1"] = bbtag_1.BBTag.createSimpleTag("h1"); bbTags["h2"] = bbtag_1.BBTag.createSimpleTag("h2"); bbTags["h3"] = bbtag_1.BBTag.createSimpleTag("h3"); bbTags["h4"] = bbtag_1.BBTag.createSimpleTag("h4"); bbTags["h5"] = bbtag_1.BBTag.createSimpleTag("h5"); bbTags["hr"] = bbtag_1.BBTag.createSimpleTag("hr"); bbTags["table"] = bbtag_1.BBTag.createSimpleTag("table"); bbTags["tr"] = bbtag_1.BBTag.createSimpleTag("tr"); bbTags["th"] = bbtag_1.BBTag.createSimpleTag("th"); bbTags["td"] = bbtag_1.BBTag.createSimpleTag("td"); bbTags["spoiler"] = new bbtag_1.BBTag("spoiler", true, false, false, function (tag, content, attributes) { return "<details><summary>" + (attributes["spoiler"] || "Spoiler") + "</summary>" + content + "</details>"; }); bbTags["noparse"] = new bbtag_1.BBTag("noparse", true, false, true); bbTags["strike"] = bbtag_1.BBTag.createSimpleHTMLTag("strike", "s"); bbTags["list"] = bbtag_1.BBTag.createSimpleHTMLTag("olist", "ul"); bbTags["olist"] = bbtag_1.BBTag.createSimpleHTMLTag("olist", "ol"); bbTags["*"] = bbtag_1.BBTag.createSimpleHTMLTag("*", "li"); bbTags["text"] = new bbtag_1.BBTag("text", true, false, true, function (tag, content) { return content; }); bbTags["img"] = new bbtag_1.BBTag("img", true, false, false, function (tag, content) { return "<img src=\"" + content + "\" alt=\"" + content + "\"/>"; }); bbTags["quote"] = new bbtag_1.BBTag("quote", true, false, false, function (tag, content, attributes) { var address = attributes["quote"] ? "<address>" + attributes["quote"] + "</address>" : ""; return "<blockquote>" + address + content + "</blockquote>"; }); bbTags["url"] = new bbtag_1.BBTag("url", true, false, false, function (tag, content, attributes) { var link = content; if (attributes["url"]) { link = escapeHTML(attributes["url"]); } if (!startsWith(link, "http://") && !startsWith(link, "https://")) { link = "http://" + link; } return '<a href="' + link + '" target="_blank">' + content + "</a>"; }); bbTags["code"] = new bbtag_1.BBTag("code", true, false, true, function (tag, content, attributes) { var lang = attributes["lang"]; if (lang) { 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; }()); exports.BBCodeParser = BBCodeParser; //# sourceMappingURL=bbcodeparser.js.map