svenslaggare / BBCodeParser

BB code parser written in TypeScript.
MIT License
34 stars 18 forks source link

BBCodeParser

An extensible BB code parser written in TypeScript that can be used both in the browser and Node.js.

Usage

<script src="https://github.com/svenslaggare/BBCodeParser/raw/master/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:

var bbTags = {};

//Simple tag. A simple tag means that the generated HTML will be <tagName>content</tagName>
bbTags["b"] = BBTag.createSimpleTag("b");

//Tag with a custom generator.
bbTags["img"] = BBTag.createSimpleTag("img", function (tag, content, attr) {
    return "<img src=\"" + content + "\" />";
});

//Tag with a custom generator + attributes
bbTags["url"] = BBTag.createSimpleTag("url", function (tag, content, attr) {
    var link = content;

    if (attr["site"] != undefined) {
        link = escapeHTML(attr["site"]);
    }

    if (!startsWith(link, "http://") && !startsWith(link, "https://")) {
        link = "http://" + link;
    }

    return "<a href=\"" + link + "\" target=\"_blank\">" + content + "</a>";
});

//A tag that doesn't support nested tags. Useful when implementing code highlighting.
bbTags["code"] = new BBTag("code", true, false, true, function (tag, content, attr) {
    return "<code>" + content + "</code>";
});

var parser = new BBCodeParser(bbTags);

Documentation

See the wiki.

Build

To run the build script you need: