Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 16, 2018
1 parent 8700bb6 commit f5e23de
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const TAGS = {
]
}

const REGEX_HTTP_PROTOCOL = /https?/i

const reduceSelector = (collection, fn, acc = []) => {
collection.each(function () {
acc = fn(acc, this)
Expand All @@ -41,9 +43,17 @@ const reduceSelector = (collection, fn, acc = []) => {

const includes = (collection, fn) => findIndex(collection, fn) !== -1

const isHttpUrl = url => {
try {
return REGEX_HTTP_PROTOCOL.test(new URL(url).protocol)
} catch (err) {
return false
}
}

const getLink = ({ url, el, attribute }) => {
const attr = get(el, `attribs.${attribute}`, '')
if (isEmpty(attr) || attr.startsWith('mailto:') || attr.startsWith('callto:')) { return null }
if (isEmpty(attr) || !isHttpUrl(attr)) return null

try {
const normalizedUrl = normalizeUrl(url, attr)
Expand Down

0 comments on commit f5e23de

Please sign in to comment.