Yes. The easiest way is to use regular expressions to replace things that look like a link for their related equivalents. Something like:
node.innerHTML = node.innerHTML.replace(/(http:\/\/[^\s]+)/g, "<a href='$1'>$1</a>")
(my RegEx is a little rusty, so you may need to play with the syntax). This is a simple case. You should be careful with the script application here (for example, if I have http://"><script>doevil()</script> ). One way to get around this is to use the link building function:
node.innerHTML = node.innerHTML.replace(/ ... /g, buildLink($1));
Where buildLink() can verify that the URL does not contain anything malicious.
However, the RegEx-innerHTML method will not work well on large text topics, as it breaks and restores all node HTML content. You can also achieve this with DOM methods:
- Find link to node text
- In the content, find the start and end indexes of the URL
- Use the
splitText() method to split the node into 3: before, link, after - Create a
<a> node using href that matches the link - Use
insertBefore() to insert this <a> node before the link - Use
appendChild() to move the link in the <a> node
levik
source share