Refactor out isLinkPreviewable

pull/21833/head
Matthew Hodgson 2016-04-21 18:00:39 +01:00
parent ee11838537
commit 4d399a3640
1 changed files with 33 additions and 24 deletions

View File

@ -84,10 +84,27 @@ module.exports = React.createClass({
findLink: function(nodes) {
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (node.tagName === "A" && node.getAttribute("href") &&
(node.getAttribute("href").startsWith("http://") ||
node.getAttribute("href").startsWith("https://")))
if (node.tagName === "A" && node.getAttribute("href"))
{
return this.isLinkPreviewable(node) ? node : undefined;
}
else if (node.tagName === "PRE" || node.tagName === "CODE") {
return;
}
else if (node.children && node.children.length) {
return this.findLink(node.children)
}
}
},
isLinkPreviewable: function(node) {
// don't try to preview relative links
if (!node.getAttribute("href").startsWith("http://") &&
!node.getAttribute("href").startsWith("https://"))
{
return false;
}
// as a random heuristic to avoid highlighting things like "foo.pl"
// we require the linked text to either include a / (either from http://
// or from a full foo.bar/baz style schemeless URL) - or be a markdown-style
@ -109,14 +126,6 @@ module.exports = React.createClass({
return node;
}
}
}
else if (node.tagName === "PRE" || node.tagName === "CODE") {
return;
}
else if (node.children && node.children.length) {
return this.findLink(node.children)
}
}
},
onCancelClick: function(event) {