forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautolinker-tests.ts
153 lines (114 loc) · 6.42 KB
/
autolinker-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
///<reference path="./autolinker.d.ts"/>
import AutolinkerCJS = require('autolinker');
() => {
let linkedText1 = Autolinker.link( "Check out google.com" );
new Autolinker();
let autolinker1 = new Autolinker( { className: "myLink" } );
let textToAutoLink = 'text';
let linkedText2 = autolinker1.link( textToAutoLink );
let linkedText3 = Autolinker.link( "Check out google.com", { className: "myLink" } );
let linkedText4 = Autolinker.link( "Check out google.com", { newWindow: false } );
let linkedText5 = Autolinker.link( "http://www.yahoo.com/some/long/path/to/a/file", { truncate: 25, newWindow: false } );
let myTextEl = document.getElementById( 'text' );
myTextEl.innerHTML = Autolinker.link( myTextEl.innerHTML );
let autolinker2 = new Autolinker( { newWindow: false, truncate: 25 } );
autolinker2.link( "Check out http://www.yahoo.com/some/long/path/to/a/file" );
// Produces: "Check out <a href="http://www.yahoo.com/some/long/path/to/a/file">yahoo.com/some/long/pat..</a>"
autolinker2.link( "Go to www.google.com" );
// Produces: "Go to <a href="http://www.google.com">google.com</a>"
let input = "..."; // string with URLs, Email Addresses, Twitter Handles, and Hashtags
let linkedText6 = Autolinker.link( input, {
replaceFn : function( autolinker, match ) {
console.log( "href = ", match.getAnchorHref() );
console.log( "text = ", match.getAnchorText() );
switch( match.getType() ) {
case 'url' :
console.log( "url: ", match.getUrl() );
if( match.getUrl().indexOf( 'mysite.com' ) === -1 ) {
let tag = autolinker.getTagBuilder().build( match ); // returns an `Autolinker.HtmlTag` instance, which provides mutator methods for easy changes
tag.setAttr( 'rel', 'nofollow' );
tag.addClass( 'external-link' );
return tag;
} else {
return true; // let Autolinker perform its normal anchor tag replacement
}
case 'email' :
let email = match.getEmail();
console.log( "email: ", email );
if( email === "my@own.address" ) {
return false; // don't auto-link this particular email address; leave as-is
} else {
return; // no return value will have Autolinker perform its normal anchor tag replacement (same as returning `true`)
}
case 'phone' :
let phoneNumber = match.getPhoneNumber();
console.log( phoneNumber );
return '<a href="http://newplace.to.link.phone.numbers.to/">' + phoneNumber + '</a>';
case 'twitter' :
let twitterHandle = match.getTwitterHandle();
console.log( twitterHandle );
return '<a href="http://newplace.to.link.twitter.handles.to/">' + twitterHandle + '</a>';
case 'hashtag' :
let hashtag = match.getHashtag();
console.log( hashtag );
return '<a href="http://newplace.to.link.hashtag.handles.to/">' + hashtag + '</a>';
}
}
} );
}
() => {
let linkedText1 = AutolinkerCJS.link( "Check out google.com" );
new AutolinkerCJS();
let autolinker1 = new AutolinkerCJS( { className: "myLink" } );
let textToAutoLink = 'text';
let linkedText2 = autolinker1.link( textToAutoLink );
let linkedText3 = AutolinkerCJS.link( "Check out google.com", { className: "myLink" } );
let linkedText4 = AutolinkerCJS.link( "Check out google.com", { newWindow: false } );
let linkedText5 = AutolinkerCJS.link( "http://www.yahoo.com/some/long/path/to/a/file", { truncate: 25, newWindow: false } );
let myTextEl = document.getElementById( 'text' );
myTextEl.innerHTML = AutolinkerCJS.link( myTextEl.innerHTML );
let autolinker2 = new AutolinkerCJS( { newWindow: false, truncate: 25 } );
autolinker2.link( "Check out http://www.yahoo.com/some/long/path/to/a/file" );
// Produces: "Check out <a href="http://www.yahoo.com/some/long/path/to/a/file">yahoo.com/some/long/pat..</a>"
autolinker2.link( "Go to www.google.com" );
// Produces: "Go to <a href="http://www.google.com">google.com</a>"
let input = "..."; // string with URLs, Email Addresses, Twitter Handles, and Hashtags
let linkedText6 = AutolinkerCJS.link( input, {
replaceFn : function( autolinker, match ) {
console.log( "href = ", match.getAnchorHref() );
console.log( "text = ", match.getAnchorText() );
switch( match.getType() ) {
case 'url' :
console.log( "url: ", match.getUrl() );
if( match.getUrl().indexOf( 'mysite.com' ) === -1 ) {
let tag = autolinker.getTagBuilder().build( match ); // returns an `AutolinkerCJS.HtmlTag` instance, which provides mutator methods for easy changes
tag.setAttr( 'rel', 'nofollow' );
tag.addClass( 'external-link' );
return tag;
} else {
return true; // let AutolinkerCJS perform its normal anchor tag replacement
}
case 'email' :
let email = match.getEmail();
console.log( "email: ", email );
if( email === "my@own.address" ) {
return false; // don't auto-link this particular email address; leave as-is
} else {
return; // no return value will have AutolinkerCJS perform its normal anchor tag replacement (same as returning `true`)
}
case 'phone' :
let phoneNumber = match.getPhoneNumber();
console.log( phoneNumber );
return '<a href="http://newplace.to.link.phone.numbers.to/">' + phoneNumber + '</a>';
case 'twitter' :
let twitterHandle = match.getTwitterHandle();
console.log( twitterHandle );
return '<a href="http://newplace.to.link.twitter.handles.to/">' + twitterHandle + '</a>';
case 'hashtag' :
let hashtag = match.getHashtag();
console.log( hashtag );
return '<a href="http://newplace.to.link.hashtag.handles.to/">' + hashtag + '</a>';
}
}
} );
}