Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

feat(mobile): open links and images in in-app browser (#2696)

Evan Simkowitzbcc8d83ec529
diff --git a/apps/mobile/components/bookmarks/BookmarkHtmlHighlighterDom.tsx b/apps/mobile/components/bookmarks/BookmarkHtmlHighlighterDom.tsx
index 10965307..2919ddcc 100644
--- a/apps/mobile/components/bookmarks/BookmarkHtmlHighlighterDom.tsx
+++ b/apps/mobile/components/bookmarks/BookmarkHtmlHighlighterDom.tsx
@@ -2,6 +2,8 @@
import "@/globals.css";
+import { useEffect } from "react";
+
import type { Highlight } from "@karakeep/shared-react/components/BookmarkHtmlHighlighter";
import BookmarkHTMLHighlighter from "@karakeep/shared-react/components/BookmarkHtmlHighlighter";
import ScrollProgressTracker from "@karakeep/shared-react/components/ScrollProgressTracker";
@@ -14,6 +16,8 @@ export default function BookmarkHtmlHighlighterDom({
onHighlight,
onUpdateHighlight,
onDeleteHighlight,
+ onLinkPress,
+ onImagePress,
readingProgressOffset,
readingProgressAnchor,
restoreReadingPosition,
@@ -27,6 +31,8 @@ export default function BookmarkHtmlHighlighterDom({
onHighlight?: (highlight: Highlight) => void;
onUpdateHighlight?: (highlight: Highlight) => void;
onDeleteHighlight?: (highlight: Highlight) => void;
+ onLinkPress?: (url: string) => void;
+ onImagePress?: (src: string) => void;
readingProgressOffset?: number | null;
readingProgressAnchor?: string | null;
restoreReadingPosition?: boolean;
@@ -42,6 +48,74 @@ export default function BookmarkHtmlHighlighterDom({
}) => void;
dom?: import("expo/dom").DOMProps;
}) {
+ // Strip href from links so the browser treats them as regular selectable text
+ // instead of activating native link gestures (iOS preview, Android drag).
+ // The URL is preserved in data-href for our click handler.
+ useEffect(() => {
+ const stripHrefs = () => {
+ document.querySelectorAll("a[href]").forEach((a) => {
+ const anchor = a as HTMLAnchorElement;
+ if (!anchor.dataset.href) {
+ anchor.dataset.href = anchor.getAttribute("href")!;
+ anchor.removeAttribute("href");
+ }
+ });
+ };
+
+ stripHrefs();
+
+ // Re-strip if the DOM changes (e.g. highlight effects re-render content)
+ const observer = new MutationObserver(stripHrefs);
+ observer.observe(document.body, { childList: true, subtree: true });
+
+ return () => observer.disconnect();
+ }, []);
+
+ // Intercept link and image clicks to open them externally
+ useEffect(() => {
+ const handleClick = (e: MouseEvent) => {
+ const target = e.target as HTMLElement;
+
+ // Don't intercept if the user is selecting text (for highlighting)
+ const selection = window.getSelection();
+ if (selection && !selection.isCollapsed) {
+ return;
+ }
+
+ // Check for link clicks (href is stored in data-href)
+ const anchor = target.closest("a");
+ const href = anchor?.dataset.href;
+ if (href) {
+ // Allow in-page anchor links
+ if (href.startsWith("#")) {
+ const targetEl = document.querySelector(href);
+ if (targetEl) {
+ targetEl.scrollIntoView();
+ }
+ return;
+ }
+ // Ignore javascript: URLs
+ if (href.startsWith("javascript:")) {
+ e.preventDefault();
+ return;
+ }
+ e.preventDefault();
+ onLinkPress?.(href);
+ return;
+ }
+
+ // Check for image clicks
+ const img = target.closest("img");
+ if (img?.src) {
+ e.preventDefault();
+ onImagePress?.(img.src);
+ return;
+ }
+ };
+
+ document.addEventListener("click", handleClick);
+ return () => document.removeEventListener("click", handleClick);
+ }, [onLinkPress, onImagePress]);
return (
<div style={{ maxWidth: "100vw", overflowX: "hidden" }}>
<ScrollProgressTracker
diff --git a/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx b/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx
index 80a918a9..c1e1f154 100644
--- a/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx
+++ b/apps/mobile/components/bookmarks/BookmarkLinkPreview.tsx
@@ -1,8 +1,12 @@
-import { useState } from "react";
-import { Pressable, TouchableOpacity, View } from "react-native";
+import { useCallback, useState } from "react";
+import { Linking, Pressable, TouchableOpacity, View } from "react-native";
import ImageView from "react-native-image-viewing";
import WebView from "react-native-webview";
-import { WebViewSourceUri } from "react-native-webview/lib/WebViewTypes";
+import {
+ ShouldStartLoadRequest,
+ WebViewSourceUri,
+} from "react-native-webview/lib/WebViewTypes";
+import * as WebBrowser from "expo-web-browser";
import { Text } from "@/components/ui/Text";
import { useAssetUrl } from "@/lib/hooks";
import { useReaderSettings, WEBVIEW_FONT_FAMILIES } from "@/lib/readerSettings";
@@ -25,6 +29,19 @@ import BookmarkAssetImage from "./BookmarkAssetImage";
import BookmarkHtmlHighlighterDom from "./BookmarkHtmlHighlighterDom";
import { PDFViewer } from "./PDFViewer";
+function openUrlExternally(url: string) {
+ if (url.startsWith("http://") || url.startsWith("https://")) {
+ void WebBrowser.openBrowserAsync(url);
+ } else if (
+ url.startsWith("mailto:") ||
+ url.startsWith("tel:") ||
+ url.startsWith("sms:")
+ ) {
+ void Linking.openURL(url);
+ }
+ // Ignore javascript: and other schemes
+}
+
export function BookmarkLinkBrowserPreview({
bookmark,
}: {
@@ -34,11 +51,27 @@ export function BookmarkLinkBrowserPreview({
throw new Error("Wrong content type rendered");
}
+ const bookmarkUrl = bookmark.content.url;
+
+ const onShouldStartLoadWithRequest = useCallback(
+ (request: ShouldStartLoadRequest) => {
+ const bookmarkOrigin = new URL(bookmarkUrl).origin;
+ if (request.url.startsWith(bookmarkOrigin)) {
+ return true;
+ }
+ openUrlExternally(request.url);
+ return false;
+ },
+ [bookmarkUrl],
+ );
+
return (
<WebView
startInLoadingState={true}
mediaPlaybackRequiresUserAction={true}
- source={{ uri: bookmark.content.url }}
+ source={{ uri: bookmarkUrl }}
+ setSupportMultipleWindows={false}
+ onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
/>
);
}
@@ -112,6 +145,16 @@ export function BookmarkLinkReaderPreview({
bookmarkId: bookmark.id,
});
+ const [viewingImage, setViewingImage] = useState<string | null>(null);
+
+ const handleLinkPress = useCallback((url: string) => {
+ openUrlExternally(url);
+ }, []);
+
+ const handleImagePress = useCallback((src: string) => {
+ setViewingImage(src);
+ }, []);
+
if (isLoading) {
return <FullPageSpinner />;
}
@@ -135,6 +178,13 @@ export function BookmarkLinkReaderPreview({
return (
<View className="flex-1 bg-background">
+ <ImageView
+ visible={!!viewingImage}
+ imageIndex={0}
+ onRequestClose={() => setViewingImage(null)}
+ doubleTapToZoomEnabled={true}
+ images={viewingImage ? [{ uri: viewingImage }] : []}
+ />
{showBanner && (
<View className="flex-row items-center gap-2 border-b border-border bg-background px-4 py-2">
<BookOpen size={16} className="text-muted-foreground" />
@@ -165,6 +215,8 @@ export function BookmarkLinkReaderPreview({
restoreReadingPosition={restorePosition}
onSavePosition={onSavePosition}
onScrollPositionChange={onScrollPositionChange}
+ onLinkPress={handleLinkPress}
+ onImagePress={handleImagePress}
onHighlight={(h) =>
createHighlight({
startOffset: h.startOffset,
@@ -204,6 +256,22 @@ export function BookmarkLinkArchivePreview({
const assetSource = useAssetUrl(asset?.id ?? "");
+ const originUri = assetSource.uri;
+ const onShouldStartLoadWithRequest = useCallback(
+ (request: ShouldStartLoadRequest) => {
+ // Allow loading the archive asset itself
+ if (
+ originUri &&
+ (request.url === originUri || request.url.startsWith(originUri))
+ ) {
+ return true;
+ }
+ openUrlExternally(request.url);
+ return false;
+ },
+ [originUri],
+ );
+
if (!asset) {
return (
<View className="flex-1 bg-background">
@@ -216,12 +284,15 @@ export function BookmarkLinkArchivePreview({
uri: assetSource.uri!,
headers: assetSource.headers,
};
+
return (
<WebView
startInLoadingState={true}
mediaPlaybackRequiresUserAction={true}
source={webViewUri}
decelerationRate={0.998}
+ setSupportMultipleWindows={false}
+ onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
/>
);
}