forked from alexcorvi/anchorme.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
527 lines (462 loc) · 19.6 KB
/
index.html
File metadata and controls
527 lines (462 loc) · 19.6 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Anchorme: Javascript library to detect links / URLs / Emails in text and convert them to clickable HTML anchor links</title>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<link href="./assets/style.min.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.3.0/bootbox.min.js"></script>
<script src="./assets/script.js" type="text/javascript"></script>
<script src="./dist-browser/anchorme.js" type="text/javascript"></script>
</head>
<body>
<!-- Main navigation
=========================================== -->
<header class="navbar navbar-static-top bs-docs-nav" id="top">
<div class="container">
<div class="navbar-header" role="banner">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://alexcorvi.github.io/anchorme.js/">Anchorme</a>
</div>
<nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/alexcorvi/anchorme.js">View on GitHub</a>
</li>
</ul>
</nav>
</div>
</header>
<!-- Header
=========================================== -->
<div class="bs-docs-masthead" id="content" style="background: linear-gradient(135deg, #673AB7, #3F51B5);">
<div class="container">
<h1 class="home-title">Anchorme</h1>
<p class="lead" style="font-size:18px;margin-top:25px">Tiny, fast, efficient, feature rich Javascript library to detect links / URLs / Emails in text and convert them to clickable HTML anchor links</p>
<p class="lead">
<a data-bootbox="download-popup" data-bootbox-size="large" class="btn btn-outline-inverse btn-lg">Download</a>
</p>
</div>
</div>
<div id="download-popup" title="Download" class="hidden">
<div class="row">
<div class="col-xs-12">
<div class="panel panel-default">
<div class="panel-heading">Install with NPM</div>
<div class="panel-body">
<p>You can get this library via NPM</p>
<pre><code>$ npm i --save anchorme</code></pre>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-heading">Download full version</div>
<div class="panel-body">
<p>Compiled version into single file.</p>
<a href="https://raw.githubusercontent.com/alexcorvi/anchorme.js/gh-pages/dist-browser/anchorme.js">Download</a>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-heading">Download minified version</div>
<div class="panel-body">
<p>Compiled and minified version.</p>
<a href="https://raw.githubusercontent.com/alexcorvi/anchorme.js/gh-pages/dist-browser/anchorme.min.js">Download</a>
</div>
</div>
</div>
</div>
</div>
<div class="container bs-docs-container">
<div class="row">
<div class="col-md-9" role="main">
<section class="bs-docs-section">
<h1 id="overview" class="page-header">Overview</h1>
<blockquote>
Javascript library to detect links / URLs / Emails in text and convert them to clickable HTML anchor links. You can also use it for extracting a list of valid links and emails out of text or use it as a validation library for emails, URLs and IPs.
</blockquote>
<h3 id="features">Features</h3>
<ul>
<li>
<strong>Sensitivity</strong>: It’s Highly sensitive with the least false positives. <br>
<ul>
<li>It validates URLs and Emails against full <strong>IANA</strong> list.</li>
<li>Validates <strong>port numbers</strong> (if present).</li>
<li>Validates <strong>IP octet numbers</strong> (if present).</li>
</ul>
</li>
<li><strong>Robustness</strong>: <br>
<ul>
<li><strong>Skips HTML</strong>, so it doesn’t break your HTML if it had a URL as an attribute for an element.</li>
<li>Links with or without protocols.</li>
<li>Works with IPs, FTPs, Emails and files.</li>
<li>Can detect parenthesis and quotation marks as part of the URL or as a surrounding to the URL.</li>
</ul>
</li>
<li>
<strong>Fast</strong>: It’s definitely fast! processing the full H.G. Wells "<strong>The Time Machine</strong>" novel (633 KB text file) with over <strong>1500</strong> URLs inserted at random places takes about <strong>3</strong> seconds.
</li>
<li>
<strong>Light Weight</strong>: Although it’s a feature rich library with a full IANA list included, it’s only <strong>6KB</strong> when minified and gzipped.
</li>
</ul>
</section>
<section class="bs-docs-section">
<h1 id="demo" class="page-header">Demo</h1>
<div class="row">
<div class="col-md-6">
<p><b>Input:</b></p>
<textarea onkeydown="refresh();" onkeyup="refresh();" onchange="refresh();" id="input" class="form-control" style="min-width:100%;max-width:100%;min-height:666px"></textarea>
</div>
<div class="col-md-6">
<p><b>Output:</b></p>
<pre class="output code text"><code id="output"></code></pre>
<style type="text/css" media="screen">
#output a {
background: #673AB7;
padding: 3px;
color: #fff;
margin: 0;
border-radius: 3px;
}
.output {
line-height: 20px;
padding: 15px;
font-size: 11px;
}
</style>
</div>
</div>
<script type="text/javascript">
var initialInput = `Examples:\nSimply a domain:\nwww.google.com\nDomain with port:\nwww.google.com:8000\nURL with query:\nwww.google.com/?key=value\nDomain with international TLD:\ngithub.io\nEmail address:\[email protected]\nIP address:\n127.0.0.1:8000/somethinghere\nHTML elements won't break:\n<img src='http://dummyimage.com/50'>\nCapital case:\nFTP://GOOGLE.COM\nmixed case:\nyoutube.com/watch?v=pS-gbqbVd8c\nLinks with parenthesis:\nen.c.org/a_(b)\nLinks inside parenthesis:\n(www.google.com)\nLinks with paranethisis AND inside paranthesis:\n(en.c.org/a_(x)_(y))\nAnd even emojis:\nhttp://🐌🍏⌚✨😐😍🐸🍑.🍕💩.ws\nAnd non English letters:\nhttps://ka.wikipedia.org/wiki/მთავარი_გვერდი\n\n`;
document.getElementById("input").value = initialInput;
refresh();
/*anchor me*/
function refresh(){
val = document.getElementById("input").value;
document.getElementById("output").innerHTML = anchorme(val);
}
</script>
</section>
<section class="bs-docs-section">
<h1 id="installation" class="page-header">Installation</h1>
<p>You can get this library via NPM</p>
<pre><code>npm i --save anchorme</code></pre>
<p>Or download the single filed compiled version <a href="https://github.com/alexcorvi/anchorme.js">here</a>.</p>
</section>
<section class="bs-docs-section">
<h1 id="usgae" class="page-header">Usage</h1>
<h3 id="basic-usage">Basic usage</h3>
<p>The anchorme library exports one function that can receives one or two parameters:</p>
<pre class="sml"><code>anchorme(input:string,[options:object])</code></pre>
<p>The example below demonstrates basic usage with no options</p>
<pre class="code"><code>
var anchorme = require("anchorme").default; // if installed via NPM
import anchorme from "anchorme"; // ES6 / Typescript style imports
var someText = "this is a text with a link www.github.com ..";
var result = anchorme(someText);
// You can also pass few options (totally optional)
// to add attribute, truncate results, setting default protocols and more
// read below
</code></pre>
<hr>
<h3 id="truncation">Truncation</h3>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value type</th>
<th>Default value</th>
</tr>
</thead>
<tbody>
<tr>
<td><pre class="sml"><code>truncate</code></pre></td>
<td><pre class="sml"><code>Number|[Number,Number]</code></pre></td>
<td><pre class="sml"><code>Infinity</code></pre></td>
</tr>
</tbody>
</table>
<p>This will convert a long link like this:
<a href="https://raw.githubusercontent.com/alexcorvi/anchorme.js/gh-pages/src/tests/hasprotocol.js">https://raw.githubusercontent.com/alexcorvi/anchorme.js/gh-pages/src/tests/hasprotocol.js</a>
to this:
<a href="https://raw.githubusercontent.com/alexcorvi/anchorme.js/gh-pages/src/tests/hasprotocol.js">https://raw.githubusercontent.com/alexcorv...</a></p>
<p>However, if you wanted to remove characters from the middle instead of the end to produce a link like this: <a href="https://raw.githubusercontent.com/alexcorvi/anchorme.js/gh-pages/src/tests/hasprotocol.js">raw.githubusercontent.com/.../hasprotocol.js</a>; you can pass an array of two numbers first number is the number of desired characters at the beginning and second number is the number of desired characters at the end.
</p>
<p><b>Example:</b></p>
<pre><code>
anchorme(string,{
truncate:40 // 40 is the maximum length of the viewed link
});
anchorme(string,{
truncate:[26,15],
// means 26 characters from the beginning
// and 15 characters from the end
});
</code></pre>
<p><b>JSFiddle Demo</b></p>
<script async src="//jsfiddle.net/alexcorvi/fouwnjkx/2/embed/js,result/"></script>
<hr>
<h3 id="adding-attributes">Adding attributes</h3>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value type</th>
<th>Default value</th>
</tr>
</thead>
<tbody>
<tr>
<td><pre class="sml"><code>attributes</code></pre></td>
<td><pre class="sml"><code>Array<Object|Function></code></pre></td>
<td><pre class="sml"><code>[]</code></pre></td>
</tr>
</tbody>
</table>
<p>You can add attributes to the links produced by anchorme. using the `attributes` key in the options object. The value of this option should be an array of the attributes you'd like to pass.</p>
<p>Each value of this array can be either an <strong>object</strong> describing the attribute or a <strong>function</strong> that returns an <strong>object</strong> that describes the attribute</p>
<pre><code>
anchorme(string,{
attributes:[ // attributes is an array
// an object that describes the attribute
// will produce: class="link"
{
name:"class",
value:"link"
},
// a function that returns an object
function(urlObj){
if(urlObj.reason === "ip") return {name:"class",value:"ip-link"};
},
function(urlObj){
if(urlObj.protocol !== "mailto:") return {name:"target",value:"blank"};
// following conditions can also be used:
// if(urlObj.raw.indexOf("@") > 0) return {name:"target",value:"blank"};
// if(urlObj.reason !== "email") return {name:"target",value:"blank"};
}
]
});
</code></pre>
<p>Where <code>urlObj</code> is an object containing detailed info about the link in question. The example above will add `ip-link` class to all the links that are IPs, and add `target='_blank'` to all the links that are not emails.</p>
<p>If you log the <code>urlObj</code> you'll get something similar to this:</p>
<pre><code>
{
// the reason this fragment
// was detected
// possible reasons: "file", "url", "ip", "email"
reason: "email",
// the protocol that the link came with
// or the protocol that was added to the link
protocol: "mailto:",
// the link (without any modification)
raw: "[email protected]",
// the encoded version of the link
// i.e. non-Latin characters -> URI encoding
// also doesn't have a protocol (if it came with any)
encoded: "[email protected]",
}
</code></pre>
<p><b>JSFiddle Demo</b></p>
<script async src="//jsfiddle.net/alexcorvi/y1qL2gmc/4/embed/js,result/"></script>
<hr>
<h3 id="default-protocol">Default protocol</h3>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value type</th>
<th>Default value</th>
</tr>
</thead>
<tbody>
<tr>
<td><pre class="sml"><code>defaultProtocol</code></pre></td>
<td><pre class="sml"><code>String|Function</code></pre></td>
<td><pre class="sml"><code>"http://"</code></pre></td>
</tr>
</tbody>
</table>
<p>If the link came without protocol, like <code>www.google.com</code> then anchorme will add the <code>http://</code> by default. However you can set your own default protocol.</p>
<pre class="javascript"><code>
anchorme(string,{
defaultProtocol:"ftp://",
// ... or anything
});
</code></pre>
<p>In some cases, you might want the protocol to be set conditionally. Anchorme allows you to pass a function as the `defaultProtocol` and uses whatever this function returns.</p>
<pre class="javascript"><code>
anchorme(string,{
defaultProtocol:function(url){
// where url is like: "www.google.com"
if(url.indexOf("google") > 0) return "https://";
else return "http://";
},
});
</code></pre>
<p>
Please note that the default protocol does <strong>not</strong> apply on detected emails. Detected emails will always have <code>mailto:</code> as their protocol.
</p>
<p><b>JSFiddle Demo</b></p>
<script async src="//jsfiddle.net/alexcorvi/fu0k9ata/1/embed/js,result/"></script>
<hr>
<h3 id="excluding">Excluding</h3>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value type</th>
<th>Default value</th>
</tr>
</thead>
<tbody>
<tr>
<td><pre class="sml"><code>emails</code></pre></td>
<td><pre class="sml"><code>Boolean</code></pre></td>
<td><pre class="sml"><code>true</code></pre></td>
</tr>
<tr>
<td><pre class="sml"><code>urls</code></pre></td>
<td><pre class="sml"><code>Boolean</code></pre></td>
<td><pre class="sml"><code>true</code></pre></td>
</tr>
<tr>
<td><pre class="sml"><code>ips</code></pre></td>
<td><pre class="sml"><code>Boolean</code></pre></td>
<td><pre class="sml"><code>true</code></pre></td>
</tr>
<tr>
<td><pre class="sml"><code>files</code></pre></td>
<td><pre class="sml"><code>Boolean</code></pre></td>
<td><pre class="sml"><code>true</code></pre></td>
</tr>
</tbody>
</table>
<p>You can exclude IPs/Emails/URLs/Files like this:</p>
<pre><code>
anchorme(string,{
emails:false,
urls:false,
ips:false,
files:false
})
// the example above won't do anything to your string
// since you're excluding every possible link that can be detected
</code></pre>
<p><b>JSFiddle Demo</b></p>
<script async src="//jsfiddle.net/alexcorvi/gydnngrz/1/embed/js,result/"></script>
<hr>
<h3 id="custom-exclusion">Custom Exclusion</h3>
<table>
<thead>
<tr>
<th>Key</th>
<th>Value type</th>
<th>Default value</th>
</tr>
</thead>
<tbody>
<tr>
<td><pre class="sml"><code>exclude</code></pre></td>
<td><pre class="sml"><code>function</code></pre></td>
<td><pre class="sml"><code>undefined</code></pre></td>
</tr>
</tbody>
</table>
<p>The aforementioned exclusion options exclude URLs by their protocol. However, you might want to exclude URLs for any other reason.</p>
<p>Each identified URL will go through this function. The function can be used to test the URLs and return a truthy value (<code>true</code> or <code>1</code> or <code>"non-empty string"</code> ... etc) to exclude or a falsy value (<code>false</code> or <code>1</code> or <code>undefined</code> ... etc) to include.</p>
<pre><code>
// this example will exclude URLs ends with .zip
anchorme(string,{
exclude:function(URLObj){
if(URLObj.raw.endsWith(".zip")) return true;
// returning "truthy" value will cause
// exclusion of the URL
}
});
</code></pre>
<p><b>JSFiddle Demo</b></p>
<script async src="//jsfiddle.net/gydnngrz/3/embed/js,result/"></script>
</section>
<section class="bs-docs-section">
<h1 id="additional-features" class="page-header">Additional Features</h1>
<h3 id="validation">Validation</h3>
<pre class="sml"><code>anchorme.validate.ip(input:String)</code></pre><br>
<pre class="sml"><code>anchorme.validate.email(input:String)</code></pre><br>
<pre class="sml"><code>anchorme.validate.url(input:String)</code></pre><br>
<p>You can use <strong>anchorme</strong> to validate emails, URLs and IPs.</p>
<p><b>Example:</b></p>
<pre class="javascript"><code>
anchorme.validate.ip("1.1.1.1:3000/something");
// returns true
anchorme.validate.ip("1.1.1.500:3000/something");
// returns false
anchorme.validate.email("[email protected]");
// returns true
anchorme.validate.url("google.co.uk");
// returns true
</code></pre>
<p><b>JSFiddle Demo:</b></p>
<script async src="//jsfiddle.net/alexcorvi/L0dex4q9/1/embed/js,result/"></script>
<hr>
<h3 id="listing">Listing</h3>
<p>Although anchorme was authored to transform URLs in text strings to a click-able HTML anchor tags, passing `true` to `list` property in options will change the library's behavior and instead of returning a text with an HTML tags it will only return an array of valid URLs.</p>
<pre><code>
anchorme(myText,{
list:true
});
// returns an array of detected valid URLs
</code></pre>
<p><b>JSFiddle Demo:</b></p>
<script async src="//jsfiddle.net/alexcorvi/z3g3haL1/1/embed/js,result/"></script>
</section>
</div>
<div class="col-md-3" role="complementary">
<div class="bs-docs-sidebar hidden-print">
<ul class="nav bs-docs-sidenav">
</ul>
<a class="back-to-top" href="#top">Back to top</a>
</div>
</div>
</div>
</div>
<footer class="bs-docs-footer" role="contentinfo">
<div class="container">
<div class="bs-docs-social">
<ul class="bs-docs-social-buttons">
<li>
<iframe class="github-btn" src="//ghbtns.com/github-btn.html?user=alexcorvi&repo=anchorme.js&type=fork&count=true" width="90" height="20" title="Fork on GitHub"></iframe>
</li>
<li>
<iframe class="github-btn" src="//ghbtns.com/github-btn.html?user=alexcorvi&repo=anchorme.js&type=star&count=true" width="90" height="20" title="Star on GitHub"></iframe>
</li>
</ul>
</div>
<p>Licensed under <a href="http://opensource.org/licenses/MIT" target="_blank">MIT License</a></p>
<p>Documentation template by <a href="http://getbootstrap.com">Bootstrap team</a></p>
<ul class="bs-docs-footer-links muted">
<li>·</li>
<li><a href="https://github.com/alexcorvi/anchorme.js">GitHub</a></li>
<li>·</li>
<li><a href="https://github.com/alexcorvi/anchorme.js/issues?state=open">Issues</a></li>
</ul>
</div>
</footer>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/highlight.min.js" type="text/javascript"></script>
<script type="text/javascript">hljs.initHighlightingOnLoad();</script>
</body>
</html>