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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
const re = {
http: /.*?:\/\//g,
url: /(\s|^)((https?|ftp):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\w-\/\?\=\#\.])*/gi,
image: /\.(jpe?g|png|gif)$/,
email:
/(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/gi,
cloudmusic: /http:\/\/music\.163\.com\/#\/song\?id=(\d+)/i,
kickstarter:
/(https?:\/\/www\.kickstarter\.com\/projects\/\d+\/[a-zA-Z0-9_-]+)(\?\w+\=\w+)?/i,
youtube:
/https?:\/\/www\.youtube\.com\/watch\?v=([a-zA-Z0-9_-]+)(\?\w+\=\w+)?/i,
vimeo:
/https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/[^\/]*\/videos\/|album\/\d+\/video\/|video\/|)(\d+)(?:$|\/|\?)/i,
youku:
/https?:\/\/v\.youku\.com\/v_show\/id_([a-zA-Z0-9_\=-]+).html(\?\w+\=\w+)?(\#\w+)?/i,
};
/**
* AutoLink constructor function
*
* @param {String} text
* @param {Object} options
* @constructor
*/
function AutoLink(string, options = {}) {
this.string = options.safe ? safe_tags_replace(string) : string;
this.options = options;
this.attrs = "";
this.linkAttr = "";
this.imageAttr = "";
if (this.options.sharedAttr) {
this.attrs = getAttr(this.options.sharedAttr);
}
if (this.options.linkAttr) {
this.linkAttr = getAttr(this.options.linkAttr);
}
if (this.options.imageAttr) {
this.imageAttr = getAttr(this.options.imageAttr);
}
}
AutoLink.prototype = {
constructor: AutoLink,
/**
* call relative functions to parse url/email/image
*
* @returns {String}
*/
parse: function () {
var shouldReplaceImage = defaultTrue(this.options.image);
var shouldRelaceEmail = defaultTrue(this.options.email);
var shouldRelaceBr = defaultTrue(this.options.br);
var result = "";
if (!shouldReplaceImage) {
result = this.string.replace(re.url, this.formatURLMatch.bind(this));
} else {
result = this.string.replace(re.url, this.formatIMGMatch.bind(this));
}
if (shouldRelaceEmail) {
result = this.formatEmailMatch.call(this, result);
}
if (shouldRelaceBr) {
result = result.replace(/\r?\n/g, "<br />");
}
return result;
},
/**
* @param {String} match
* @returns {String} Offset 1
*/
formatURLMatch: function (match, p1) {
match = prepHTTP(match.trim());
if (this.options.cloudmusic || this.options.embed) {
if (match.indexOf("music.163.com/#/song?id=") > -1) {
return match.replace(
re.cloudmusic,
p1 +
'<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="http://music.163.com/outchain/player?type=2&id=$1&auto=1&height=66"></iframe>'
);
}
}
if (this.options.kickstarter || this.options.embed) {
if (re.kickstarter.test(match)) {
return match.replace(
re.kickstarter,
p1 +
'<iframe width="480" height="360" src="$1/widget/video.html" frameborder="0" scrolling="no"> </iframe>'
);
}
}
if (this.options.vimeo || this.options.embed) {
if (re.vimeo.test(match)) {
return match.replace(
re.vimeo,
p1 +
'<iframe width="500" height="281" src="https://player.vimeo.com/video/$1" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'
);
}
}
if (this.options.youtube || this.options.embed) {
if (re.youtube.test(match)) {
return match.replace(
re.youtube,
p1 +
'<iframe width="560" height="315" src="https://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>'
);
}
}
if (this.options.youku || this.options.embed) {
if (re.youku.test(match)) {
return match.replace(
re.youku,
p1 +
'<iframe height=498 width=510 src="http://player.youku.com/embed/$1" frameborder=0 allowfullscreen></iframe>'
);
}
}
var text = this.options.removeHTTP ? removeHTTP(match) : match;
return (
p1 +
'<a href="' +
match +
'"' +
this.attrs +
this.linkAttr +
">" +
text +
"</a>"
);
},
/**
* @param {String} match
* @param {String} Offset 1
*/
formatIMGMatch: function (match, p1) {
match = match.trim();
var isIMG = re.image.test(match);
if (isIMG) {
return (
p1 +
'<img src="' +
prepHTTP(match.trim()) +
'"' +
this.attrs +
this.imageAttr +
"/>"
);
}
return this.formatURLMatch(match, p1);
},
/**
* @param {String} text
*/
formatEmailMatch: function (text) {
return text.replace(
re.email,
'<a href="mailto:$&"' + this.attrs + this.linkAttr + ">$&</a>"
);
},
};
/**
* return true if undefined
* else return itself
*
* @param {Boolean} val
* @returns {Boolean}
*/
function defaultTrue(val) {
return typeof val === "undefined" ? true : val;
}
/**
* parse attrs from object
*
* @param {Object} obj
* @returns {Stirng}
*/
function getAttr(obj) {
var attr = "";
for (var key in obj) {
if (key) {
attr += " " + key + '="' + obj[key] + '"';
}
}
return attr;
}
/**
* @param {String} url
* @returns {String}
*/
function prepHTTP(url) {
if (url.substring(0, 4) !== "http" && url.substring(0, 2) !== "//") {
return "http://" + url;
}
return url;
}
/**
* @param {String} url
* @returns {String}
*/
function removeHTTP(url) {
return url.replace(re.http, "");
}
var tagsToReplace = {
"&": "&",
"<": "<",
">": ">",
};
/**
* Replace tag if should be replace
*
* @param {String} tag
* @returns {String}
*/
function replaceTag(tag) {
return tagsToReplace[tag] || tag;
}
/**
* Make string safe by replacing html tag
*
* @param {String} str
* @returns {String}
*/
function safe_tags_replace(str) {
return str.replace(/[&<>]/g, replaceTag);
}
/**
* return an instance of AutoLink
*
* @param {String} string
* @param {Object} options
* @returns {Object}
*/
function autoLink(string, options) {
return new AutoLink(string, options).parse();
}
//export default autoLink
|