Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support mailto links #911

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions packages/expo-router/src/global-state/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as Linking from "expo-linking";
import { ResultState } from "../fork/getStateFromPath";
import { Href, resolveHref } from "../link/href";
import { resolve } from "../link/path";
import { hasUrlProtocolPrefix } from "../utils/url";
import { shouldLinkExternally } from "../utils/url";
import type { RouterStore } from "./router-store";

function assertIsReady(store: RouterStore) {
Expand Down Expand Up @@ -52,7 +52,7 @@ export function setParams(
}

export function linkTo(this: RouterStore, href: string, event?: string) {
if (hasUrlProtocolPrefix(href)) {
if (shouldLinkExternally(href)) {
Linking.openURL(href);
return;
}
Expand Down
16 changes: 16 additions & 0 deletions packages/expo-router/src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,19 @@
export function hasUrlProtocolPrefix(href: string): boolean {
return /^[\w\d_+.-]+:\/\//.test(href);
}

export function isWellKnownUri(href: string): boolean {
// This is a hack and we should change this to work like the web in the future where we have full confidence in the
// ability to match URLs and send anything unmatched to the OS. The main difference between this and `hasUrlProtocolPrefix` is
// that we don't require `//`, e.g. `mailto:` is valid and common, and `mailto://bacon` is invalid.
return /^(https?|mailto|tel|sms|geo|maps|market|itmss?|itms-apps|content|file):/.test(
href
);
}

export function shouldLinkExternally(href: string): boolean {
// Cheap check first to avoid regex if the href is not a path fragment.
return (
!/^[./]/.test(href) && (hasUrlProtocolPrefix(href) || isWellKnownUri(href))
);
}