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-rehype

v0.1.6

Published

Unified plugin to convert BBCode AST to HAST (HTML Abstract Syntax Tree)

Downloads

9

Readme

@onachi/bbcode-rehype

Unified plugin to convert BBCode AST (bbast) to hast (HTML Abstract Syntax Tree).

Installation

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

Usage

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

// Complete BBCode to HTML pipeline
const processor = unified()
  .use(bbcodeParser)
  .use(bbcodeRehype)
  .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

bbcodeRehype(options?)

Unified plugin that transforms bbast syntax tree to hast.

Parameters

Returns

Returns a unified plugin that transforms bbast to hast.

ToHastOptions

Transform configuration options (inherited from @onachi/bbast-util-to-hast):

interface ToHastOptions {
  /** Whether to create paragraphs for text blocks (default: true) */
  paragraphs?: boolean
  /** Custom element mappings */
  elementMappings?: Partial<ElementMappings>
  /** Custom class names for elements */
  classNames?: Partial<ClassNames>
  /** Whether to preserve data attributes (default: true) */
  preserveDataAttributes?: boolean
  /** Default image attributes */
  defaultImageAttributes?: Properties
}

For detailed documentation of these options, see @onachi/bbast-util-to-hast.

Examples

Basic Usage

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)
  .use(bbcodeRehype)
  .use(rehypeStringify)

const bbcode = '[b]Bold text[/b] and [i]italic text[/i]'
const result = await processor.process(bbcode)

console.log(String(result))
// <p><strong>Bold text</strong> and <em>italic text</em></p>

Custom Element Mappings

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)
  .use(bbcodeRehype, {
    elementMappings: {
      bold: 'b',        // Use <b> instead of <strong>
      italic: 'i',      // Use <i> instead of <em>
      quote: 'div'      // Use <div> instead of <blockquote>
    }
  })
  .use(rehypeStringify)

const result = await processor.process('[b]Bold[/b] [quote]Quote[/quote]')
console.log(String(result))
// <p><b>Bold</b> <div>Quote</div></p>

Custom CSS Classes

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)
  .use(bbcodeRehype, {
    classNames: {
      bold: 'my-bold',
      italic: 'my-italic',
      quote: 'my-quote'
    }
  })
  .use(rehypeStringify)

const result = await processor.process('[b]Bold[/b] [quote]Quote[/quote]')
console.log(String(result))
// <p><strong class="my-bold">Bold</strong> <blockquote class="my-quote">Quote</blockquote></p>

Complex BBCode Processing

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)
  .use(bbcodeRehype, {
    defaultImageAttributes: {
      loading: 'lazy',
      decoding: 'async'
    }
  })
  .use(rehypeStringify)

const bbcode = `
[quote author="John"]
  [b]Important:[/b] Check out this [url=https://example.com]awesome site[/url]!
  
  [img=https://example.com/image.jpg]
  
  [code]
  const example = 'Hello World';
  console.log(example);
  [/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">awesome site</a>!</p>
//   <p><img src="https://example.com/image.jpg" loading="lazy" decoding="async"></p>
//   <p><code>const example = 'Hello World';
//   console.log(example);</code></p>
// </blockquote>

Without Paragraphs

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, { paragraphs: false })
  .use(bbcodeRehype, { paragraphs: false })
  .use(rehypeStringify)

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

Processing Files

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

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

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

With Additional Rehype Plugins

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

// Pretty-formatted HTML
const prettyProcessor = unified()
  .use(bbcodeParser)
  .use(bbcodeRehype)
  .use(rehypeFormat)
  .use(rehypeStringify)

// Minified HTML
const minifyProcessor = unified()
  .use(bbcodeParser)
  .use(bbcodeRehype)
  .use(rehypeMinify)
  .use(rehypeStringify)

Custom Tag Handling

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, {
    customTags: {
      spoiler: (tagName, attributes, children) => ({
        type: 'spoiler',
        revealed: attributes.revealed === 'true',
        children
      }),
      youtube: (tagName, attributes) => ({
        type: 'video',
        provider: 'youtube',
        id: attributes.youtube || attributes.id
      })
    }
  })
  .use(bbcodeRehype, {
    // Custom element mapping for the custom tags
    elementMappings: {
      spoiler: 'details',
      video: 'iframe'
    }
  })
  .use(rehypeStringify)

const result = await processor.process('[spoiler]Hidden content[/spoiler]')

Streaming Processing

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

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

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

Transform Inspection

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'
import bbcodeRehype from '@onachi/bbcode-rehype'
import { inspect } from 'unist-util-inspect'

const processor = unified()
  .use(bbcodeParser)
  .use(() => (tree) => {
    console.log('BBCode AST:')
    console.log(inspect(tree))
    return tree
  })
  .use(bbcodeRehype)
  .use(() => (tree) => {
    console.log('HTML AST:')
    console.log(inspect(tree))
    return tree
  })

await processor.process('[b]Hello[/b] [i]World[/i]!')

Integration with Unified Ecosystem

This plugin integrates seamlessly with the unified ecosystem:

With Rehype Plugins

import { unified } from 'unified'
import bbcodeParser from '@onachi/bbcode-parser'
import bbcodeRehype from '@onachi/bbcode-rehype'
import rehypeHighlight from 'rehype-highlight'
import rehypeSlug from 'rehype-slug'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeStringify from 'rehype-stringify'

const processor = unified()
  .use(bbcodeParser)
  .use(bbcodeRehype)
  .use(rehypeHighlight)           // Syntax highlighting for code blocks
  .use(rehypeSlug)                // Add IDs to headings
  .use(rehypeAutolinkHeadings)    // Add links to headings
  .use(rehypeStringify)

Building a Complete Content Pipeline

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

const processor = unified()
  .use(bbcodeParser)
  .use(bbcodeRehype, {
    classNames: {
      quote: 'blockquote',
      code: 'code-block'
    }
  })
  .use(rehypeDocument, {
    title: 'BBCode Content',
    css: ['styles.css'],
    meta: [{ name: 'viewport', content: 'width=device-width, initial-scale=1' }]
  })
  .use(rehypeFormat)
  .use(rehypeStringify)

const result = await processor.process('[b]Hello[/b] [i]World[/i]!')
// Outputs complete HTML document with head, body, etc.

Supported Transformations

This plugin supports all transformations handled by @onachi/bbast-util-to-hast:

Formatting

  • [b]bold[/b]<strong> or <b>
  • [i]italic[/i]<em> or <i>
  • [u]underline[/u]<u>
  • [s]strikethrough[/s]<s> or <del>

Colors and Styling

  • [color=red]text[/color]<span style="color: red">
  • [size=large]text[/size]<span class="bb-size" data-size="large">
  • [font=Arial]text[/font]<span style="font-family: Arial">

Alignment

  • [center]text[/center]<div class="bb-center" style="text-align: center">
  • [left]text[/left]<div class="bb-left" style="text-align: left">
  • [right]text[/right]<div class="bb-right" style="text-align: right">

Links and Media

  • [url=https://example.com]Link[/url]<a href="https://example.com">Link</a>
  • [img=https://example.com/image.jpg]<img src="https://example.com/image.jpg" />

Blocks

  • [quote]text[/quote]<blockquote>text</blockquote>
  • [code]code[/code]<code>code</code>

Lists

  • [list][li]item[/li][/list]<ul><li>item</li></ul>

For complete documentation of supported transformations, see @onachi/bbast-util-to-hast.

Related

License

MIT