Fix issue with popover on full sn links (#1216)
* Fix issue with popover on full sn links * allow custom text for internal links --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
This commit is contained in:
parent
86b857b8d4
commit
d454bbdb72
|
@ -186,6 +186,7 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
|
|||
} catch {
|
||||
// ignore invalid URLs
|
||||
}
|
||||
|
||||
const internalURL = process.env.NEXT_PUBLIC_URL
|
||||
if (!!text && !/^https?:\/\//.test(text)) {
|
||||
if (props['data-footnote-ref'] || typeof props['data-footnote-backref'] !== 'undefined') {
|
||||
|
@ -210,6 +211,19 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
|
|||
</UserPopover>
|
||||
)
|
||||
} else if (href.startsWith('/') || url?.origin === internalURL) {
|
||||
try {
|
||||
const linkText = parseInternalLinks(href)
|
||||
if (linkText) {
|
||||
return (
|
||||
<ItemPopover id={linkText.replace('#', '').split('/')[0]}>
|
||||
<Link href={href}>{text}</Link>
|
||||
</ItemPopover>
|
||||
)
|
||||
}
|
||||
} catch {
|
||||
// ignore errors like invalid URLs
|
||||
}
|
||||
|
||||
return (
|
||||
<Link
|
||||
id={props.id}
|
||||
|
|
14
lib/url.js
14
lib/url.js
|
@ -26,14 +26,22 @@ export function removeTracking (value) {
|
|||
/**
|
||||
* parse links like https://stacker.news/items/123456 as #123456
|
||||
*/
|
||||
|
||||
export function isItemPath (pathname) {
|
||||
if (!pathname) return false
|
||||
|
||||
const [page, id] = pathname.split('/').filter(part => !!part)
|
||||
return page === 'items' && /^[0-9]+$/.test(id)
|
||||
}
|
||||
|
||||
export function parseInternalLinks (href) {
|
||||
const url = new URL(href)
|
||||
const internalURL = process.env.NEXT_PUBLIC_URL
|
||||
const { pathname, searchParams } = url
|
||||
|
||||
// ignore empty parts which exist due to pathname starting with '/'
|
||||
const emptyPart = part => !!part
|
||||
const parts = pathname.split('/').filter(emptyPart)
|
||||
if (parts[0] === 'items' && /^[0-9]+$/.test(parts[1]) && url.origin === internalURL) {
|
||||
if (isItemPath(pathname) && url.origin === internalURL) {
|
||||
const parts = pathname.split('/').filter(part => !!part)
|
||||
const itemId = parts[1]
|
||||
// check for valid item page due to referral links like /items/123456/r/ekzyis
|
||||
const itemPages = ['edit', 'ots', 'related']
|
||||
|
|
Loading…
Reference in New Issue