⚡️Blazing fast BBCode transforming and parsing tool in pure javascript, no dependencies
Switch branches/tags
v2.3.0 v2.2.0 @bbob/react@2.0.3 @bbob/react@2.0.2 @bbob/react@2.0.1 @bbob/react@2.0.0 @bbob/react@1.1.1 @bbob/react@1.1.0 @bbob/react@1.0.7 @bbob/react@1.0.6 @bbob/react@1.0.5 @bbob/react@1.0.4 @bbob/react@1.0.3 @bbob/react@1.0.1 @bbob/react@1.0.0 @bbob/preset@2.1.0 @bbob/preset@2.0.0 @bbob/preset@1.1.1 @bbob/preset-react@2.0.3 @bbob/preset-react@2.0.2 @bbob/preset-react@2.0.1 @bbob/preset-react@2.0.0 @bbob/preset-react@1.1.1 @bbob/preset-html5@2.1.2 @bbob/preset-html5@2.1.1 @bbob/preset-html5@2.1.0 @bbob/preset-html5@2.0.0 @bbob/preset-html5@1.1.1 @bbob/preset-html5@1.1.0 @bbob/preset-html5@1.0.6 @bbob/preset-html5@1.0.5 @bbob/preset-html5@1.0.4 @bbob/preset-html5@1.0.3 @bbob/preset-html5@1.0.2 @bbob/plugin-helper@2.0.1 @bbob/plugin-helper@2.0.0 @bbob/plugin-helper@1.1.1 @bbob/plugin-helper@1.1.0 @bbob/parser@2.2.0 @bbob/parser@2.1.0 @bbob/parser@2.0.0 @bbob/parser@1.3.1 @bbob/parser@1.2.0 @bbob/parser@1.1.0 @bbob/parser@1.0.10 @bbob/parser@1.0.9 @bbob/parser@1.0.8 @bbob/parser@1.0.7 @bbob/parser@1.0.3 @bbob/parser@1.0.2 @bbob/html@2.1.1 @bbob/html@2.1.0 @bbob/html@2.0.0 @bbob/html@1.1.1 @bbob/html@1.1.0 @bbob/html@1.0.7 @bbob/html@1.0.6 @bbob/html@1.0.5 @bbob/html@1.0.4 @bbob/html@1.0.3 @bbob/html@1.0.1 @bbob/core@2.1.0 @bbob/core@2.0.2 @bbob/core@2.0.1 @bbob/core@2.0.0 @bbob/core@1.1.1 @bbob/core@1.1.0 @bbob/core@1.0.6 @bbob/core@1.0.5 @bbob/core@1.0.4 @bbob/core@1.0.3 @bbob/core@1.0.2 @bbob/cli@2.0.3 @bbob/cli@2.0.2 @bbob/cli@2.0.1 @bbob/cli@2.0.0 @bbob/cli@1.0.8 @bbob/cli@1.0.7 @bbob/cli@1.0.6 @bbob/cli@1.0.5 @bbob/cli@1.0.4 @bbob/cli@1.0.3 @bbob/cli@1.0.2 @bbob/cli@1.0.1 @bbob/cli@1.0.0
Nothing to show
Clone or download

README.md

BBob a BBCode processor

BBob is a tool to parse and transform BBCode written in pure javascript, no dependencies

Build Status codecov CodeFactor BCH compliance Known Vulnerabilities

Packages

Package Status Size Description
@bbob/core @bbob/core-status 3.2K Core package
@bbob/react @bbob/react-status 1.0K React renderer
@bbob/preset-react @bbob/preset-react-status 1.8K React default tags preset
@bbob/html @bbob/html-status 689B HTML renderer
@bbob/preset-html5 @bbob/preset-html5-status 1.6K HTML5 default tags preset

DEMO Playground

Table of contents

Basic usage

npm i @bbob/core @bbob/html @bbob/preset-html5
import bbob from '@bbob/core'
import { render } from '@bbob/html'
import presetHTML5 from '@bbob/preset-html5'

const processed = bbob(presetHTML5()).process(`[i]Text[/i]`, { render })

console.log(processed.html); // <span style="font-style: italic;">Text</span>

React usage

npm i @bbob/react @bbob/preset-react
import React from 'react'
import {render} from 'react-dom'
import bbobReactRender from '@bbob/react/es/render'
import presetReact from '@bbob/preset-react'

console.log(render(<span>{bbobReactRender(`[i]Text[/i]`, presetReact(), { onlyAllowTags: ['i'] })}</span>)); // <span><span style="font-style: italic;">Text</span></span>

Presets

Its a way to transform parsed BBCode AST tree to another tree by rules in preset

Create your own preset

import { createPreset } from '@bbob/preset'

export default createPreset({
  quote: node => ({
    tag: 'blockquote',
    attrs: {},
    content: [{
      tag: 'p',
      attrs: {},
      content: node.content,
    }],
  }),
})

HTML Preset

Also you can use predefined preset for HTML

import html5Preset from '@bbob/preset-html5/es'
import { render } from '@bbob/html/es'
import bbob from '@bbob/core'

console.log(bbob(html5Preset()).process(`[quote]Text[/quote]`, { render }).html) // <blockquote><p>Text</p></blockquote>

React Preset

Also you can use predefined preset for React

import reactPreset from "@bbob/preset-react";
import reactRender from "@bbob/react/es/render";

const preset = reactPreset.extend((tags, options) => ({
  quote: node => ({
    tag: "blockquote",
    content: node.content
  })
}));

const result = reactRender(`[quote]Text[/quote]`, reactPreset());

/* 
It produces a VDOM Nodes equal to
React.createElement('blockquote', 'Text')
*/
document.getElementById("root").innerHTML = JSON.stringify(result, 4);

Edit lp7q9yj0lq

React usage

Component

Or you can use React Component

import React from 'react'
import { render } from 'react-dom'

import BBCode from '@bbob/react/es/Component'
import reactPreset from '@bbob/preset-react/es'

const MyComponent = () => (
  <BBCode plugins={[reactPreset()]} options={{ onlyAllowTags: ['i'] }}>
    [quote]Text[/quote]
  </BBCode>
)

render(<MyComponent />) // <div><blockquote><p>Text</p></blockquote></div>

Edit 306pzr9k5p

Render prop

Or pass result as render prop

import React from "react";
import { render } from 'react-dom'

import reactRender from '@bbob/react/es/render'
import reactPreset from '@bbob/preset-react/es'

const toReact = input => reactRender(input, reactPreset())

const text = toReact('[b]Super [i]easy[/i][/b] [u]to[/u] render')

const App = ({ renderProp }) => (
  <span>{text}</span>
)

render(<App />) // <span><span style="font-weight: bold;">Super <span style="font-style: italic;">easy</span></span> <span style="text-decoration: underline;">to</span> render</span>

Edit x7w52lqmzz

PostHTML usage

Create Plugin