|
| 1 | +export type Validate = (text: string, pos: number, self: LinkifyIt) => number | boolean; |
| 2 | + |
| 3 | +export interface FullRule { |
| 4 | + validate: string | RegExp | Validate; |
| 5 | + normalize?: ((match: Match) => void) | undefined; |
| 6 | +} |
| 7 | + |
| 8 | +export type Rule = string | FullRule; |
| 9 | + |
| 10 | +/** |
| 11 | + * An object, where each key/value describes protocol/rule: |
| 12 | + * |
| 13 | + * - __key__ - link prefix (usually, protocol name with `:` at the end, `skype:` |
| 14 | + * for example). `linkify-it` makes sure that prefix is not preceded with |
| 15 | + * alphanumeric char and symbols. Only whitespaces and punctuation allowed. |
| 16 | + * - __value__ - rule to check tail after link prefix |
| 17 | + * - _String_ - just alias to existing rule |
| 18 | + * - _Object_ |
| 19 | + * - _validate_ - validator function (should return matched length on success), |
| 20 | + * or `RegExp`. |
| 21 | + * - _normalize_ - optional function to normalize text & url of matched result |
| 22 | + * (for example, for `@twitter` mentions). |
| 23 | + */ |
| 24 | +export interface SchemaRules { |
| 25 | + [schema: string]: Rule; |
| 26 | +} |
| 27 | + |
| 28 | +export interface Options { |
| 29 | + /** |
| 30 | + * recognize URL-s without `http(s):` prefix. Default `true`. |
| 31 | + */ |
| 32 | + fuzzyLink?: boolean | undefined; |
| 33 | + /** |
| 34 | + * allow IPs in fuzzy links above. Can conflict with some texts |
| 35 | + * like version numbers. Default `false`. |
| 36 | + */ |
| 37 | + fuzzyIP?: boolean | undefined; |
| 38 | + /** |
| 39 | + * recognize emails without `mailto:` prefix. Default `true`. |
| 40 | + */ |
| 41 | + fuzzyEmail?: boolean | undefined; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Match result. Single element of array, returned by {@link LinkifyIt#match}. |
| 46 | + */ |
| 47 | +declare class Match { |
| 48 | + constructor(self: LinkifyIt, shift: number); |
| 49 | + |
| 50 | + /** |
| 51 | + * First position of matched string. |
| 52 | + */ |
| 53 | + index: number; |
| 54 | + /** |
| 55 | + * Next position after matched string. |
| 56 | + */ |
| 57 | + lastIndex: number; |
| 58 | + /** |
| 59 | + * Matched string. |
| 60 | + */ |
| 61 | + raw: string; |
| 62 | + /** |
| 63 | + * Prefix (protocol) for matched string. |
| 64 | + */ |
| 65 | + schema: string; |
| 66 | + /** |
| 67 | + * Normalized text of matched string. |
| 68 | + */ |
| 69 | + text: string; |
| 70 | + /** |
| 71 | + * Normalized url of matched string. |
| 72 | + */ |
| 73 | + url: string; |
| 74 | +} |
| 75 | + |
| 76 | +export type { Match }; |
| 77 | + |
| 78 | +declare class LinkifyIt { |
| 79 | + /** |
| 80 | + * new LinkifyIt(schemas, options) |
| 81 | + * - schemas (Object): Optional. Additional schemas to validate (prefix/validator) |
| 82 | + * - options (Object): { fuzzyLink|fuzzyEmail|fuzzyIP: true|false } |
| 83 | + * |
| 84 | + * Creates new linkifier instance with optional additional schemas. |
| 85 | + * Can be called without `new` keyword for convenience. |
| 86 | + * |
| 87 | + * By default understands: |
| 88 | + * |
| 89 | + * - `http(s)://...` , `ftp://...`, `mailto:...` & `//...` links |
| 90 | + * - "fuzzy" links and emails (example.com, foo@bar.com). |
| 91 | + */ |
| 92 | + constructor(schemas?: SchemaRules | Options, options?: Options); |
| 93 | + |
| 94 | + // Use overloads to provide contextual typing to `FullRule.normalize`, which is ambiguous with string.normalize |
| 95 | + /** |
| 96 | + * Add new rule definition. See constructor description for details. |
| 97 | + * |
| 98 | + * @param schema rule name (fixed pattern prefix) |
| 99 | + * @param definition schema definition |
| 100 | + */ |
| 101 | + add(schema: string, definition: string): this; |
| 102 | + add(schema: string, definition: FullRule | null): this; |
| 103 | + |
| 104 | + /** |
| 105 | + * Set recognition options for links without schema. |
| 106 | + */ |
| 107 | + set(options: Options): this; |
| 108 | + |
| 109 | + /** |
| 110 | + * Searches linkifiable pattern and returns `true` on success or `false` on fail. |
| 111 | + */ |
| 112 | + test(text: string): boolean; |
| 113 | + |
| 114 | + /** |
| 115 | + * Very quick check, that can give false positives. Returns true if link MAY BE |
| 116 | + * can exists. Can be used for speed optimization, when you need to check that |
| 117 | + * link NOT exists. |
| 118 | + */ |
| 119 | + pretest(text: string): boolean; |
| 120 | + |
| 121 | + /** |
| 122 | + * Similar to {@link LinkifyIt#test} but checks only specific protocol tail exactly |
| 123 | + * at given position. Returns length of found pattern (0 on fail). |
| 124 | + * |
| 125 | + * @param text text to scan |
| 126 | + * @param schema rule (schema) name |
| 127 | + * @param pos text offset to check from |
| 128 | + */ |
| 129 | + testSchemaAt(text: string, schema: string, pos: number): number; |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns array of found link descriptions or `null` on fail. We strongly |
| 133 | + * recommend to use {@link LinkifyIt#test} first, for best speed. |
| 134 | + */ |
| 135 | + match(text: string): Match[] | null; |
| 136 | + |
| 137 | + /** |
| 138 | + * Returns fully-formed (not fuzzy) link if it starts at the beginning |
| 139 | + * of the string, and null otherwise. |
| 140 | + */ |
| 141 | + matchAtStart(text: string): Match | null; |
| 142 | + |
| 143 | + /** |
| 144 | + * Load (or merge) new tlds list. Those are user for fuzzy links (without prefix) |
| 145 | + * to avoid false positives. By default this algorythm used: |
| 146 | + * |
| 147 | + * - hostname with any 2-letter root zones are ok. |
| 148 | + * - biz|com|edu|gov|net|org|pro|web|xxx|aero|asia|coop|info|museum|name|shop|рф |
| 149 | + * are ok. |
| 150 | + * - encoded (`xn--...`) root zones are ok. |
| 151 | + * |
| 152 | + * If list is replaced, then exact match for 2-chars root zones will be checked. |
| 153 | + * |
| 154 | + * @param list list of tlds |
| 155 | + * @param keepOld merge with current list if `true` (`false` by default) |
| 156 | + */ |
| 157 | + tlds(list: string | string[], keepOld?: boolean): this; |
| 158 | + |
| 159 | + /** |
| 160 | + * Default normalizer (if schema does not define it's own). |
| 161 | + */ |
| 162 | + normalize(match: Match): void; |
| 163 | + |
| 164 | + /** |
| 165 | + * Override to modify basic RegExp-s. |
| 166 | + */ |
| 167 | + onCompile(): void; |
| 168 | + |
| 169 | + re: { |
| 170 | + [key: string]: RegExp; |
| 171 | + }; |
| 172 | +} |
| 173 | + |
| 174 | +export default LinkifyIt; |
0 commit comments