BB code parser written in TypeScript.
npm install @panzer1119/bbcode-parserBBCodeParser
============
An extensible BB code parser written in TypeScript that can be used
both in the browser and Node.js.
javascript
...
var parser = new BBCodeParser(BBCodeParser.defaultTags());
var inputText = "[b]Bold text[/b]";
var generatedHtml = parser.parseString(inputText);
`
Node.js: npm install bbcode-parser
` javascript
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 -> ) 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: .
`javascript
var bbTags = {};
//Simple tag. A simple tag means that the generated HTML will be content
bbTags["b"] = BBTag.createSimpleTag("b");
//Tag with a custom generator.
bbTags["img"] = BBTag.createSimpleTag("img", function (tag, content, attributes) {
return "
";
});
//Tag with a custom generator + attributes
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 "" + content + "";
});
//A tag that doesn't support nested tags. Useful when implementing code highlighting.
bbTags["code"] = new BBTag("code", true, false, true, function (tag, content, attributes) {
return "" + content + "";
});
var parser = new BBCodeParser(bbTags);
``