BBCodeParser
An extensible BB code parser written in TypeScript that can be used
both in the browser and Node.js.
Usage
<script src="bbCodeParser.min.js"></script>
...
var parser = new BBCodeParser(BBCodeParser.defaultTags());
var inputText = "[b]Bold text[/b]";
var generatedHtml = parser.parseString(inputText);
Node.js: npm install bbcode-parser
var BBCodeParser = require('bbcode-parser');
var parser = new BBCodeParser(BBCodeParser.defaultTags());
var html = parser.parseString('[b]Bold text[/b]');
Custom tags
BBTag constructor:
- tagName: The name of the tag.
- insertLineBreaks: Indicates if the tag inserts line breaks (\n ->
<br>) in the content.
- suppressLineBreaks: Suppresses line breaks for any nested tag.
- noNesting: If the tags doesn't support nested tags.
- tagGenerator: The HTML generator for the tag. If not supplied the default one is used:
<tagName>content</tagName>.
var bbTags = {};
bbTags["b"] = BBTag.createSimpleTag("b");
bbTags["img"] = BBTag.createSimpleTag("img", function (tag, content, attributes) {
return "<img src=\"" + content + "\" />";
});
bbTags["url"] = BBTag.createSimpleTag("url", function (tag, content, attributes) {
var link = content;
if (attributes["site"] != undefined) {
link = escapeHTML(attributes["site"]);
}
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, attributes) {
return "<code>" + content + "</code>";
});
var parser = new BBCodeParser(bbTags);
Documentation
See the wiki.
Build
To run the build script you need:
- Node.js
- TypeScript (npm install -g typescript)
- uglify-js (npm install -g uglify-js)