1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
import type {
// TODO ref backend fetching!!
Reference,
Block,
Inline,
Media as MediaType,
ExternalContent,
} from "@/types/trill";
import crow from "@/assets/icons/crow.svg";
import type { PostProps } from "./Post";
import Media from "./Media";
import JSONContent, { YoutubeSnippet } from "./External";
import { useLocation } from "wouter";
import Quote from "./Quote";
import PostData from "./Loader";
import Card from "./Card.tsx";
import type { Ship } from "@/types/urbit.ts";
function Body(props: PostProps) {
const text = props.poast.contents.filter((c) => {
return (
"paragraph" in c ||
"blockquote" in c ||
"heading" in c ||
"codeblock" in c ||
"list" in c
);
});
const media: MediaType[] = props.poast.contents.filter(
(c): c is MediaType => "media" in c,
);
const refs = props.poast.contents.filter((c): c is Reference => "ref" in c);
const json = props.poast.contents.filter(
(c): c is ExternalContent => "json" in c,
);
return (
<div className="trill-post-body body">
<div className="body-text">
{text.map((b, i) => (
<TextBlock key={JSON.stringify(b) + i} block={b} />
))}
</div>
{media.length > 0 && <Media media={media} />}
{refs.map((r, i) => (
<Ref r={r} nest={props.nest || 0} key={JSON.stringify(r) + i} />
))}
<JSONContent content={json} />
</div>
);
}
export default Body;
function TextBlock({ block }: { block: Block }) {
const key = JSON.stringify(block);
return "paragraph" in block ? (
<div className="trill-post-paragraph">
{block.paragraph.map((i, ind) => (
<Inlin key={key + ind} i={i} />
))}
</div>
) : "blockquote" in block ? (
<blockquote>
{block.blockquote.map((i, ind) => (
<Inlin key={key + ind} i={i} />
))}
</blockquote>
) : "heading" in block ? (
<Heading string={block.heading.text} num={block.heading.num} />
) : "codeblock" in block ? (
<pre>
<code className={`language-${block.codeblock.lang}`}>
{block.codeblock.code}
</code>
</pre>
) : "list" in block ? (
block.list.ordered ? (
<ol>
{block.list.text.map((i, ind) => (
<li key={JSON.stringify(i) + ind}>
<Inlin key={key + ind} i={i} />
</li>
))}
</ol>
) : (
<ul>
{block.list.text.map((i, ind) => (
<li key={JSON.stringify(i) + ind}>
<Inlin key={JSON.stringify(i) + ind} i={i} />
</li>
))}
</ul>
)
) : null;
}
function Inlin({ i }: { i: Inline }) {
const [_, navigate] = useLocation();
function gotoShip(e: React.MouseEvent, ship: Ship) {
e.stopPropagation();
navigate(`/feed/${ship}`);
}
return "text" in i ? (
<span>{i.text}</span>
) : "italic" in i ? (
<i>{i.italic}</i>
) : "bold" in i ? (
<strong>{i.bold}</strong>
) : "strike" in i ? (
<span>{i.strike}</span>
) : "underline" in i ? (
<span>{i.underline}</span>
) : "sup" in i ? (
<sup>{i.sup}</sup>
) : "sub" in i ? (
<sub>{i.sub}</sub>
) : "ship" in i ? (
<span
className="mention"
role="link"
onMouseUp={(e) => gotoShip(e, i.ship)}
>
{i.ship}
</span>
) : "codespan" in i ? (
<code>{i.codespan}</code>
) : "link" in i ? (
<LinkParser {...i.link} />
) : "break" in i ? (
<br />
) : null;
}
function LinkParser({ href, show }: { href: string; show: string }) {
const YOUTUBE_REGEX_1 = /(youtube\.com\/watch\?v=)(\w+)/;
const YOUTUBE_REGEX_2 = /(youtu\.be\/)([a-zA-Z0-9-_]+)/;
const m1 = href.match(YOUTUBE_REGEX_1);
const m2 = href.match(YOUTUBE_REGEX_2);
const ytb = m1 && m1[2] ? m1[2] : m2 && m2[2] ? m2[2] : "";
return ytb ? (
<YoutubeSnippet href={href} id={ytb} />
) : (
<a href={href}>{show}</a>
);
}
function Heading({ string, num }: { string: string; num: number }) {
return num === 1 ? (
<h1>{string}</h1>
) : num === 2 ? (
<h2>{string}</h2>
) : num === 3 ? (
<h3>{string}</h3>
) : num === 4 ? (
<h4>{string}</h4>
) : num === 5 ? (
<h5>{string}</h5>
) : num === 6 ? (
<h6>{string}</h6>
) : null;
}
function Ref({ r, nest }: { r: Reference; nest: number }) {
if (r.ref.type === "nostril") {
const comp = PostData({
host: r.ref.ship,
id: r.ref.path.slice(1),
nest: nest + 1,
className: "quote-in-post",
})(Quote);
return <Card logo={crow}>{comp}</Card>;
}
return <></>;
}
|