Skip to content

Commit ef57ff9

Browse files
amiorinclaude
andcommitted
Open outside reader links in a new tab
Markdown links and URL field values that point at http(s) addresses now open in a new tab through a shared ContentLink component, keeping the reader's place. In-app record links and mailto links stay in this tab; rel="noopener noreferrer" is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014MEXKqCEx1e1cDjXbLX4NY
1 parent 5ccbae6 commit ef57ff9

4 files changed

Lines changed: 51 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ Use Node.js 24 and pnpm 10.33.2. The browser smoke uses synthetic records and an
244244
temporary database. It tests actual production assets, authentication, direct links,
245245
reload, search pagination, mobile navigation, and SQL/schema compatibility. Unit and
246246
mocked browser tests additionally cover query escaping, malformed routes, relationship
247-
labels, and inert Markdown. Generated assets are not committed.
247+
labels, inert Markdown, and outside links opening in a new tab. Generated assets are
248+
not committed.
248249
For browser Google OAuth, register the application's own
249250
`https://<application-host>/api/oauth2-redirect` URI in its existing OAuth client.
250251

‎ui/src/App.tsx‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useState } from "react";
22
import Markdown from "react-markdown";
33
import remarkGfm from "remark-gfm";
4+
import { ContentLink } from "./links";
45
import { app, dateOnly, subtitle, type Entity } from "./config";
56
import {
67
pb,
@@ -180,9 +181,7 @@ function Value({
180181
components={{
181182
img: () => null,
182183
a: ({ href, children }) => (
183-
<a href={href} rel="noopener noreferrer">
184-
{children}
185-
</a>
184+
<ContentLink href={href}>{children}</ContentLink>
186185
),
187186
}}
188187
>
@@ -191,11 +190,7 @@ function Value({
191190
</div>
192191
);
193192
if (/^https?:\/\//i.test(text))
194-
return (
195-
<a href={text} rel="noopener noreferrer">
196-
{text}
197-
</a>
198-
);
193+
return <ContentLink href={text}>{text}</ContentLink>;
199194
return <span className="text-value">{text}</span>;
200195
}
201196
function Detail({ e, id }: { e: Entity; id: string }) {

‎ui/src/links.test.tsx‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { render, screen } from "@testing-library/react";
2+
import { describe, expect, it } from "vitest";
3+
import { ContentLink } from "./links";
4+
5+
describe("ContentLink", () => {
6+
it("opens http(s) links in a new tab without opener or referrer", () => {
7+
render(<ContentLink href="https://example.com/x">outside</ContentLink>);
8+
const link = screen.getByRole("link", { name: "outside" });
9+
expect(link).toHaveAttribute("target", "_blank");
10+
expect(link).toHaveAttribute("rel", "noopener noreferrer");
11+
});
12+
it("keeps in-app and mailto links in this tab", () => {
13+
render(
14+
<>
15+
<ContentLink href="#/records/record000000001">record</ContentLink>
16+
<ContentLink href="mailto:someone@example.com">mail</ContentLink>
17+
</>,
18+
);
19+
expect(screen.getByRole("link", { name: "record" })).not.toHaveAttribute(
20+
"target",
21+
);
22+
expect(screen.getByRole("link", { name: "mail" })).not.toHaveAttribute(
23+
"target",
24+
);
25+
});
26+
});

‎ui/src/links.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { ReactNode } from "react";
2+
// Outside links open in a new tab so the reader keeps its place; in-app and
3+
// mailto links stay in this tab.
4+
export function ContentLink({
5+
href,
6+
children,
7+
}: {
8+
href?: string;
9+
children: ReactNode;
10+
}) {
11+
return (
12+
<a
13+
href={href}
14+
rel="noopener noreferrer"
15+
{...(/^https?:/i.test(href || "") ? { target: "_blank" } : {})}
16+
>
17+
{children}
18+
</a>
19+
);
20+
}

0 commit comments

Comments
 (0)