only render hyperlink if link is valid
This commit is contained in:
parent
9b2fb3b6bf
commit
74bfa03227
@ -7,7 +7,7 @@ import * as unist from "unist";
|
||||
import { HexKey, NostrPrefix } from "@snort/nostr";
|
||||
|
||||
import { MentionRegex, InvoiceRegex, HashtagRegex } from "Const";
|
||||
import { eventLink, hexToBech32, splitByUrl, unwrap } from "Util";
|
||||
import { eventLink, hexToBech32, splitByUrl, unwrap, validateNostrLink } from "Util";
|
||||
import Invoice from "Element/Invoice";
|
||||
import Hashtag from "Element/Hashtag";
|
||||
import Mention from "Element/Mention";
|
||||
@ -36,7 +36,21 @@ export default function Text({ content, tags, creator, disableMedia, depth }: Te
|
||||
.map(f => {
|
||||
if (typeof f === "string") {
|
||||
return splitByUrl(f).map(a => {
|
||||
if (a.match(/^(?:https?|(?:web\+)?nostr|magnet):/i) && a.toLowerCase() !== "nostr:npub") {
|
||||
const validateLink = () => {
|
||||
const normalizedStr = a.toLowerCase();
|
||||
|
||||
if (normalizedStr.startsWith("web+nostr") || normalizedStr.startsWith("nostr")) {
|
||||
return validateNostrLink(normalizedStr);
|
||||
}
|
||||
|
||||
return (
|
||||
normalizedStr.startsWith("http") ||
|
||||
normalizedStr.startsWith("https") ||
|
||||
normalizedStr.startsWith("magnet")
|
||||
);
|
||||
};
|
||||
|
||||
if (validateLink()) {
|
||||
if (disableMedia ?? false) {
|
||||
return (
|
||||
<a href={a} onClick={e => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">
|
||||
|
@ -491,6 +491,20 @@ export interface NostrLink {
|
||||
encode(): string;
|
||||
}
|
||||
|
||||
export function validateNostrLink(link: string): boolean {
|
||||
try {
|
||||
const parsedLink = parseNostrLink(link);
|
||||
|
||||
if (!parsedLink) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parsedLink.id.length === 64;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function parseNostrLink(link: string): NostrLink | undefined {
|
||||
const entity = link.startsWith("web+nostr:") || link.startsWith("nostr:") ? link.split(":")[1] : link;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user