npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@onachi/bbcode-parser

v0.1.6

Published

Unified plugin to parse BBCode text into bbast syntax tree

Downloads

16

Readme

@onachi/bbcode-parser

Unified plugin to parse BBCode text into bbast syntax tree.

Installation

npm install @onachi/bbcode-parser
# or
pnpm add @onachi/bbcode-parser
# or
yarn add @onachi/bbcode-parser

Usage

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'
import { toHast } from '@onachi/bbast-util-to-hast'
import rehypeStringify from 'rehype-stringify'

// Parse BBCode and convert to HTML
const processor = unified()
  .use(bbcodeParser)
  .use(() => (tree) => toHast(tree))
  .use(rehypeStringify)

const result = await processor.process('[b]Hello[/b] [i]World[/i]!')
console.log(String(result))
// <p><strong>Hello</strong> <em>World</em>!</p>

API

bbcodeParser(options?)

Unified plugin that parses BBCode text into bbast syntax tree.

Parameters

Returns

Returns a unified plugin that transforms BBCode text into bbast.

ParseOptions

Parser configuration options (inherited from @onachi/bbast-util-from-bbcode):

interface ParseOptions {
  /** Whether to create paragraph nodes for text blocks (default: true) */
  paragraphs?: boolean
  /** Whether to preserve line breaks (default: true) */
  preserveLineBreaks?: boolean
  /** Custom tag handlers */
  customTags?: Record<string, TagHandler>
  /** Whether to be strict about tag matching (default: false) */
  strict?: boolean
}

For detailed documentation of these options, see @onachi/bbast-util-from-bbcode.

Examples

Basic Usage

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'

const processor = unified().use(bbcodeParser)

const bbcode = '[b]Bold text[/b] and [i]italic text[/i]'
const ast = processor.parse(bbcode)

console.log(ast)
// {
//   type: 'root',
//   children: [
//     {
//       type: 'paragraph',
//       children: [
//         {
//           type: 'bold',
//           children: [{ type: 'text', value: 'Bold text' }]
//         },
//         { type: 'text', value: ' and ' },
//         {
//           type: 'italic',
//           children: [{ type: 'text', value: 'italic text' }]
//         }
//       ]
//     }
//   ]
// }

With Custom Options

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'

const processor = unified().use(bbcodeParser, {
  paragraphs: false,
  strict: true,
  customTags: {
    spoiler: (tagName, attributes, children) => ({
      type: 'spoiler',
      hidden: true,
      children
    })
  }
})

const ast = processor.parse('[spoiler]Hidden content[/spoiler]')

Complete Pipeline: BBCode to HTML

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'
import { toHast } from '@onachi/bbast-util-to-hast'
import rehypeStringify from 'rehype-stringify'

const processor = unified()
  .use(bbcodeParser)
  .use(() => (tree) => toHast(tree))
  .use(rehypeStringify)

const bbcode = `
[quote author="John"]
  [b]Important:[/b] Check out this [url=https://example.com]link[/url]!
  
  [code]console.log('Hello World')[/code]
[/quote]
`

const result = await processor.process(bbcode)
console.log(String(result))
// <blockquote data-author="John">
//   <p><strong>Important:</strong> Check out this <a href="https://example.com">link</a>!</p>
//   <p><code>console.log('Hello World')</code></p>
// </blockquote>

Using with bbcode-rehype

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'
import bbcodeRehype from '@onachi/bbcode-rehype'
import rehypeStringify from 'rehype-stringify'

const processor = unified()
  .use(bbcodeParser, { strict: false })
  .use(bbcodeRehype, { 
    elementMappings: { bold: 'b', italic: 'i' }
  })
  .use(rehypeStringify)

const result = await processor.process('[b]Bold[/b] and [i]italic[/i]')
console.log(String(result))
// <p><b>Bold</b> and <i>italic</i></p>

Processing Files

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'
import { toHast } from '@onachi/bbast-util-to-hast'
import rehypeStringify from 'rehype-stringify'
import { read, write } from 'to-vfile'

const processor = unified()
  .use(bbcodeParser)
  .use(() => (tree) => toHast(tree))
  .use(rehypeStringify)

const input = await read('input.bbcode')
const result = await processor.process(input)
await write({ path: 'output.html', contents: String(result) })

Streaming Processing

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'
import { stream } from 'unified-stream'

const processor = unified()
  .use(bbcodeParser)
  .use(() => (tree) => {
    // Transform the bbast tree
    console.log('Parsed BBCode AST:', tree)
    return tree
  })

process.stdin
  .pipe(stream(processor))
  .pipe(process.stdout)

Error Handling

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'

const processor = unified().use(bbcodeParser, { strict: true })

try {
  const ast = processor.parse('[invalid bbcode')
  console.log('Parsed successfully:', ast)
} catch (error) {
  console.error('Parse error:', error.message)
}

Integration with Unified Ecosystem

This plugin works seamlessly with the unified ecosystem:

With Remark (Markdown)

import { unified } from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import bbcodeParser from '@onachi/bbcode-parser'
import rehypeStringify from 'rehype-stringify'

// Process both Markdown and BBCode
const processor = unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeStringify)

const bbcodeProcessor = unified()
  .use(bbcodeParser)
  .use(() => (tree) => toHast(tree))
  .use(rehypeStringify)

With Rehype (HTML)

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'
import bbcodeRehype from '@onachi/bbcode-rehype'
import rehypeFormat from 'rehype-format'
import rehypeStringify from 'rehype-stringify'

const processor = unified()
  .use(bbcodeParser)
  .use(bbcodeRehype)
  .use(rehypeFormat)
  .use(rehypeStringify)

Supported BBCode Tags

This plugin supports all BBCode tags handled by @onachi/bbast-util-from-bbcode:

Formatting

  • [b]bold[/b], [i]italic[/i], [u]underline[/u], [s]strikethrough[/s]

Colors and Styling

  • [color=red]text[/color], [size=large]text[/size], [font=Arial]text[/font]

Alignment

  • [center]text[/center], [left]text[/left], [right]text[/right]

Links and Media

  • [url=https://example.com]Link[/url], [img=https://example.com/image.jpg]

Blocks

  • [quote]text[/quote], [code]code[/code]

Lists

  • [list][li]item[/li][/list]

Tables

  • [table][tr][td]cell[/td][/tr][/table]

For complete documentation of supported tags, see @onachi/bbast-util-from-bbcode.

Related

License

MIT