File size: 2,196 Bytes
3f268e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';

;(function (window, linkify) {
	var linkifyString = function (linkify) {
		'use strict';

		/**
  	Convert strings of text into linkable HTML text
  */

		var tokenize = linkify.tokenize,
		    options = linkify.options;
		var Options = options.Options;


		function escapeText(text) {
			return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
		}

		function escapeAttr(href) {
			return href.replace(/"/g, '&quot;');
		}

		function attributesToString(attributes) {
			if (!attributes) {
				return '';
			}
			var result = [];

			for (var attr in attributes) {
				var val = attributes[attr] + '';
				result.push(attr + '="' + escapeAttr(val) + '"');
			}
			return result.join(' ');
		}

		function linkifyStr(str) {
			var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

			opts = new Options(opts);

			var tokens = tokenize(str);
			var result = [];

			for (var i = 0; i < tokens.length; i++) {
				var token = tokens[i];

				if (token.type === 'nl' && opts.nl2br) {
					result.push('<br>\n');
					continue;
				} else if (!token.isLink || !opts.check(token)) {
					result.push(escapeText(token.toString()));
					continue;
				}

				var _opts$resolve = opts.resolve(token),
				    formatted = _opts$resolve.formatted,
				    formattedHref = _opts$resolve.formattedHref,
				    tagName = _opts$resolve.tagName,
				    className = _opts$resolve.className,
				    target = _opts$resolve.target,
				    attributes = _opts$resolve.attributes;

				var link = '<' + tagName + ' href="' + escapeAttr(formattedHref) + '"';

				if (className) {
					link += ' class="' + escapeAttr(className) + '"';
				}

				if (target) {
					link += ' target="' + escapeAttr(target) + '"';
				}

				if (attributes) {
					link += ' ' + attributesToString(attributes);
				}

				link += '>' + escapeText(formatted) + '</' + tagName + '>';
				result.push(link);
			}

			return result.join('');
		}

		if (!String.prototype.linkify) {
			String.prototype.linkify = function (opts) {
				return linkifyStr(this, opts);
			};
		}

		return linkifyStr;
	}(linkify);

	window.linkifyStr = linkifyString;
})(window, linkify);