|
| 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; |
0 commit comments