Skip to content

Commit d152bf8

Browse files
authored
🤖 Merge PR DefinitelyTyped#69355 [markdown-it,mdurl,linkify-it,markdown-it-emoji] update type definitions by @brc-dd
1 parent 0f5b635 commit d152bf8

72 files changed

Lines changed: 2757 additions & 1588 deletions

Some content is hidden

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

Comments
 (0)