BBCode Parser

Parse BBCode into AST and convert into HTML

Stan na 16-09-2025. Zobacz najnowsza wersja.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/549682/1661585/BBCode%20Parser.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

Autor
PYUDNG
Wersja
0.1
Utworzono
16-09-2025
Zaktualizowano
16-09-2025
Rozmiar
24,9 KB
Licencja
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.