The `js-bbcode-parser` library offers a simple and efficient solution for converting BBCode markup into HTML. Currently, at version 5.1.0, the package demonstrates an active development lifecycle with consistent updates within the v5 series since its major release. Its core strength lies in its configurability, allowing developers to extend the parser with custom BBCode tags and their corresponding HTML output. A crucial distinction is its explicit design choice *not* to include built-in XSS protection; implementers are responsible for sanitizing all user-generated input to prevent potential cross-site scripting vulnerabilities, making it a powerful but security-conscious tool. The library supports a comprehensive set of default BBCode tags, including formatting (`[b]`, `[i]`, `[u]`), headings (`[h1]` to `[h6]`), paragraphs (`[p]`), styling (`[color]`, `[size]`), and media (`[img]`, `[email]`). It also handles attributes like `class` and `data-*` within tags, providing flexibility for styled output. Its simple API makes integration straightforward, either by using a pre-configured default parser instance or by creating custom instances for more fine-grained control over the parsing rules.
npm install js-bbcode-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of the default parser, how to create and configure a custom parser with new BBCode tags, and highlights the critical need for XSS sanitization.
Review the v5.x documentation for new instantiation patterns (e.g., `new BBCodeParser({})`) and configuration methods (e.g., `parser.addCode()` or `parser.setCodes()`).Always pass the parsed HTML output through a dedicated HTML sanitization library (e.g., DOMPurify) before rendering it in a browser or storing it. Do not rely on this library for XSS protection.
Ensure you use `import BBCodeParser from 'js-bbcode-parser/src/index.js';` for the class constructor. Be aware that changes to internal paths are possible in future major versions.
For CommonJS, use `const bbCodeParser = require('js-bbcode-parser').default;`. For ES Modules, ensure you are importing the default export correctly: `import bbCodeParser from 'js-bbcode-parser';`.Import the class constructor with `import BBCodeParser from 'js-bbcode-parser/src/index.js';`.
Register the custom BBCode tag using `parser.addCode('mycustomtag', (content) => `<div>${content}</div>');` before parsing the string.No dependency data recorded yet.