Skip to content

Instantly share code, notes, and snippets.

@iwatakeshi
Last active October 4, 2019 18:35
Show Gist options
  • Save iwatakeshi/d4a665707e76926ed69fea9123c6d9b2 to your computer and use it in GitHub Desktop.
Save iwatakeshi/d4a665707e76926ed69fea9123c6d9b2 to your computer and use it in GitHub Desktop.
Sanitize Prismic URLs
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