Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3cffc2

Browse files
committed
feat: Add an alias toBBCode and improve docs
1 parent 6f77b53 commit a3cffc2

File tree

2 files changed

+76
-13
lines changed

2 files changed

+76
-13
lines changed

src/node.ts

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,26 @@
22
* Parameters for constructing a new Node object.
33
*/
44
export 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
*/
3359
export 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
*/
4983
export 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
*/
66102
export 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;

src/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export default class Parser {
238238
// We found the end of the tag.
239239
buildingValue = false;
240240
const currentTag = new Node({ name: currentTagName });
241-
currentTag.setValue(currentTagValue);
241+
currentTag.value = currentTagValue;
242242
currentStack.push(currentTag);
243243
buildingTagName = false;
244244
buildingText = true;
@@ -273,7 +273,7 @@ export default class Parser {
273273
attributes: currentTagAttributes,
274274
});
275275
if (currentTagValue) {
276-
currentTag.setValue(currentTagValue);
276+
currentTag.value = currentTagValue;
277277
}
278278

279279
currentStack.push(currentTag);

0 commit comments

Comments
 (0)