bug: url parser

fixes #564
This commit is contained in:
Kieran 2023-05-17 20:37:25 +01:00
parent 167f1c5e65
commit cefc21709a
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 11 additions and 2 deletions

View File

@ -1,6 +1,7 @@
import useImgProxy from "Hooks/useImgProxy";
import { useEffect, useState } from "react";
import { FormattedMessage } from "react-intl";
import { getUrlHostname } from "Util";
interface ProxyImgProps extends React.DetailedHTMLProps<React.ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement> {
size?: number;
@ -34,7 +35,7 @@ export const ProxyImg = (props: ProxyImgProps) => {
<FormattedMessage
defaultMessage="Failed to proxy image from {host}, click here to load directly"
values={{
host: new URL(src ?? "").hostname,
host: getUrlHostname(src),
}}
/>
</div>

View File

@ -166,7 +166,7 @@ export default function Text({ content, tags, creator, disableMedia, depth }: Te
if (t) {
return <ProxyImg src={t[2]} size={15} className="custom-emoji" />;
} else {
return i;
return `:${i}:`;
}
});
}

View File

@ -497,6 +497,14 @@ export function getRelayName(url: string) {
return parsedUrl.host + parsedUrl.search;
}
export function getUrlHostname(url?: string) {
try {
return new URL(url ?? "").hostname;
} catch {
return url?.match(/(\S+\.\S+)/i)?.[1] ?? url;
}
}
export interface NostrLink {
type: NostrPrefix;
id: string;