only render hyperlink if link is valid
This commit is contained in:
@ -7,7 +7,7 @@ import * as unist from "unist";
|
|||||||
import { HexKey, NostrPrefix } from "@snort/nostr";
|
import { HexKey, NostrPrefix } from "@snort/nostr";
|
||||||
|
|
||||||
import { MentionRegex, InvoiceRegex, HashtagRegex } from "Const";
|
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 Invoice from "Element/Invoice";
|
||||||
import Hashtag from "Element/Hashtag";
|
import Hashtag from "Element/Hashtag";
|
||||||
import Mention from "Element/Mention";
|
import Mention from "Element/Mention";
|
||||||
@ -36,7 +36,21 @@ export default function Text({ content, tags, creator, disableMedia, depth }: Te
|
|||||||
.map(f => {
|
.map(f => {
|
||||||
if (typeof f === "string") {
|
if (typeof f === "string") {
|
||||||
return splitByUrl(f).map(a => {
|
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) {
|
if (disableMedia ?? false) {
|
||||||
return (
|
return (
|
||||||
<a href={a} onClick={e => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">
|
<a href={a} onClick={e => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">
|
||||||
|
@ -491,6 +491,20 @@ export interface NostrLink {
|
|||||||
encode(): string;
|
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 {
|
export function parseNostrLink(link: string): NostrLink | undefined {
|
||||||
const entity = link.startsWith("web+nostr:") || link.startsWith("nostr:") ? link.split(":")[1] : link;
|
const entity = link.startsWith("web+nostr:") || link.startsWith("nostr:") ? link.split(":")[1] : link;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user