fix inconsistency in url handling and don't let parseEmbedURL throw
This commit is contained in:
parent
ea97fbf4a4
commit
2775b49ce7
|
@ -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>
|
||||
|
|
23
lib/url.js
23
lib/url.js
|
@ -1,10 +1,23 @@
|
|||
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) {
|
||||
|
@ -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 {}
|
||||
|
|
Loading…
Reference in New Issue