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'
|
paddingRight: '15px'
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
const { provider, id, meta } = parseEmbedUrl(href)
|
const { provider, id, meta } = parseEmbedUrl(href)
|
||||||
// Youtube video embed
|
// Youtube video embed
|
||||||
if (provider === 'youtube') {
|
if (provider === 'youtube') {
|
||||||
@ -290,9 +289,6 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
|
|||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
// ignore invalid URLs
|
|
||||||
}
|
|
||||||
|
|
||||||
// assume the link is an image which will fallback to link if it's not
|
// 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>
|
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) {
|
export function ensureProtocol (value) {
|
||||||
if (!value) return value
|
if (!value) return value
|
||||||
value = value.trim()
|
value = value.trim()
|
||||||
if (!/^([a-z0-9]+:\/\/|mailto:)/.test(value)) {
|
let url
|
||||||
value = 'http://' + value
|
|
||||||
}
|
try {
|
||||||
|
url = new URL(value)
|
||||||
|
} catch {
|
||||||
|
try {
|
||||||
|
url = new URL('http://' + value)
|
||||||
|
} catch {
|
||||||
return value
|
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) {
|
export function isExternal (url) {
|
||||||
@ -63,6 +76,7 @@ export function parseInternalLinks (href) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function parseEmbedUrl (href) {
|
export function parseEmbedUrl (href) {
|
||||||
|
try {
|
||||||
const { hostname, pathname, searchParams } = new URL(href)
|
const { hostname, pathname, searchParams } = new URL(href)
|
||||||
|
|
||||||
if (hostname.endsWith('youtube.com') && pathname.includes('/watch')) {
|
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
|
// Important to return empty object as default
|
||||||
return {}
|
return {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user