Format internal links as #<id> (#765)

* Format internal links

* Fix URL text ignored
This commit is contained in:
ekzyis 2024-01-25 00:29:52 +01:00 committed by GitHub
parent bd23c91fda
commit e4aff5d11b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -185,6 +185,21 @@ export default memo(function Text ({ nofollow, imgproxyUrls, children, tab, item
) )
} }
try {
const url = new URL(href)
const { pathname, searchParams } = url
const itemId = pathname.match(/items\/(\w+)/)[1]
// don't format invalid item links
const valid = !/[a-zA-Z_]/.test(itemId)
if (valid && itemId) {
const commentId = searchParams.get('commentId')
const linkText = `#${commentId || itemId}`
return <a target='_blank' href={href} rel='noreferrer'>{linkText}</a>
}
} catch {
// ignore invalid URLs
}
// if the link is to a youtube video, render the video // if the link is to a youtube video, render the video
const youtube = href.match(/(https?:\/\/)?((www\.)?(youtube(-nocookie)?|youtube.googleapis)\.com.*(v\/|v=|vi=|vi\/|e\/|embed\/|user\/.*\/u\/\d+\/)|youtu\.be\/)(?<id>[_0-9a-z-]+)((?:\?|&)(?:t|start)=(?<start>\d+))?/i) const youtube = href.match(/(https?:\/\/)?((www\.)?(youtube(-nocookie)?|youtube.googleapis)\.com.*(v\/|v=|vi=|vi\/|e\/|embed\/|user\/.*\/u\/\d+\/)|youtu\.be\/)(?<id>[_0-9a-z-]+)((?:\?|&)(?:t|start)=(?<start>\d+))?/i)
if (youtube?.groups?.id) { if (youtube?.groups?.id) {