Skip to content

Commit 7c3aab6

Browse files
committed
fix: fill relative url in entry title link, fix #372
Signed-off-by: Innei <i@innei.in>
1 parent ae3d52c commit 7c3aab6

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/renderer/src/lib/link-parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type LinkParserOptions = {
66
icon: string
77
}
88

9-
const parseSafeUrl = (url: string) => {
9+
export const parseSafeUrl = (url: string) => {
1010
try {
1111
return new URL(url)
1212
} catch {
@@ -27,6 +27,7 @@ const defineLinkParser = (options: LinkParserOptions) => {
2727
define.icon = options.icon
2828
return define
2929
}
30+
3031
export const isYoutubeUrl = defineLinkParser({
3132
name: "youtube",
3233
validator: (url) => url.hostname.includes("youtube.com"),

src/renderer/src/modules/entry-content/index.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ import { EntryHeader } from "./header"
4848
import { EntryContentLoading } from "./loading"
4949
import { EntryContentProvider } from "./provider"
5050

51+
const safeUrl = (url: string, baseUrl: string) => {
52+
try {
53+
return new URL(url, baseUrl).href
54+
} catch {
55+
return url
56+
}
57+
}
58+
5159
export const EntryContent = ({ entryId }: { entryId: ActiveEntryId }) => {
5260
const title = useFeedHeaderTitle()
5361
const { feedId, view } = useRouteParms()
@@ -150,6 +158,17 @@ export const EntryContentRender: Component<{ entryId: string }> = ({ entryId, cl
150158
: undefined,
151159
[readerFontFamily],
152160
)
161+
162+
const populatedFullHref = useMemo(() => {
163+
const href = entry?.entries.url
164+
if (!href) return "#"
165+
166+
if (href.startsWith("http")) return href
167+
const feedSiteUrl = feed?.siteUrl
168+
if (href.startsWith("/") && feedSiteUrl) return safeUrl(href, feedSiteUrl)
169+
return href
170+
}, [entry?.entries.url, feed?.siteUrl])
171+
153172
if (!entry) return null
154173

155174
const content = entry?.entries.content ?? data?.entries.content
@@ -184,7 +203,7 @@ export const EntryContentRender: Component<{ entryId: string }> = ({ entryId, cl
184203
className="relative m-auto min-w-0 max-w-[550px] @3xl:max-w-[70ch]"
185204
>
186205
<a
187-
href={entry.entries.url || void 0}
206+
href={populatedFullHref || void 0}
188207
target="_blank"
189208
className="-mx-6 block cursor-default rounded-lg p-6 transition-colors hover:bg-theme-item-hover focus-visible:bg-theme-item-hover focus-visible:!outline-none @sm:-mx-3 @sm:p-3"
190209
rel="noreferrer"

0 commit comments

Comments
 (0)