snort/src/Text.js

138 lines
4.8 KiB
JavaScript
Raw Normal View History

2023-01-06 11:05:27 +00:00
import { Link } from "react-router-dom";
2023-01-11 20:23:19 +00:00
import { TwitterTweetEmbed } from "react-twitter-embed";
2023-01-06 11:05:27 +00:00
import Invoice from "./element/Invoice";
2023-01-11 20:23:19 +00:00
import { UrlRegex, FileExtensionRegex, MentionRegex, InvoiceRegex, YoutubeUrlRegex, TweetUrlRegex, HashtagRegex } from "./Const";
2023-01-09 16:18:34 +00:00
import { eventLink, hexToBech32, profileLink } from "./Util";
2023-01-10 10:30:33 +00:00
import LazyImage from "./element/LazyImage";
2023-01-10 21:31:08 +00:00
import Hashtag from "./element/Hashtag";
2023-01-06 11:05:27 +00:00
2023-01-06 20:38:51 +00:00
function transformHttpLink(a) {
try {
const url = new URL(a);
2023-01-09 16:18:34 +00:00
const youtubeId = YoutubeUrlRegex.test(a) && RegExp.$1;
2023-01-11 20:23:19 +00:00
const tweetId = TweetUrlRegex.test(a) && RegExp.$2;
2023-01-09 16:18:34 +00:00
const extension = FileExtensionRegex.test(url.pathname.toLowerCase()) && RegExp.$1;
2023-01-06 20:38:51 +00:00
if (extension) {
switch (extension) {
case "gif":
case "jpg":
case "jpeg":
case "png":
case "bmp":
case "webp": {
2023-01-10 10:30:33 +00:00
return <LazyImage key={url} src={url} />;
2023-01-06 20:38:51 +00:00
}
case "mp4":
case "mov":
case "mkv":
case "avi":
case "m4v": {
return <video key={url} src={url} controls />
}
2023-01-08 00:29:59 +00:00
default:
2023-01-09 16:18:34 +00:00
return <a key={url} href={url} onClick={(e) => e.stopPropagation()}>{url.toString()}</a>
2023-01-06 20:38:51 +00:00
}
2023-01-11 20:23:19 +00:00
} else if (tweetId) {
return (
<div className="tweet">
<TwitterTweetEmbed tweetId={tweetId} />
</div>
)
2023-01-06 20:38:51 +00:00
} else if (youtubeId) {
return (
2023-01-08 00:29:59 +00:00
<>
<br />
<iframe
2023-01-10 16:22:41 +00:00
className="w-max"
2023-01-08 00:29:59 +00:00
src={`https://www.youtube.com/embed/${youtubeId}`}
title="YouTube video player"
2023-01-09 16:18:34 +00:00
frameBorder="0"
2023-01-08 00:29:59 +00:00
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
2023-01-10 09:52:51 +00:00
allowFullScreen=""
2023-01-08 00:29:59 +00:00
/>
<br />
</>
2023-01-06 20:38:51 +00:00
)
} else {
2023-01-09 16:18:34 +00:00
return <a key={url} href={url} onClick={(e) => e.stopPropagation()}>{url.toString()}</a>
2023-01-06 20:38:51 +00:00
}
} catch (e) {
console.warn(`Not a valid url: ${a}`);
}
}
2023-01-06 11:05:27 +00:00
export function extractLinks(fragments) {
return fragments.map(f => {
if (typeof f === "string") {
return f.split(UrlRegex).map(a => {
if (a.startsWith("http")) {
2023-01-08 00:29:59 +00:00
return transformHttpLink(a)
2023-01-06 11:05:27 +00:00
}
return a;
});
}
return f;
}).flat();
}
export function extractMentions(fragments, tags, users) {
return fragments.map(f => {
if (typeof f === "string") {
return f.split(MentionRegex).map((match) => {
let matchTag = match.match(/#\[(\d+)\]/);
if (matchTag && matchTag.length === 2) {
let idx = parseInt(matchTag[1]);
let ref = tags.find(a => a.Index === idx);
if (ref) {
switch (ref.Key) {
case "p": {
2023-01-09 16:18:34 +00:00
let pUser = users[ref.PubKey]?.name ?? hexToBech32("npub", ref.PubKey).substring(0, 12);
return <Link key={ref.PubKey} to={profileLink(ref.PubKey)} onClick={(e) => e.stopPropagation()}>@{pUser}</Link>;
2023-01-06 11:05:27 +00:00
}
case "e": {
2023-01-09 16:18:34 +00:00
let eText = hexToBech32("note", ref.Event).substring(0, 12);
return <Link key={ref.Event} to={eventLink(ref.Event)} onClick={(e) => e.stopPropagation()}>#{eText}</Link>;
2023-01-06 11:05:27 +00:00
}
}
}
return <b style={{ color: "var(--error)" }}>{matchTag[0]}?</b>;
2023-01-06 11:05:27 +00:00
} else {
return match;
}
});
}
return f;
}).flat();
}
export function extractInvoices(fragments) {
return fragments.map(f => {
if (typeof f === "string") {
return f.split(InvoiceRegex).map(i => {
if (i.toLowerCase().startsWith("lnbc")) {
return <Invoice key={i} invoice={i} />
} else {
return i;
}
});
}
return f;
}).flat();
}
2023-01-10 21:31:08 +00:00
export function extractHashtags(fragments) {
return fragments.map(f => {
if (typeof f === "string") {
return f.split(HashtagRegex).map(i => {
if (i.toLowerCase().startsWith("#")) {
return <Hashtag>{i}</Hashtag>
} else {
return i;
}
});
}
return f;
}).flat();
}