only render hyperlink if link is valid

This commit is contained in:
Sam Samskies 2023-04-19 19:29:37 -05:00
parent 9b2fb3b6bf
commit 74bfa03227
No known key found for this signature in database
GPG Key ID: E3F697EE1B6EB156
2 changed files with 30 additions and 2 deletions

View File

@ -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">

View File

@ -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;