bbcode-parser-sax is a bbcode parser, writen with PEGjs. The PEGjs grammar is compiled into a parser library and does not have any dependencies.
The parser outputs a list of events in json, each with a specified type, similar to a SAX parser.
For example, given the string: The quick brown fox [i]jumped[/i] [b]over[/b] the [u]stream[/u]
, the tool would emit:
[
{ type: 'text', text: 'The quick brown ' },
{ type: 'startTag', tag: { name: 'i', value: null } },
{ type: 'text', text: 'fox' },
{ type: 'endTag', tag: { name: 'i', value: null } },
{ type: 'text', text: ' ' },
{ type: 'startTag', tag: { name: 'b', value: null } },
{ type: 'text', text: 'jumped' },
{ type: 'endTag', tag: { name: 'b', value: null } }
]
Tags that have values, like [url=https://site.com]
will be parsed
as tag: { name: 'url', value: 'https://site.com' }
.
This style allows the parser to be simple, and makes it easy to adapt the output to whatever you'd like!
A package with both CommonJS and ES Modules is available on NPM.
You can install it with yarn add bbcode-parser-sax
or npm install bbcode-parser-sax
To build the package from source, run yarn build
from the project directory to
both generate the parser from the grammar, and to build cjs and esm outputs.
Usage will depend if you're using this with CommonJS or ESM
For CommonJS:
const {parse} = require("bbcode-parser-sax")
return parse(BBCODE_STRING)
For ESM:
import parser from 'bbcode-parser-sax'
return parser.parse(BBCODE_STRING)
This package has a few basic tests to confirm the parser is working.
Run yarn test
to execute the tests.