Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(docs) Add external link indicator for screenreader and visual (#46825)

Miranda Limonczenkocb6ad35ac1d6
diff --git a/apps/docs/features/docs/MdxAnchor.tsx b/apps/docs/features/docs/MdxAnchor.tsx
new file mode 100644
index 00000000000..30b2b54db42
--- /dev/null
+++ b/apps/docs/features/docs/MdxAnchor.tsx
@@ -0,0 +1,46 @@
+import { ExternalLink as ExternalLinkIcon } from 'lucide-react'
+import { type ComponentPropsWithoutRef } from 'react'
+import { cn } from 'ui'
+
+const isExternalHref = (href?: string) => {
+ if (!href || !/^https?:\/\//i.test(href)) return false
+ try {
+ return !/(^|\.)supabase\.com$/i.test(new URL(href).hostname)
+ } catch {
+ return false
+ }
+}
+
+// Walk the tree and concatenate text so the aria-label matches the visible link
+const flattenChildrenToText = (node: unknown): string => {
+ if (node == null || typeof node === 'boolean') return ''
+ if (typeof node === 'string' || typeof node === 'number') return String(node)
+ if (Array.isArray(node)) return node.map(flattenChildrenToText).join('')
+ if (typeof node === 'object' && node !== null && 'props' in node) {
+ const props = (node as { props?: { children?: unknown } }).props
+ return flattenChildrenToText(props?.children)
+ }
+ return ''
+}
+
+export function MdxAnchor({ href, children, ...rest }: ComponentPropsWithoutRef<'a'>) {
+ if (!isExternalHref(href)) {
+ return (
+ <a href={href} {...rest}>
+ {children}
+ </a>
+ )
+ }
+ const label = flattenChildrenToText(children).trim()
+ return (
+ <a href={href} aria-label={label ? `External Source: ${label}` : undefined} {...rest}>
+ {children}
+ <ExternalLinkIcon
+ size={14}
+ strokeWidth={1.5}
+ aria-hidden="true"
+ className="inline-block align-[-1px] ml-0.5 text-lighter"
+ />
+ </a>
+ )
+}
diff --git a/apps/docs/features/docs/MdxBase.shared.tsx b/apps/docs/features/docs/MdxBase.shared.tsx
index 134ffc1ed31..591a8bf20cb 100644
--- a/apps/docs/features/docs/MdxBase.shared.tsx
+++ b/apps/docs/features/docs/MdxBase.shared.tsx
@@ -19,6 +19,7 @@ import { SharedData } from '~/components/SharedData'
import StepHikeCompact from '~/components/StepHikeCompact'
import { CodeSampleDummy, CodeSampleWrapper } from '~/features/directives/CodeSample.client'
import { NamedCodeBlock } from '~/features/directives/CodeTabs.components'
+import { MdxAnchor } from '~/features/docs/MdxAnchor'
import { Accordion, AccordionItem } from '~/features/ui/Accordion'
import { CodeBlock } from '~/features/ui/CodeBlock/CodeBlock'
import InfoTooltip from '~/features/ui/InfoTooltip'
@@ -100,6 +101,7 @@ const components = {
Tabs,
TabPanel,
InfoTooltip,
+ a: MdxAnchor,
h2: (props: any) => (
<Heading tag="h2" {...props}>
{props.children}