Last active
October 4, 2019 18:35
-
-
Save iwatakeshi/d4a665707e76926ed69fea9123c6d9b2 to your computer and use it in GitHub Desktop.
Sanitize Prismic URLs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hashLink = /(#)([^\s]+)/gi | |
function isAnchor (url) { | |
if (!process.client) { | |
return false | |
} | |
if (url) { | |
if ( | |
url.includes(location.href) && | |
hashLink.test(url) | |
) { | |
return true | |
} | |
} | |
return false | |
} | |
function toAnchor (url) { | |
if (url) { | |
const matches = url.match(hashLink) | |
if (matches) { | |
return matches[0] || '#' | |
} | |
} | |
return '' | |
} | |
export default function sanitizeUrl (url) { | |
if (!url) { | |
return '' | |
} | |
if (isAnchor(url)) { | |
return toAnchor(url) | |
} | |
return url | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment