BBCode Parser

Parse BBCode into AST and convert into HTML

Mint 2025.09.16.. Lásd a legutóbbi verzió

Ezt a szkriptet nem ajánlott közvetlenül telepíteni. Ez egy könyvtár más szkriptek számára, amik tartalmazzák a // @require https://update.greasyfork.org/scripts/549682/1661585/BBCode%20Parser.js hivatkozást.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

Fejlesztő
PYUDNG
Verzió
0.1
Létrehozva
2025.09.16.
Frissítve
2025.09.16.
Size
20 KB
Licensz
GPL-3.0-or-later

A BBCode parser without any built-in parsing rules, allowing users to implement their own rules.

Usage

1. First of all, create parser

const parser = new BBCodeParser();

2. Implement and register parsing rules

A rule for [url=URL]CONTENT[/url] and [url]URL_AS_CONTENT[/url] can be registered like this:

parser.register({
    'url': {
        openTag(params, content) {
            let url = params ?? content;
            url = url.startsWith('http://') || url.startsWith('https://') ? url : 'http://' + url;
            return `<a href="${ url }" target="_blank">`;
        },
        closeTag(params, content) {
            return '</a>'
        },
    }
});

A rule for [b]CONTENT[/b] can be registered like this:

parser.register({
    'b': {
        openTag(params, content) {
            return '<b>';
        },
        closeTag(params, content) {
            return '</b>';
        },
    },
});

And yes, they can be registered with one call:

parser.register({
    'url': {
        openTag(params, content) {
            let url = params ?? content;
            url = url.startsWith('http://') || url.startsWith('https://') ? url : 'http://' + url;
            return `<a href="${ url }" target="_blank">`;
        },
        closeTag(params, content) {
            return '</a>'
        },
    },
    'b': {
        openTag(params, content) {
            return '<b>';
        },
        closeTag(params, content) {
            return '</b>';
        },
    },
});

3. Parse bbcode (and get html if need)

const result = parser.parse('[b]Hello[/b], [url=https://example.com/]bbcode[/url]');
// You can access html or ast / nodes in result object, like
console.log(result.html);

Types

Please refer to JSDoc in source code.