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:ⓘ Example usage:
npm install -g coffee-script
To install KBB:
ⓘ Example usage:
npm install bb2
Simple code
The code:ⓘ Example usage:
prints to console:# example_01.coffee BBCodeHtml = require('bb2').BBCodeHtml bb = new BBCodeHtml() console.log bb.parse('visible [0]invisible[1] [b]bold[/] [i]italic[/]')
ⓘ Example usage:
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 usage:
prints to console# 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')
ⓘ Example usage:
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ⓘ Example usage:
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:
ⓘ Example usage:
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 <
- true - direct HTML entities are in pass-through mode.
- innerUrl (string): sets the prefix for internal links used by [linkin] bb-tags.
For example: if innerUrl is 'http://example.com/', thenⓘ Example usage:is renered as[linkin url="test"]xxx[/]
ⓘ Example usage:<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ⓘ Example usage:is renered as[img url="test.jpg"]
ⓘ Example usage:<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
- true - all line-break (linux line-feed, '\n', ASCII 10) characters, are converted to <br> HTML code
- 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
- true - the click event for internal links ([linkin]) is overriden in HTML and returns false to browser, so default browser action is disabled
- 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
- true - store plain url of link as "data-linkin" parameter of HTML tag - very useful for link routing on browser side