Skip to content

Commit 2656554

Browse files
authored
feat(bbcode-to-react): Add declarations and tests (DefinitelyTyped#46891)
1 parent 8c885b6 commit 2656554

File tree

4 files changed

+321
-0
lines changed

4 files changed

+321
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import parser, { Tag, Parser } from 'bbcode-to-react';
2+
3+
// $ExpectType void
4+
parser.registerTag('br', Tag);
5+
6+
// $ExpectType string
7+
parser.toHTML('[B]strong[/B]');
8+
9+
// $ExpectType ReactNode
10+
parser.toReact('[B]strong[/B]');
11+
12+
new (class extends Parser {
13+
testParser() {
14+
// $ExpectType { [name: string]: Tag<{ linkify: boolean; }>; }
15+
this.tags;
16+
17+
// $ExpectType Renderer<{ linkify: boolean; }>
18+
this.renderer;
19+
20+
// $ExpectType Tag<{ linkify: boolean; }>
21+
this.parse('');
22+
}
23+
24+
testRenderer() {
25+
// $ExpectType { linkify: boolean; }
26+
this.renderer.options;
27+
28+
// $ExpectType { linkify: boolean; }[]
29+
this.renderer.contexts;
30+
31+
// $ExpectType () => string[]
32+
this.renderer.context({ linkify: false }, () => ['']);
33+
34+
// $ExpectType string
35+
this.renderer.escape('');
36+
37+
// $ExpectType string
38+
this.renderer.linkify('');
39+
40+
// $ExpectType string
41+
this.renderer.strip('');
42+
43+
// $ExpectType string
44+
this.renderer.cosmeticReplace('');
45+
}
46+
47+
testTag() {
48+
new (class extends Tag {
49+
tagMethod() {
50+
// @ExpectedType string
51+
this.name;
52+
53+
// @ExpectedType ReactNode
54+
this.parent;
55+
56+
// @ExpectedType String
57+
this.text;
58+
59+
// @ExpectedType object
60+
this.params;
61+
62+
// @ExpectedType ReactNode
63+
this.children;
64+
65+
// @ExpectedType ReactNode
66+
this.getComponents();
67+
68+
// @ExpectedType string
69+
this.getContent();
70+
71+
// @ExpectedType string
72+
this.toHTML();
73+
74+
// @ExpectedType ReactNode
75+
this.toReact();
76+
}
77+
})(this.renderer);
78+
}
79+
})();
80+
81+
new (class extends Parser<{ canEscape: boolean }> {
82+
testParser() {
83+
// $ExpectType Renderer<{ canEscape: boolean; }>
84+
this.renderer;
85+
}
86+
87+
testRenderer() {
88+
// $ExpectType { canEscape: boolean; }
89+
this.renderer.options;
90+
91+
// $ExpectType { canEscape: boolean; }[]
92+
this.renderer.contexts;
93+
}
94+
})();

types/bbcode-to-react/index.d.ts

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// Type definitions for bbcode-to-react 0.2
2+
// Project: https://github.com/JimLiu/bbcode-to-react#readme
3+
// Definitions by: hkleungai <https://github.com/hkleungai>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
// Minimum TypeScript Version: 3.5
6+
7+
import { ReactNode } from 'react';
8+
9+
/**
10+
* A generic building block for Tag class and Parser class.
11+
* An optional interface can be provided for constructing the class
12+
* The class is NOT exported from the library.
13+
*/
14+
declare class Renderer<optionType = { linkify: boolean }> {
15+
/**
16+
* Initializing the class members "options" and "renderer"
17+
* @param options An object to be injected into the class memeber "options"
18+
*/
19+
constructor(options: optionType);
20+
/**
21+
* An object storing some essential flags and params,
22+
* with default value { linkify: false }
23+
*/
24+
options: optionType;
25+
/**
26+
* An array storing objects with type "optionType"
27+
*/
28+
contexts: optionType[];
29+
/**
30+
* Injecting incoming context to class and return the input function
31+
* @param context An array storing objects with type "optionType"
32+
* @param func: A function to be returned
33+
* @returns A function returning a string array
34+
*/
35+
context: (context: optionType, func: () => string[]) => () => string[];
36+
/**
37+
* Process incoming value by regexp and return the output
38+
* @param value Input to be processed
39+
* @returns A processed string
40+
*/
41+
escape: (value: string) => string;
42+
/**
43+
* Process incoming value by regexp and return the output
44+
* @param value Input to be processed
45+
* @returns A processed string
46+
*/
47+
linkify: (value: string) => string;
48+
/**
49+
* Process incoming value by regexp and return the output
50+
* @param value Input to be processed
51+
* @returns A processed string
52+
*/
53+
strip: (value: string) => string;
54+
/**
55+
* Process incoming value by regexp and return the output
56+
* @param value Input to be processed
57+
* @returns A processed string
58+
*/
59+
cosmeticReplace: (value: string) => string;
60+
}
61+
62+
/**
63+
* An interface for class member in "Tag"
64+
*/
65+
interface TagType {
66+
/**
67+
* Tag name, with type string
68+
*/
69+
name: string;
70+
/**
71+
* The parent of a tag object, with type ReactNode
72+
*/
73+
parent: ReactNode;
74+
/**
75+
* The inner text of a tag object, with type string
76+
*/
77+
text: string;
78+
/**
79+
* A "params" object for inner processing
80+
*/
81+
params: object;
82+
/**
83+
* The children of a tag object, with type ReactNode array
84+
*/
85+
children: ReactNode[];
86+
}
87+
88+
/**
89+
* A class designed for handling bbcode.
90+
* The Tag class mostly is extended into another customized class,
91+
* and seldom an instance would be needed for bbcode parsing.
92+
* The class is exported from the library.
93+
*/
94+
declare class Tag<T = { linkify: boolean }> {
95+
/**
96+
* Initializing the class members
97+
* @param renderer A Renderer instance to be injected
98+
* @param settings
99+
* A "TagType" object (without children attribute)
100+
* for initializing the class member "params"
101+
*/
102+
constructor(renderer: Renderer<T>, settings?: Partial<Omit<TagType, 'children'>>);
103+
/**
104+
* Tag name, with type string
105+
*/
106+
protected name: TagType['name'];
107+
/**
108+
* The parent of a tag object, with type ReactNode
109+
*/
110+
protected parent: TagType['parent'];
111+
/**
112+
* The inner text of a tag object, with type string
113+
*/
114+
protected text: TagType['text'];
115+
/**
116+
* A "params" object for inner processing
117+
*/
118+
protected params: TagType['params'];
119+
/**
120+
* The children of a tag object, with type ReactNode array
121+
*/
122+
protected children: TagType['children'];
123+
/**
124+
* Getter method of the "children" member of the Tag instance
125+
* @returns A ReactNode array representing the "children" member of the Tag instance
126+
*/
127+
protected getComponents(): ReactNode[];
128+
/**
129+
* Getter method for inner text of the Tag instance
130+
* @param raw If true, also convert a plain url to an "a" tag in text form
131+
* @returns Inner text of the Tag instance
132+
*/
133+
protected getContent(raw?: boolean): string;
134+
/**
135+
* Method for converting a tag instance to HTML
136+
* @returns a HTML in string form
137+
*/
138+
protected toHTML(): string;
139+
/**
140+
* Method for converting a tag instance to react component
141+
* @returns a react component
142+
*/
143+
protected toReact(): ReactNode;
144+
}
145+
146+
/**
147+
* A class designed for parsing bbcode.
148+
* The class exposes some public function
149+
* to make the conversion between bbcode and html/ReactNode possible.
150+
* The class is exported from the library,
151+
* and a class instance with pre-defined tags collections is exported as default.
152+
*/
153+
declare class Parser<T = { linkify: boolean }> {
154+
/**
155+
* Initializing the class members
156+
* @param allowedTags
157+
* An object to be combined with a set of default tags
158+
* for forming the class member "tag"
159+
*/
160+
constructor(allowedTags?: object | null);
161+
/**
162+
* A tags collection for a parser instance
163+
*/
164+
protected tags: { [name: string]: Tag };
165+
/**
166+
* A renderer member for a parser instance
167+
*/
168+
protected renderer: Renderer<T>;
169+
/**
170+
* Method for parsing an input string into a tag instance
171+
* @param input A string to be parsed
172+
* @returns a tag instance
173+
*/
174+
protected parse(input: string): Tag;
175+
/**
176+
* Method for adding an user-defined tag instance into the parser
177+
* @param name A tag name to be provided
178+
* @param tag A tag instance to be provided
179+
*/
180+
registerTag(name: string, tag: typeof Tag): void;
181+
/**
182+
* Method for converting a bbcode to HTML
183+
* @param input a bbcode string
184+
* @returns a HTML in string form
185+
*/
186+
toHTML(input: string): string;
187+
/**
188+
* Method for converting a bbcode to react component
189+
* @param input a bbcode string
190+
* @returns a react component
191+
*/
192+
toReact(input: string): ReactNode;
193+
}
194+
195+
/**
196+
* A defualt parser instance to be exported in this library
197+
*/
198+
declare const parser: Parser;
199+
200+
export { Tag, Parser };
201+
export default parser;

types/bbcode-to-react/tsconfig.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"baseUrl": "../",
12+
"typeRoots": [
13+
"../"
14+
],
15+
"types": [],
16+
"noEmit": true,
17+
"forceConsistentCasingInFileNames": true
18+
},
19+
"files": [
20+
"index.d.ts",
21+
"bbcode-to-react-tests.ts"
22+
]
23+
}

types/bbcode-to-react/tslint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "dtslint/dt.json"
3+
}

0 commit comments

Comments
 (0)