Skip to content

Commit

Permalink
fix: fill relative url in entry title link, fix #372
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Sep 13, 2024
1 parent ae3d52c commit 7c3aab6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/renderer/src/lib/link-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type LinkParserOptions = {
icon: string
}

const parseSafeUrl = (url: string) => {
export const parseSafeUrl = (url: string) => {
try {
return new URL(url)
} catch {
Expand All @@ -27,6 +27,7 @@ const defineLinkParser = (options: LinkParserOptions) => {
define.icon = options.icon
return define
}

export const isYoutubeUrl = defineLinkParser({
name: "youtube",
validator: (url) => url.hostname.includes("youtube.com"),
Expand Down
21 changes: 20 additions & 1 deletion src/renderer/src/modules/entry-content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ import { EntryHeader } from "./header"
import { EntryContentLoading } from "./loading"
import { EntryContentProvider } from "./provider"

const safeUrl = (url: string, baseUrl: string) => {
try {
return new URL(url, baseUrl).href
} catch {
return url
}
}

export const EntryContent = ({ entryId }: { entryId: ActiveEntryId }) => {
const title = useFeedHeaderTitle()
const { feedId, view } = useRouteParms()
Expand Down Expand Up @@ -150,6 +158,17 @@ export const EntryContentRender: Component<{ entryId: string }> = ({ entryId, cl
: undefined,
[readerFontFamily],
)

const populatedFullHref = useMemo(() => {
const href = entry?.entries.url
if (!href) return "#"

if (href.startsWith("http")) return href
const feedSiteUrl = feed?.siteUrl
if (href.startsWith("/") && feedSiteUrl) return safeUrl(href, feedSiteUrl)
return href
}, [entry?.entries.url, feed?.siteUrl])

if (!entry) return null

const content = entry?.entries.content ?? data?.entries.content
Expand Down Expand Up @@ -184,7 +203,7 @@ export const EntryContentRender: Component<{ entryId: string }> = ({ entryId, cl
className="relative m-auto min-w-0 max-w-[550px] @3xl:max-w-[70ch]"
>
<a
href={entry.entries.url || void 0}
href={populatedFullHref || void 0}
target="_blank"
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"
rel="noreferrer"
Expand Down

0 comments on commit 7c3aab6

Please sign in to comment.