22 * Parameters for constructing a new Node object.
33 */
44export interface NodeConstructorParams {
5+ /**
6+ * The name of the node.
7+ *
8+ * Ref: {@link Node.name}
9+ * @example `[b]Hello World![/b]` -> `b`
10+ */
511 name : string ;
12+ /**
13+ * The attributes of the node.
14+ *
15+ * Ref: {@link Node.attributes}
16+ * @example `[img width=100 height=100]https://example.com/image.png[/img]` -> `{ width: "100", height: "100" }`
17+ */
618 attributes ?: { [ key : string ] : string } ;
19+ /**
20+ * The value of the node.
21+ *
22+ * Ref: {@link Node.value}
23+ * @example `[color=red]Hello World![/color]` -> `red`
24+ */
725 value ?: string ;
826}
927
@@ -25,37 +43,55 @@ export abstract class BaseNode {
2543 * Converts the node into a textual representation of the node as BBCode.
2644 */
2745 abstract toString ( ) : string ;
46+
47+ /**
48+ * Gets the textual representation of the node as BBCode.
49+ * An alias for {@link BaseNode.toString}.
50+ */
51+ toBBCode ( ) : string {
52+ return this . toString ( ) ;
53+ }
2854}
2955
3056/**
3157 * An interface for nodes that can hold children.
3258 */
3359export interface ChildrenHolder {
3460 /**
35- * The children of the node.
61+ * The children of the node. A node can have any number of children of all types.
3662 */
3763 children : BaseNode [ ] ;
3864
3965 /**
40- * Adds a child to the node. If the node is a {@link TextNode}, attempts to flatten the node with an previous TextNode.
66+ * Adds a child to the node. If the node is a {@link TextNode}, attempts to flatten the node with a previous TextNode.
4167 * @param child The child to add.
4268 */
4369 addChild ( child : BaseNode ) : void ;
4470}
4571
72+ /**
73+ * A type for attributes.
74+ *
75+ * A BBCode attribute is a key-value pair, where the key is a string and the value is a string. A tag can have multiple attributes.
76+ * @example `[img width=100 height=100]https://example.com/image.png[/img]` -> `{ width: "100", height: "100" }`
77+ */
78+ export type AttributeType = { [ key : string ] : string } ;
79+
4680/**
4781 * An interface for nodes that can hold attributes.
4882 */
4983export interface AttributeHolder {
5084 /**
5185 * The attributes of the node.
86+ * This should be public, so it can be directly accessed and modified.
5287 */
53- attributes : { [ key : string ] : string } ;
88+ attributes : AttributeType ;
5489
5590 /**
56- * Adds an attribute to the node.
57- * @param key The name of the attribute.
91+ * Sets the attribute of the node.
92+ * @param key The key of the attribute.
5893 * @param value The value of the attribute.
94+ * @deprecated Use {@link AttributeHolder.attributes} instead.
5995 */
6096 setAttribute ( key : string , value : string ) : void ;
6197}
@@ -65,16 +101,29 @@ export interface AttributeHolder {
65101 */
66102export class Node extends BaseNode implements ChildrenHolder , AttributeHolder {
67103 /**
68- * The name of the tag. If a Node is used to represent a `[b]` tag, `name` will be `b`.
104+ * The name of the tag.
105+ *
106+ * @example `[b]Hello World![/b]` -> `b`
69107 */
70- name : string ;
108+ public name : string ;
71109
72- attributes : { [ key : string ] : string } ;
110+ /**
111+ * The attributes of the tag.
112+ *
113+ * Ref: {@link AttributeType}
114+ */
115+ public attributes : AttributeType ;
73116
74- children : BaseNode [ ] = [ ] ;
117+ /**
118+ * The children of the tag.
119+ *
120+ * Ref: {@link ChildrenHolder.children}
121+ */
122+ public children : BaseNode [ ] = [ ] ;
75123
76124 /**
77125 * Stores the [simple parameterized value](https://www.bbcode.org/reference.php) of the tag.
126+ *
78127 * @example `[b=red]Hello World![/b]` -> `red`
79128 */
80129 value ?: string ;
@@ -111,7 +160,9 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
111160
112161 /**
113162 * Sets the value of the node.
114- * @param value
163+ *
164+ * @param {string } value The value of the node.
165+ * @deprecated Use {@link Node.value} instead.
115166 */
116167 setValue ( value : string ) : void {
117168 this . value = value ;
@@ -121,6 +172,10 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
121172 this . attributes [ key ] = value ;
122173 }
123174
175+ /**
176+ * Makes the opening tag of the node. This will include the name, and all attributes.
177+ * @returns The opening tag of the node.
178+ */
124179 makeOpeningTag ( ) : string {
125180 let nodeString = `[${ this . name } ` ;
126181 if ( this . value ) {
@@ -153,8 +208,12 @@ export class TextNode extends BaseNode {
153208 */
154209 text : string ;
155210
156- name = "TextNode" ;
211+ readonly name = "TextNode" ;
157212
213+ /**
214+ * Create a new TextNode.
215+ * @param {string } text The text of the node.
216+ */
158217 constructor ( text : string ) {
159218 super ( ) ;
160219 this . text = text ;
@@ -177,6 +236,10 @@ export class RootNode extends BaseNode implements ChildrenHolder {
177236
178237 children : BaseNode [ ] ;
179238
239+ /**
240+ * Create a new RootNode.
241+ * @param {BaseNode[] } children The children of the node.
242+ */
180243 constructor ( children : BaseNode [ ] = [ ] ) {
181244 super ( ) ;
182245 this . children = children ;
0 commit comments