Class Parser

Constructors

  • Parameters

    • supportedTagNames: string[]
    • Optional caseSensitive: boolean
    • Optional lenient: boolean

    Returns Parser

Properties

caseSensitive: boolean

Whether the parser should be case-sensitive or not regarding tag names and supportedTagNames.

lenient: boolean

Is more lenient about closing tags and mismatched tags. Instead of throwing an error, it will turn the entire node into a TextNode with the text of the entire node.

supportedTagNames: string[]

The list of supported tag names. Any tags that are not in this list will be treated like text.

Methods

  • Parameters

    • name: string

    Returns string

  • Convert a chunk of BBCode to a RootNode.

    Parameters

    • text: string

      The chunk of BBCode to convert.

    Returns RootNode

    The RootNode representing the BBCode.

    Throws

    If the BBCode is not valid (missing a closing tag).

    Example: Basic Example

    const parsed = parser.parse("[b]Hello, world![/b]");
    console.log(parsed.toString()); // [b]Hello, world![/b]
    console.log(parsed.nodeTree());

    // RootNode {
    // Node [b] {
    // TextNode {
    // Hello, world!
    // }
    // }
    // }

    Example: Example with Attributes

    const parsed = parser.parse("[b][i]Hi![/i][/b][img width=100 height=100]https://example.com/image.png[/img]");
    console.log(parsed.toString()); // [b][i]Hi![/i][/b][img width=100 height=100]https://example.com/image.png[/img]
    console.log(parsed.nodeTree());

    // RootNode {
    // Node [b] {
    // Node [i] {
    // TextNode {
    // Hi!
    // }
    // }
    // }
    // Node [img] (width=100, height=100) {
    // TextNode {
    // https://example.com/image.png
    // }
    // }
    // }

    Example: Complex Example

    const parsed = parser.parse('[size=50][quote=JohnDoe message=1]Hello, world![/quote][/size][img alt="World said hi!" width=100 height=100]https://example.com/image.png[/img]');
    console.log(parsed.toString()); // [size=50][quote=JohnDoe message=1]Hello, world![/quote][/size][img alt="World said hi!" width=100 height=100]https://example.com/image.png[/img]
    console.log(parsed.nodeTree());

    // RootNode {
    // Node [size] {
    // Node [quote] (JohnDoe, message=1) {
    // TextNode {
    // Hello, world!
    // }
    // }
    // }
    // Node [img] (alt="World said hi!", width=100, height=100) {
    // TextNode {
    // https://example.com/image.png
    // }
    // }
    // }

Generated using TypeDoc