(text, targets, is_touch)
| 475 | /* * Turns plain text links into real links |
| 476 | ================================================== */ |
| 477 | export function linkify(text, targets, is_touch) { |
| 478 | |
| 479 | var make_link = function(url, link_text, prefix) { |
| 480 | if (!prefix) { |
| 481 | prefix = ""; |
| 482 | } |
| 483 | var MAX_LINK_TEXT_LENGTH = 30; |
| 484 | if (link_text && link_text.length > MAX_LINK_TEXT_LENGTH) { |
| 485 | link_text = link_text.substring(0, MAX_LINK_TEXT_LENGTH) + "\u2026"; // unicode ellipsis |
| 486 | } |
| 487 | return prefix + "<a class='tl-makelink' href='" + url + "' onclick='void(0)'>" + link_text + "</a>"; |
| 488 | } |
| 489 | // http://, https://, ftp:// |
| 490 | var urlPattern = /\b(?:https?|ftp):\/\/([a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|])/gim; |
| 491 | |
| 492 | // www. sans http:// or https:// |
| 493 | var pseudoUrlPattern = /(^|[^\/>])(www\.[\S]+(\b|$))/gim; |
| 494 | |
| 495 | // Email addresses |
| 496 | var emailAddressPattern = /([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)/gim; |
| 497 | |
| 498 | |
| 499 | return text |
| 500 | .replace(urlPattern, function(match, url_sans_protocol, offset, string) { |
| 501 | // Javascript doesn't support negative lookbehind assertions, so |
| 502 | // we need to handle risk of matching URLs in legit hrefs |
| 503 | if (offset > 0) { |
| 504 | var prechar = string[offset - 1]; |
| 505 | if (prechar == '"' || prechar == "'" || prechar == "=") { |
| 506 | return match; |
| 507 | } |
| 508 | } |
| 509 | return make_link(match, url_sans_protocol); |
| 510 | }) |
| 511 | .replace(pseudoUrlPattern, function(match, beforePseudo, pseudoUrl, offset, string) { |
| 512 | return make_link('http://' + pseudoUrl, pseudoUrl, beforePseudo); |
| 513 | }) |
| 514 | .replace(emailAddressPattern, function(match, email, offset, string) { |
| 515 | return make_link('mailto:' + email, email); |
| 516 | }); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Try to make seamless the process of interpreting a URL to a web page which embeds an image for sharing purposes |
no test coverage detected