Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/link.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { type ReactNode, type DetailedHTMLProps, type AnchorHTMLAttributes } from "react";
import { UrlObject, format } from "url";

type HTMLAnchorProps = DetailedHTMLProps<
AnchorHTMLAttributes<HTMLAnchorElement>,
Expand All @@ -18,12 +19,26 @@ let Link: (
props: RegisteredLinkProps & { children: ReactNode }
) => ReturnType<React.FC> = props => <a {...props} />;

/**
* Returns a url string from an Link href. If the link href is an UrlObject, it is converted to a string
*
* Return undefined if the href params is undefined
*/
function getUrlStringFromLinkHref(href: string | UrlObject | undefined): string | undefined {
if (href === undefined || typeof href === "string") {
return href;
}

// format is deprecated but didn't find a real alternative yet
return format(href);
}

export function setLink(params: { Link: typeof Link }): void {
Link = props => {
{
const { to, href, ...rest } = props as { to?: string; href?: string };
const { to, href, ...rest } = props as { to?: string; href?: string | UrlObject };

const target = to ?? href;
const target = to ?? getUrlStringFromLinkHref(href);

mailto: {
if (target === undefined || !target.startsWith("mailto:")) {
Expand Down