KEMU Documents server
JavaScript failed !
So this is static version of this website.
This website works a lot better in JavaScript enabled browser.
Please enable JavaScript.

Introduction

This section is related to JavaScript (Node.js) implementation of Kemu BB-Code.

We started node.js implementation of BB2 in 2014 as internal project based on our existing PHP implementation.
We published it in 2015 as npm package.

All the code of KBB is written in CoffeeScript and so are the examples below. Obviously you can use KBB from your plain JavaScript code.
CoffeeScript is not mandatory for your application code.

The node.js package is publicly available on Node Package Manager as bb2

The source code is published as github repo

All the example code given here can be found in node_package_check of github repository.

Installation

KBB is written in CoffeeScript, so it needs your node.js to support CoffeeScript notation. If you don't have CoffeeScript installed:
npm install -g coffee-script

To install KBB:
npm install bb2

Simple code

The code:
# example_01.coffee
BBCodeHtml = require('bb2').BBCodeHtml
bb = new BBCodeHtml()
console.log bb.parse('visible [0]invisible[1] [b]bold[/] [i]italic[/]')
prints to console:
visible  bold italic

So, simple usage pattern is:
  • Load bb2
  • Create instance of BBCodeHtml
  • Call parse()

Config - usage example

The instance of BBCodeHtml holds parsing config. The config should be passed in constructor (config details explained in config section):
# example_config.coffee
BBCodeHtml = require('bb2').BBCodeHtml
config = {innerUrl: 'http://example.com/'}
bb = new BBCodeHtml(config)
console.log bb.parse('AAAAA [linkin url="the_internal_link"]BBBB[/] CCCC')
prints to console
AAAAA <a href="http://example.com/the_internal_link">BBBB</a> CCCC

Config - defaults and overriding them

This is default config as for version 1.0.2
allowDirectHtml: false
innerUrl: ''
imgUrl: ''
lbToBrEnabled: true
blockOnClickOnInternalLinks: false
passLinkDataOnInternalLinks: false

To override some of default values, just pass object with needed values to the constructor. For example:
config = {
    innerUrl: 'http://example.com/'
    lbToBrEnabled: false
}
bb = new BBCodeHtml(config)

Config - parameters reference

  • allowDirectHtml (true/false) - controls direct HTML entities obfuscation (for example for security reason)
    • true - direct HTML entities are in pass-through mode.
    • false - direct HTML entities are not allowed
      In this mode, characters in set of <, >, &, ", ' are converted to their corresponding HTML entities, e.g. < is rendered as &lt;
  • innerUrl (string): sets the prefix for internal links used by [linkin] bb-tags.
    For example: if innerUrl is 'http://example.com/', then
    [linkin url="test"]xxx[/]
    is renered as
    <a href="http://example.com/test">xxx</a>
  • imgUrl (string): sets the prefix for image urls, in BB tags like [img]
    For example: if imgUrl is 'http://example.com/gfx/', then
    [img url="test.jpg"]
    is renered as
    <img src="http://example.com/gfx/test.jpg"/>
  • lbToBrEnabled (true/false) - line-break conversion mode
    • true - all line-break (linux line-feed, '\n', ASCII 10) characters, are converted to <br> HTML code
    • false - line breaks are simply ignored
  • blockOnClickOnInternalLinks (true/false)
    This is useful when default browser "click" control on internal links is to be overriden by JavaScript code (like Ajax/Websocket loaders).
    • true - the click event for internal links ([linkin]) is overriden in HTML and returns false to browser, so default browser action is disabled
    • false - normal behaviour
  • passLinkDataOnInternalLinks (true/false)
    This is useful when default browser "click" control on internal links is to be overriden by JavaScript code (like Ajax/Websocket loaders).
    • true - store plain url of link as "data-linkin" parameter of HTML tag - very useful for link routing on browser side
    • false - normal behaviour

Help and feedback

If something doesn't work, you just have questions or request for upgrade, don't hesitate to contact us.