function
linkify(string, buildHashtagUrl, includeW3, target, noFollow) {
var
relNoFollow =
""
;
if
(noFollow) {
relNoFollow =
' rel="nofollow"'
;
}
if
(string.toLowerCase().indexOf(
"www."
) === 0 && includeW3) {
string =
string +
'" target="'
+
target +
'"'
+
relNoFollow +
">"
+
string +
"</a>"
;
}
else
{
string =
'<a href="'
+
string +
'" target="'
+
target +
'"'
+
relNoFollow +
">"
+
string +
"</a>"
;
}
if
(buildHashtagUrl) {
string = string.replace(
/\B
#(\w+)/g,
'<a href="'
+
buildHashtagUrl("$1
") +
'"
target=
"' +
target +
'"
' +
relNoFollow +
">#$1</a>"
);
}
return
string;
}
function
linkifyContent() {
var
editableContent = document.getElementById(
"editableContent"
);
var
content = editableContent.textContent;
var
output =
""
;
var
regex = /((http|https|ftp):\/\/|\bwww\.)[^\s<]+/gi;
output = content.replace(regex,
function
(match) {
return
linkify(match,
null
,
true
,
"_self"
,
false
);
});
output = output.replace(/\B
#(\w+)/g, function (match, hashtag) {
return
linkify(
match,
function
(hashtag) {
},
true
,
"_self"
,
false
);
});
document.getElementById(
"content"
).innerHTML = output;
}
document
.getElementById(
"linkifyButton"
)
.addEventListener(
"click"
, linkifyContent);