snort/src/element/Text.js

175 lines
6.0 KiB
JavaScript
Raw Normal View History

2023-01-06 11:05:27 +00:00
import { Link } from "react-router-dom";
2023-01-14 01:39:20 +00:00
import ReactMarkdown from "react-markdown";
2023-01-11 20:23:19 +00:00
import { TwitterTweetEmbed } from "react-twitter-embed";
2023-01-06 11:05:27 +00:00
2023-01-14 14:37:31 +00:00
import { UrlRegex, FileExtensionRegex, MentionRegex, InvoiceRegex, YoutubeUrlRegex, TweetUrlRegex, HashtagRegex } from "../Const";
import { eventLink, hexToBech32, profileLink } from "../Util";
import Invoice from "./Invoice";
import LazyImage from "./LazyImage";
import Hashtag from "./Hashtag";
import './Text.css'
2023-01-14 18:50:24 +00:00
import { useMemo } from "react";
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);
const youtubeId = YoutubeUrlRegex.test(a) && RegExp.$1;
const tweetId = TweetUrlRegex.test(a) && RegExp.$2;
const extension = FileExtensionRegex.test(url.pathname.toLowerCase()) && RegExp.$1;
if (extension) {
switch (extension) {
case "gif":
case "jpg":
case "jpeg":
case "png":
case "bmp":
case "webp": {
return <LazyImage key={url} src={url} />;
}
case "mp4":
case "mov":
case "mkv":
case "avi":
case "m4v": {
return <video key={url} src={url} controls />
}
default:
return <a key={url} href={url} onClick={(e) => e.stopPropagation()}>{url.toString()}</a>
2023-01-14 01:39:20 +00:00
}
} else if (tweetId) {
return (
<div className="tweet" key={tweetId}>
<TwitterTweetEmbed tweetId={tweetId} />
</div>
)
} else if (youtubeId) {
return (
<>
<br />
<iframe
className="w-max"
src={`https://www.youtube.com/embed/${youtubeId}`}
title="YouTube video player"
key={youtubeId}
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowFullScreen=""
/>
<br />
</>
)
} else {
return <a href={a} onClick={(e) => e.stopPropagation()}>{a}</a>
2023-01-06 20:38:51 +00:00
}
} catch (error) {
2023-01-06 20:38:51 +00:00
}
return <a href={a} onClick={(e) => e.stopPropagation()}>{a}</a>
2023-01-06 20:38:51 +00:00
}
2023-01-14 01:39:20 +00:00
function extractLinks(fragments) {
2023-01-06 11:05:27 +00:00
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();
}
2023-01-14 01:39:20 +00:00
function extractMentions(fragments, tags, users) {
2023-01-06 11:05:27 +00:00
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]);
2023-01-14 14:17:17 +00:00
let ref = tags?.find(a => a.Index === idx);
2023-01-06 11:05:27 +00:00
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();
}
2023-01-14 01:39:20 +00:00
function extractInvoices(fragments) {
2023-01-06 11:05:27 +00:00
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
2023-01-14 01:39:20 +00:00
function extractHashtags(fragments) {
2023-01-10 21:31:08 +00:00
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();
}
2023-01-12 09:48:39 +00:00
function transformLi({ body, tags, users }) {
2023-01-14 18:50:24 +00:00
let fragments = transformText({ body, tags, users })
return <li>{fragments}</li>
2023-01-14 01:39:20 +00:00
}
2023-01-12 09:48:39 +00:00
function transformParagraph({ body, tags, users }) {
2023-01-14 18:50:24 +00:00
const fragments = transformText({ body, tags, users })
if (fragments.every(f => typeof f === 'string')) {
return <p>{fragments}</p>
}
return <>{fragments}</>
2023-01-14 01:39:20 +00:00
}
function transformText({ body, tags, users }) {
2023-01-14 14:17:17 +00:00
let fragments = extractMentions(body, tags, users);
2023-01-14 01:39:20 +00:00
fragments = extractLinks(fragments);
fragments = extractInvoices(fragments);
fragments = extractHashtags(fragments);
return fragments;
}
export default function Text({ content, tags, users }) {
2023-01-14 18:50:24 +00:00
const components = useMemo(() => {
return {
p: (props) => transformParagraph({ body: props.children, tags, users }),
a: (props) => transformHttpLink(props.href),
li: (props) => transformLi({ body: props.children, tags, users }),
};
}, [content]);
2023-01-14 14:37:31 +00:00
return <ReactMarkdown className="text" components={components}>{content}</ReactMarkdown>
2023-01-14 01:39:20 +00:00
}