2023-02-07 20:04:50 +00:00
|
|
|
import "./Text.css";
|
2023-01-27 21:37:04 +00:00
|
|
|
import { useMemo, useCallback } from "react";
|
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-27 21:37:04 +00:00
|
|
|
import { visit, SKIP } from "unist-util-visit";
|
2023-01-06 11:05:27 +00:00
|
|
|
|
2023-02-03 16:43:21 +00:00
|
|
|
import { UrlRegex, MentionRegex, InvoiceRegex, HashtagRegex } from "Const";
|
2023-02-07 19:47:57 +00:00
|
|
|
import { eventLink, hexToBech32, unwrap } from "Util";
|
2023-01-20 11:11:50 +00:00
|
|
|
import Invoice from "Element/Invoice";
|
|
|
|
import Hashtag from "Element/Hashtag";
|
2023-01-14 14:37:31 +00:00
|
|
|
|
2023-01-20 11:11:50 +00:00
|
|
|
import Tag from "Nostr/Tag";
|
2023-01-27 21:38:41 +00:00
|
|
|
import { MetadataCache } from "State/Users";
|
2023-01-20 11:11:50 +00:00
|
|
|
import Mention from "Element/Mention";
|
2023-02-07 20:04:50 +00:00
|
|
|
import HyperText from "Element/HyperText";
|
|
|
|
import { HexKey } from "Nostr";
|
2023-02-07 19:47:57 +00:00
|
|
|
import * as unist from "unist";
|
2023-01-06 11:05:27 +00:00
|
|
|
|
2023-02-07 19:47:57 +00:00
|
|
|
export type Fragment = string | React.ReactNode;
|
2023-01-06 20:38:51 +00:00
|
|
|
|
2023-02-03 16:43:21 +00:00
|
|
|
export interface TextFragment {
|
2023-02-07 19:47:57 +00:00
|
|
|
body: React.ReactNode[];
|
2023-02-07 20:04:50 +00:00
|
|
|
tags: Tag[];
|
|
|
|
users: Map<string, MetadataCache>;
|
2023-01-06 11:05:27 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 16:43:21 +00:00
|
|
|
export interface TextProps {
|
2023-02-07 20:04:50 +00:00
|
|
|
content: string;
|
|
|
|
creator: HexKey;
|
|
|
|
tags: Tag[];
|
|
|
|
users: Map<string, MetadataCache>;
|
2023-01-06 11:05:27 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 16:43:21 +00:00
|
|
|
export default function Text({ content, tags, creator, users }: TextProps) {
|
2023-02-07 20:04:50 +00:00
|
|
|
function extractLinks(fragments: Fragment[]) {
|
|
|
|
return fragments
|
2023-02-09 12:26:54 +00:00
|
|
|
.map(f => {
|
2023-02-07 20:04:50 +00:00
|
|
|
if (typeof f === "string") {
|
2023-02-09 12:26:54 +00:00
|
|
|
return f.split(UrlRegex).map(a => {
|
2023-02-07 20:04:50 +00:00
|
|
|
if (a.startsWith("http")) {
|
2023-02-12 22:06:21 +00:00
|
|
|
return <HyperText key={a} link={a} creator={creator} />;
|
2023-02-03 16:43:21 +00:00
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
return a;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
})
|
|
|
|
.flat();
|
|
|
|
}
|
2023-01-12 09:48:39 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
function extractMentions(frag: TextFragment) {
|
|
|
|
return frag.body
|
2023-02-09 12:26:54 +00:00
|
|
|
.map(f => {
|
2023-02-07 20:04:50 +00:00
|
|
|
if (typeof f === "string") {
|
2023-02-09 12:26:54 +00:00
|
|
|
return f.split(MentionRegex).map(match => {
|
2023-02-07 19:47:57 +00:00
|
|
|
const matchTag = match.match(/#\[(\d+)\]/);
|
2023-02-07 20:04:50 +00:00
|
|
|
if (matchTag && matchTag.length === 2) {
|
2023-02-07 19:47:57 +00:00
|
|
|
const idx = parseInt(matchTag[1]);
|
2023-02-09 12:26:54 +00:00
|
|
|
const ref = frag.tags?.find(a => a.Index === idx);
|
2023-02-07 20:04:50 +00:00
|
|
|
if (ref) {
|
|
|
|
switch (ref.Key) {
|
|
|
|
case "p": {
|
2023-02-12 22:06:21 +00:00
|
|
|
return <Mention key={ref.PubKey} pubkey={ref.PubKey ?? ""} />;
|
2023-02-07 20:04:50 +00:00
|
|
|
}
|
|
|
|
case "e": {
|
2023-02-09 12:26:54 +00:00
|
|
|
const eText = hexToBech32("note", ref.Event).substring(0, 12);
|
2023-02-07 20:04:50 +00:00
|
|
|
return (
|
2023-02-09 12:26:54 +00:00
|
|
|
<Link key={ref.Event} to={eventLink(ref.Event ?? "")} onClick={e => e.stopPropagation()}>
|
2023-02-07 20:04:50 +00:00
|
|
|
#{eText}
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "t": {
|
2023-02-12 22:06:21 +00:00
|
|
|
return <Hashtag key={ref.Hashtag} tag={ref.Hashtag ?? ""} />;
|
2023-02-07 20:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return <b style={{ color: "var(--error)" }}>{matchTag[0]}?</b>;
|
|
|
|
} else {
|
|
|
|
return match;
|
2023-02-03 16:43:21 +00:00
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
})
|
|
|
|
.flat();
|
|
|
|
}
|
2023-01-12 09:48:39 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
function extractInvoices(fragments: Fragment[]) {
|
|
|
|
return fragments
|
2023-02-09 12:26:54 +00:00
|
|
|
.map(f => {
|
2023-02-07 20:04:50 +00:00
|
|
|
if (typeof f === "string") {
|
2023-02-09 12:26:54 +00:00
|
|
|
return f.split(InvoiceRegex).map(i => {
|
2023-02-07 20:04:50 +00:00
|
|
|
if (i.toLowerCase().startsWith("lnbc")) {
|
|
|
|
return <Invoice key={i} invoice={i} />;
|
|
|
|
} else {
|
|
|
|
return i;
|
2023-02-03 16:43:21 +00:00
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
})
|
|
|
|
.flat();
|
|
|
|
}
|
2023-01-14 01:39:20 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
function extractHashtags(fragments: Fragment[]) {
|
|
|
|
return fragments
|
2023-02-09 12:26:54 +00:00
|
|
|
.map(f => {
|
2023-02-07 20:04:50 +00:00
|
|
|
if (typeof f === "string") {
|
2023-02-09 12:26:54 +00:00
|
|
|
return f.split(HashtagRegex).map(i => {
|
2023-02-07 20:04:50 +00:00
|
|
|
if (i.toLowerCase().startsWith("#")) {
|
2023-02-12 22:06:21 +00:00
|
|
|
return <Hashtag key={i} tag={i.substring(1)} />;
|
2023-02-07 20:04:50 +00:00
|
|
|
} else {
|
|
|
|
return i;
|
2023-02-03 16:43:21 +00:00
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
})
|
|
|
|
.flat();
|
|
|
|
}
|
2023-01-14 01:39:20 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
function transformLi(frag: TextFragment) {
|
2023-02-07 19:47:57 +00:00
|
|
|
const fragments = transformText(frag);
|
2023-02-07 20:04:50 +00:00
|
|
|
return <li>{fragments}</li>;
|
|
|
|
}
|
2023-01-16 17:48:25 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
function transformParagraph(frag: TextFragment) {
|
|
|
|
const fragments = transformText(frag);
|
2023-02-09 12:26:54 +00:00
|
|
|
if (fragments.every(f => typeof f === "string")) {
|
2023-02-13 17:33:24 +00:00
|
|
|
return <p>{fragments}</p>;
|
2023-02-03 16:43:21 +00:00
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
return <>{fragments}</>;
|
|
|
|
}
|
2023-01-16 17:48:25 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
function transformText(frag: TextFragment) {
|
|
|
|
let fragments = extractMentions(frag);
|
|
|
|
fragments = extractLinks(fragments);
|
|
|
|
fragments = extractInvoices(fragments);
|
|
|
|
fragments = extractHashtags(fragments);
|
|
|
|
return fragments;
|
|
|
|
}
|
2023-01-16 17:48:25 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
const components = useMemo(() => {
|
|
|
|
return {
|
2023-02-09 12:26:54 +00:00
|
|
|
p: (x: { children?: React.ReactNode[] }) => transformParagraph({ body: x.children ?? [], tags, users }),
|
|
|
|
a: (x: { href?: string }) => <HyperText link={x.href ?? ""} creator={creator} />,
|
|
|
|
li: (x: { children?: Fragment[] }) => transformLi({ body: x.children ?? [], tags, users }),
|
2023-02-07 20:04:50 +00:00
|
|
|
};
|
|
|
|
}, [content]);
|
2023-02-03 16:43:21 +00:00
|
|
|
|
2023-02-07 19:47:57 +00:00
|
|
|
interface Node extends unist.Node<unist.Data> {
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
const disableMarkdownLinks = useCallback(
|
2023-02-07 19:47:57 +00:00
|
|
|
() => (tree: Node) => {
|
2023-02-07 20:04:50 +00:00
|
|
|
visit(tree, (node, index, parent) => {
|
|
|
|
if (
|
|
|
|
parent &&
|
|
|
|
typeof index === "number" &&
|
|
|
|
(node.type === "link" ||
|
|
|
|
node.type === "linkReference" ||
|
|
|
|
node.type === "image" ||
|
|
|
|
node.type === "imageReference" ||
|
|
|
|
node.type === "definition")
|
|
|
|
) {
|
|
|
|
node.type = "text";
|
2023-02-07 19:47:57 +00:00
|
|
|
const position = unwrap(node.position);
|
2023-02-09 12:26:54 +00:00
|
|
|
node.value = content.slice(position.start.offset, position.end.offset).replace(/\)$/, " )");
|
2023-02-07 20:04:50 +00:00
|
|
|
return SKIP;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[content]
|
|
|
|
);
|
|
|
|
return (
|
2023-02-13 17:33:24 +00:00
|
|
|
<div dir="auto">
|
|
|
|
<ReactMarkdown className="text" components={components} remarkPlugins={[disableMarkdownLinks]}>
|
|
|
|
{content}
|
|
|
|
</ReactMarkdown>
|
|
|
|
</div>
|
2023-02-07 20:04:50 +00:00
|
|
|
);
|
2023-01-14 01:39:20 +00:00
|
|
|
}
|