Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@bbob/parser

JiLiZART244kMIT4.2.0TypeScript support: included

A BBCode to AST Parser part of @bbob

bbcode, parser, ast, bbcode parser, bbcodeparser, bbob, array, parse

readme

BBob a BBCode processor

BBob is a tool to parse and transform [BBCode](https://en.wikipedia.org/wiki/BBCode) written in pure javascript, no dependencies

Tests Benchmark Coverage Status CodeFactor Known Vulnerabilities

Packages

Package Status Size Description
@bbob/react @bbob/react-status @bbob/react-size React renderer
@bbob/preset-react @bbob/preset-react-status @bbob/preset-react-size React default tags preset
@bbob/vue3 @bbob/vue3-status @bbob/vue3-size Vue 3 renderer
@bbob/vue2 @bbob/vue2-status @bbob/vue2-size Vue 2 renderer
@bbob/preset-vue @bbob/preset-vue-status @bbob/preset-vue-size Vue default tags preset
@bbob/html @bbob/html-status @bbob/html-size HTML renderer
@bbob/preset-html5 @bbob/preset-html5-status @bbob/preset-html5-size HTML5 default tags preset
@bbob/core @bbob/core-status @bbob/core-size Core package

DEMO Playground

Table of contents

Basic usage

npm i @bbob/html @bbob/preset-html5
import bbobHTML from '@bbob/html'
import presetHTML5 from '@bbob/preset-html5'

const processed = bbobHTML(`[i]Text[/i]`, presetHTML5())

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

React usage

npm i @bbob/react @bbob/preset-react
import React from 'react'
import BBCode from '@bbob/react';
import presetReact from '@bbob/preset-react';

const plugins = [presetReact()];

export default () => (
    <BBCode plugins={plugins}>
    [table]
      [tr]
          [td]table 1[/td]
          [td]table 2[/td]
      [/tr]
      [tr]
          [td]table 3[/td]
          [td]table 4[/td]
      [/tr]
    [/table]
    </BBCode>
)
import { render } from '@bbob/react'

export default () => render(`
[table]
  [tr]
      [td]table 1[/td]
      [td]table 2[/td]
  [/tr]
  [tr]
      [td]table 3[/td]
      [td]table 4[/td]
  [/tr]
[/table]
`)

Vue 2 usage

npm i @bbob/vue2 @bbob/preset-vue
import Vue from 'vue'
import VueBbob from '@bbob/vue2';

Vue.use(VueBbob);
<template>
  <div class="html">
    <h2>Generated HTML here</h2>
    <bbob-bbcode container="div" :plugins="plugins">{{ bbcode }}</bbob-bbcode>
  </div>
</template>
<script>
  import Vue from 'vue'
  import preset from '@bbob/preset-vue'

  export default Vue.extend({
    name: 'App',
    data() {
      return {
        bbcode: 'Text [b]bolded[/b] and [i]Some Name[/i]',
        plugins: [
          preset()
        ],
      }
    }
  })
</script>

More examples available in examples folder

Parse options

onlyAllowTags

Parse only allowed tags

import bbobHTML from '@bbob/html'
import presetHTML5 from '@bbob/preset-html5'

const processed = bbobHTML(`[i][b]Text[/b][/i]`, presetHTML5(), { onlyAllowTags: ['i'] })

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

contextFreeTags

Enable context free mode that ignores parsing all tags inside given tags

import bbobHTML from '@bbob/html'
import presetHTML5 from '@bbob/preset-html5'

const processed = bbobHTML(`[b]Text[/b][code][b]Text[/b][/code]`, presetHTML5(), { contextFreeTags: ['code'] })

console.log(processed); // <span style="font-weight: bold;">Text</span><pre>[b]Text[/b]</pre>

enableEscapeTags

Enable escape support for tags

import bbobHTML from '@bbob/html'
import presetHTML5 from '@bbob/preset-html5'

const processed = bbobHTML(`[b]Text[/b]'\\[b\\]Text\\[/b\\]'`, presetHTML5(), { enableEscapeTags: true })

console.log(processed); // <span style="font-weight: bold;">Text</span>[b]Text[/b]

caseFreeTags

Allows to parse case insensitive tags like [h1]some[/H1] -> <h1>some</h1>

import bbobHTML from '@bbob/html'
import presetHTML5 from '@bbob/preset-html5'

const processed = bbobHTML(`[h1]some[/H1]`, presetHTML5(), { caseFreeTags: true })

console.log(processed); // <h1>some</h1>
import bbobHTML from '@bbob/html'
import presetHTML5 from '@bbob/preset-html5'

const processed = bbobHTML(`[b]Text[/b]'\\[b\\]Text\\[/b\\]'`, presetHTML5(), { enableEscapeTags: true })

console.log(processed); // <span style="font-weight: bold;">Text</span>[b]Text[/b]

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: node.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) => ({
  ...tags,
  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

For example lets parse all strings that similar to links like "https://some-site.com"

import { createRoot } from "react-dom/client";

import BBCode from "@bbob/react/es/Component";
import TagNode from "@bbob/plugin-helper/es/TagNode";
import { isStringNode } from "@bbob/plugin-helper/es";

const URL_RE = new RegExp(
  `([--:\\w?@%&+~#=]+\\/*\\.[a-z]{2,4}\\/{0,2})((?:[?&](?:\\w+)=(?:\\w+))+|[^^).|,][--:\\w?@%&+~#=()_]+)?`,
  "g"
);

const isValidUrl = (url) => URL_RE.test(url);

const linkParsePlugin = (tree) => {
  return tree.walk((node) => {
    if (isStringNode(node) && isValidUrl(node)) {
      return TagNode.create(
        "a",
        {
          href: node
        },
        `Url to: ${node}`
      );
    }

    return node;
  });
};

const rootElement = document.getElementById("root");
const root = createRoot(rootElement);

root.render(
  <BBCode plugins={[linkParsePlugin]}>
    https://github.com/JiLiZART/BBob Other text without link
  </BBCode>
);

Edit x7w52lqmzz

Benchmarks

To test on your machine run

npm run build
node benchmark

Tested on Node v20.11.1

Package Ops/sec
regex/parser 6 ops/sec
ya-bbcode 11 ops/sec
xbbcode/parser 102 ops/sec
@bbob/parser 174 ops/sec

Checkout Benckmark job results

Donations

You can support this projecti with donation in:

Bitcoin: bc1qx34sx3zmfd5e2km607p8s8t30d4rt33d2l9pwt

USDT(TRC20): TT94uVjJho8n47xbdfNYz6vdebgmKFpxAT

Also thanks to support

Jetbrains IDEA

Developed with <3 using JetBrains

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

2.9.0 (2023-01-29)

Features

2.8.3 (2022-12-18)

Bug Fixes

Features

  • bundlephobia more popular action (8725f04)
  • bundlephobia pr review (a459045)
  • publish branch to npm (2200e0a)

2.8.2 (2022-11-28)

Bug Fixes

2.8.1 (2022-05-24)

Bug Fixes

  • github: lerna bootsrap before publish (4e4b1e6)
  • lerna issue and publish patch (daf9b02)
  • lerna publish scripts (2b6e11a)

Features

  • react: update to react 18 and testing-library (#138) (502362c)

2.8.0 (2021-11-28)

Bug Fixes

  • github: publish using lerna (2eb9d28)
  • react: adjust PropTypes for React Component container (#107) (93d8027)

Features

BREAKING CHANGES

  • now we use swc.rs as main bundler and transpiler instead of babel

    • jest now uses swc
    • rollup now uses swc
  • feat: benchmark now separate package with npm start and colored output

    • benchmark as separate package with error throw if package drops performance
  • feat: all lerna packages now using scripts/pkg-task

  • feat(github): publish to npm and github registry

    • when release was created this action automaticly publish packages to npm and github
  • feat(github): move all from Travis CI to Github Actions

    • code analysis and tests now using github actions
  • test: increase tests coverage

    • add more tests for @bbob/react, @bbob/vue2 and @bbob/parser

2.7.1 (2021-11-04)

Bug Fixes

  • react: adjust PropTypes for React Component container (#107) (93d8027)

2.7.0 (2021-05-19)

Bug Fixes

  • parser: dont process nested tags as string if parent is not allowed (#84) (70ff2e6)
  • parser: tag inside tag parsing regression (#81) (09bda26)
  • react: rendering self-closed tags and tags without content (#74) (5a7211d)

Features

Performance Improvements

  • parser: cache nested tokens in Set to prevent deoptimization (#83) (cad0e9e)

2.6.2 (2020-12-16)

Bug Fixes

  • parser: dont process nested tags as string if parent is not allowed (#84) (70ff2e6)
  • parser: tag inside tag parsing regression (#81) (09bda26)
  • react: rendering self-closed tags and tags without content (#74) (5a7211d)

Features

  • parser: rewrite lexer to make it faster (#50) (772d422)

Performance Improvements

  • parser: cache nested tokens in Set to prevent deoptimization (#83) (cad0e9e)

2.6.1 (2020-12-15)

Bug Fixes

  • parser: tag inside tag parsing regression (#81) (09bda26)
  • react: rendering self-closed tags and tags without content (#74) (5a7211d)

Features

  • parser: rewrite lexer to make it faster (#50) (772d422)

2.6.0 (2020-12-10)

Bug Fixes

  • react: rendering self-closed tags and tags without content (#74) (5a7211d)

Features

  • parser: rewrite lexer to make it faster (#50) (772d422)

2.5.9 (2020-11-16)

Bug Fixes

  • react: rendering self-closed tags and tags without content (#74) (5a7211d)

2.5.8 (2020-07-08)

Bug Fixes

  • plugin-helper: escape case insensitive javascript: attrs (5ceb2f0)

2.5.7 (2020-07-05)

Bug Fixes

2.5.6 (2020-04-12)

Bug Fixes

  • parser: don't eat not allowed tags with params (#58) fixes #54 (a16b9f7)

Features

  • plugin-helper: move getUniqAttr from preset to plugin helper (#63) (f28f19e)

Performance Improvements

  • parser: optimize v8 perf deoptimizations (#61) (97ecba0)

2.5.5 (2020-03-25)

Features

  • core: allow to pass dynamic data in options for render (#59) (0b74be7)

2.5.4 (2019-09-25)

Bug Fixes

  • bbob-react: remove unique "key" prop warning (#30) (3d5c1f1), closes #28
  • parser: infinity loop problem when escape [\b] (#31) (b4cf271), closes #23
  • parser: try to hack terser minifier that removes working code (#49) (be938fd), closes #48
  • plugin-helper: avoid some malformed attributes in attrsToString (#26) (09ff9af)
  • react: fix broken prop type definition (#27) (19d7ff2)

Features

  • parse: allow tags to be escaped with backslash (#17) (c4f78c1)
  • plugin-helper: lowercase resulting tag names (#42) (597c2a9)
  • preset-html5: list type attribute support (#18) (847c55e)

2.5.3 (2019-08-11)

Bug Fixes

  • bbob-react: remove unique "key" prop warning (#30) (3d5c1f1), closes #28
  • parser: infinity loop problem when escape [\b] (#31) (b4cf271), closes #23
  • plugin-helper: avoid some malformed attributes in attrsToString (#26) (09ff9af)
  • react: fix broken prop type definition (#27) (19d7ff2)

Features

  • parse: allow tags to be escaped with backslash (#17) (c4f78c1)
  • plugin-helper: lowercase resulting tag names (#42) (597c2a9)
  • preset-html5: list type attribute support (#18) (847c55e)

2.5.2 (2019-06-30)

Bug Fixes

  • bbob-react: remove unique "key" prop warning (#30) (3d5c1f1), closes #28
  • parser: infinity loop problem when escape [\b] (#31) (b4cf271), closes #23
  • plugin-helper: avoid some malformed attributes in attrsToString (#26) (09ff9af)
  • react: fix broken prop type definition (#27) (19d7ff2)

Features

  • parse: allow tags to be escaped with backslash (#17) (c4f78c1)
  • preset-html5: list type attribute support (#18) (847c55e)

2.5.1 (2019-06-18)

Bug Fixes

  • parser: fix issue with escaping backslashes when enableEscapeTags is set (#20) (8a9e930)

Features

  • parse: allow tags to be escaped with backslash (#17) (c4f78c1)
  • preset-html5: list type attribute support (#18) (847c55e)

2.5.0 (2019-06-17)

Features

  • parse: allow tags to be escaped with backslash (#17) (c4f78c1)
  • preset-html5: list type attribute support (#18) (847c55e)

2.4.1 (2019-03-29)

Bug Fixes

  • react: move @bbob/preset-react to dev deps due to circular deps (3af3ea8)

2.4.0 (2019-03-29)

Features

  • core: add tree.messages array and tree.options (cd2b6fd)
  • html: @bbob/html now can be used without @bbob/core (c9e1dab)

2.3.4 (2019-03-29)

Bug Fixes

  • react: add prop componentProps (#9) (1dafb69)

2.3.3 (2019-03-29)

Bug Fixes

  • react: add prop componentProps (#9) (1dafb69)

2.3.2 (2019-03-09)

Note: Version bump only for package undefined

2.3.1 (2019-03-04)

Bug Fixes

2.3.0 (2018-10-25)

Features

  • react: allow pass custom options to react component (77b30f3)

2.2.0 (2018-10-11)

Bug Fixes

  • core: string walk api test error (bdd8bbd)
  • html: add more tests (4ebc512)
  • html: rigt import to support three shaking (485852d)
  • parser: dependency fail (7300535)
  • parser: only allowed tags error (d3e8e4a)
  • parser: remove bad code (4d9dc34)
  • parser: tokenizer error with quotemark strings (7f40050)
  • plugin-helper: better handle content of TagNode (505152b)
  • react: remove jsx (ada2c00)

Features

  • react render support, move some helper functions to plugin-helper (1a84968)
  • bbob: add codecov support to travis (44183fd)
  • core: add helper function to plugin api (e189a39)
  • core: implement plugin api (ee047e8)
  • core: raw tree property support (bdfd3f6)
  • lexer: new lexer (#1) (8882651)
  • parser: add support for custom tokenizer (ce03b2f)
  • parser: better handlinf of unclosed tags like '[My unclosed and [closed] tag' (b49b743)
  • parser: better line and column counting support in tokens (1c3bebe)
  • parser: custom open and close tags support, html tags tests (#3) (790825a)
  • parser: inconsistent tag detection test (2eb83c1)
  • parser: optimize size (4c8dbed)
  • base preset package '@bbob/preset' (b63864c)
  • new @bbob/html api (#4) (575c1bb)
  • preset-html5: add basic preset with tests (18ab61b)