predikt/oracle/web/components/editor/link-extension.tsx
caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

86 lines
2.3 KiB
TypeScript

import { mergeAttributes } from '@tiptap/core'
import { Link as TiptapLink } from '@tiptap/extension-link'
import NextLink from 'next/link'
import { ReactNode } from 'react'
import { linkClass, LinkFavicon } from 'web/components/widgets/site-link'
const LinkComponent = (props: { href: string; children: ReactNode }) => {
const { href: rawHref, children } = props
const href = safeHref(rawHref)
if (isInternal(href)) {
return (
<NextLink href={href} className={linkClass} prefetch={false}>
{children}
</NextLink>
)
}
return (
<a href={href} target="_blank" rel="noopener ugc" className={linkClass}>
<LinkFavicon href={href} />
{children}
</a>
)
}
export const DisplayLink = TiptapLink.extend({
renderHTML({ HTMLAttributes }) {
HTMLAttributes.href = safeHref(HTMLAttributes.href)
// This is used for SSR and copy/paste
HTMLAttributes.target = isInternal(HTMLAttributes.href) ? '_self' : '_blank'
delete HTMLAttributes.class // only use our classes (don't duplicate on paste)
return [
'a',
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
0,
]
},
renderReact(attrs: any, children: ReactNode) {
return <LinkComponent href={attrs.href}>{children}</LinkComponent>
},
}).configure({
openOnClick: false, // stop link opening twice (browser still opens)
HTMLAttributes: {
rel: 'noopener ugc',
class: linkClass,
},
})
// Allow http(s) absolute URLs, root-relative paths, and in-page anchors only.
// Reject protocol-relative (`//host`, `/\host`) and everything else
// (javascript:, data:, vbscript:, file:, etc.) → '#'.
const isLocalPath = (href: string) =>
(href.startsWith('/') && !href.startsWith('//') && !href.startsWith('/\\')) ||
href.startsWith('#')
const safeHref = (href: string | undefined | null): string => {
if (!href) return '#'
if (isLocalPath(href)) return href
if (/^https?:\/\//i.test(href)) return href
return '#'
}
const INTERNAL_HOSTS = new Set([
'oracle.markets',
'manifold.love',
'localhost',
])
const isInternal = (href: string) => {
if (isLocalPath(href)) return true
try {
const { hostname } = new URL(href)
return (
INTERNAL_HOSTS.has(hostname) ||
hostname.endsWith('.oracle.markets') ||
hostname.endsWith('.manifold.love')
)
} catch {
return false
}
}