|
| 1 | +# bbcode-parser-sax - a bbcode parser with no dependencies and a simple API. |
| 2 | + |
| 3 | +bbcode-parser-sax is a bbcode parser, writen with PEGjs. The PEGjs grammar is |
| 4 | +compiled into a parser library and does not have any dependencies. |
| 5 | + |
| 6 | +The parser outputs a list of events in json, each with a specified type, |
| 7 | +similar to a SAX parser. |
| 8 | + |
| 9 | +For example, given the string: `The quick brown fox [i]jumped[/i] |
| 10 | + [b]over[/b] the [u]stream[/u]`, the tool would emit: |
| 11 | + |
| 12 | + ```js |
| 13 | + [ |
| 14 | + { type: 'text', text: 'The quick brown ' }, |
| 15 | + { type: 'startTag', tag: { name: 'i', value: null } }, |
| 16 | + { type: 'text', text: 'fox' }, |
| 17 | + { type: 'endTag', tag: { name: 'i', value: null } }, |
| 18 | + { type: 'text', text: ' ' }, |
| 19 | + { type: 'startTag', tag: { name: 'b', value: null } }, |
| 20 | + { type: 'text', text: 'jumped' }, |
| 21 | + { type: 'endTag', tag: { name: 'b', value: null } } |
| 22 | +] |
| 23 | +``` |
| 24 | + |
| 25 | +Tags that have values, like `[url=https://site.com]` will be parsed |
| 26 | +as `tag: { name: 'url', value: 'https://site.com' }`. |
| 27 | + |
| 28 | +This style allows the parser to be simple, and makes it easy to adapt |
| 29 | +the output to whatever you'd like! |
| 30 | + |
| 31 | +## Building |
| 32 | +Run `yarn build` from the project directory to both generate the parser |
| 33 | +from the grammar, and to build cjs and esm outputs. |
| 34 | + |
| 35 | +## Usage |
| 36 | +Usage will depend if you're using this with CommonJS or ESM |
| 37 | + |
| 38 | +For CommonJS: |
| 39 | +```js |
| 40 | +const {parse} = require("bbcode-parser-sax") |
| 41 | +return parse(BBCODE_STRING) |
| 42 | +``` |
| 43 | + |
| 44 | +For ESM: |
| 45 | +```js |
| 46 | +import parser from 'bbcode-parser-sax' |
| 47 | +return parser.parse(BBCODE_STRING) |
| 48 | +``` |
0 commit comments