fix inconsistency in url handling and don't let parseEmbedURL throw

This commit is contained in:
keyan 2024-06-04 12:10:37 -05:00
parent ea97fbf4a4
commit 2775b49ce7
2 changed files with 70 additions and 57 deletions

View File

@ -259,7 +259,6 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
paddingRight: '15px'
}
try {
const { provider, id, meta } = parseEmbedUrl(href)
// Youtube video embed
if (provider === 'youtube') {
@ -290,9 +289,6 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
</div>
)
}
} catch {
// ignore invalid URLs
}
// assume the link is an image which will fallback to link if it's not
return <Img src={href} rel={rel ?? UNKNOWN_LINK_REL} {...props}>{children}</Img>

View File

@ -1,11 +1,24 @@
export function ensureProtocol (value) {
if (!value) return value
value = value.trim()
if (!/^([a-z0-9]+:\/\/|mailto:)/.test(value)) {
value = 'http://' + value
}
let url
try {
url = new URL(value)
} catch {
try {
url = new URL('http://' + value)
} catch {
return value
}
}
// remove trailing slash if new URL() added it
if (url.href.endsWith('/') && !value.endsWith('/')) {
return url.href.slice(0, -1)
}
return url.href
}
export function isExternal (url) {
return !url.startsWith(process.env.NEXT_PUBLIC_URL + '/') && !url.startsWith('/')
@ -63,6 +76,7 @@ export function parseInternalLinks (href) {
}
export function parseEmbedUrl (href) {
try {
const { hostname, pathname, searchParams } = new URL(href)
if (hostname.endsWith('youtube.com') && pathname.includes('/watch')) {
@ -96,6 +110,9 @@ export function parseEmbedUrl (href) {
}
}
}
} catch {
// ignore
}
// Important to return empty object as default
return {}