Skip to content

Commit

Permalink
fix: crash with invalid url as link (#1791)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored Dec 22, 2024
1 parent cbd7297 commit 7b3c388
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/features/comment/CommentLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SKIP, visit } from "unist-util-visit";

import CommentLink from "#/features/post/link/CommentLink";
import customRemarkGfm from "#/features/shared/markdown/customRemarkGfm";
import { parseUrl } from "#/helpers/url";
import { buildBaseLemmyUrl } from "#/services/lemmy";
import { useAppSelector } from "#/store";

Expand Down Expand Up @@ -54,7 +55,7 @@ export default function CommentLinks({ markdown }: CommentLinksProps) {
links.push({
type: node.type,
// normalize relative links
url: new URL(node.url, connectedInstanceUrl).href,
url: parseUrl(node.url, connectedInstanceUrl)?.href ?? node.url,
text:
"children" in node ? (node.children[0] as Text)?.value : undefined,
});
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export function getPathname(url: string): string | undefined {
}
}

export function parseUrl(url: string): URL | undefined {
export function parseUrl(url: string, baseUrl?: string): URL | undefined {
try {
return new URL(url);
return new URL(url, baseUrl);
} catch {
return;
}
Expand Down

0 comments on commit 7b3c388

Please sign in to comment.