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
|
import * as nip19 from "nostr-tools/nip19";
export const NEWLINE = "newline";
export const TEXT = "text";
export const TOPIC = "topic";
export const LINK = "link";
export const INVOICE = "invoice";
export const NOSTR_NOTE = "nostr:note";
export const NOSTR_NEVENT = "nostr:nevent";
export const NOSTR_NPUB = "nostr:npub";
export const NOSTR_NPROFILE = "nostr:nprofile";
export const NOSTR_NADDR = "nostr:naddr";
const first = (list: any) => (list ? list[0] : undefined);
export const fromNostrURI = (s: string) => s.replace(/^[\w+]+:\/?\/?/, "");
export const urlIsMedia = (url: string) =>
!url.match(/\.(apk|docx|xlsx|csv|dmg)/) && url.split("://").pop()?.includes("/");
export type ParsedPart = {
type: string;
value: any;
};
export const isImage = (url: string) => url?.match(/^.*\.(jpg|jpeg|png|webp|gif|avif|svg)/gi);
export const isVideo = (url: string) => url?.match(/^.*\.(mov|mkv|mp4|avi|m4v|webm)/gi);
export const isAudio = (url: string) => url?.match(/^.*\.(ogg|mp3|wav)/gi);
type ContentArgs = {
content: string;
tags?: Array<[string, string, string]>;
};
export const parseContent = ({ content, tags = [] }: ContentArgs): ParsedPart[] => {
const result: ParsedPart[] = [];
let text = content.trim();
let buffer = "";
const parseNewline = () => {
const newline = first(text.match(/^\n+/));
if (newline) {
return [NEWLINE, newline, newline];
}
};
const parseMention = () => {
const mentionMatch = text.match(/^#\[(\d+)\]/i);
if (mentionMatch) {
const i = parseInt(mentionMatch[1]);
if (tags[i]) {
const [tag, value, url] = tags[i];
const relays = url ? [url] : [];
let type, data, entity;
try {
if (tag === "p") {
type = "nprofile";
data = { pubkey: value, relays };
entity = nip19.nprofileEncode(data);
} else {
type = "nevent";
data = { id: value, relays, pubkey: null };
entity = nip19.neventEncode(data);
}
} catch {
/**/
}
return [`nostr:${type}`, mentionMatch[0], { ...data, entity }];
}
}
};
const parseTopic = () => {
const topic = first(text.match(/^#\w+/i));
if (topic && !topic.match(/^#\d+$/)) {
return [TOPIC, topic, topic.slice(1)];
}
};
const parseBech32 = () => {
const bech32 = first(
text.match(/^(web\+)?(nostr:)?\/?\/?n(event|ote|profile|pub|addr)1[\d\w]+/i),
);
if (bech32) {
try {
const entity = fromNostrURI(bech32);
const { type, data } = nip19.decode(entity) as {
type: string;
data: object;
};
let value = data;
if (type === "note") {
value = { id: data };
} else if (type === "npub") {
value = { pubkey: data };
}
return [`nostr:${type}`, bech32, { ...value, entity }];
} catch (e) {
console.log(e);
}
}
};
const parseUrl = () => {
const raw = first(text.match(/^([a-z+:]{2,30}:\/\/)?[^\s]+\.[a-z]{2,6}[^\s]*[^.!?,:\s]/gi));
if (raw) {
const prev = result[result.length - 1];
if (prev?.type === "text" && prev.value.endsWith("/")) {
return;
}
let url = raw;
if (url.match(/\.\./)) {
return;
}
if (!url.match("://")) {
url = "https://" + url;
}
return [LINK, raw, { url, isMedia: urlIsMedia(url) }];
}
};
while (text) {
const part =
parseNewline() ||
parseMention() ||
parseTopic() ||
parseBech32() ||
parseUrl();
if (part) {
if (buffer) {
result.push({ type: "text", value: buffer });
buffer = "";
}
const [type, raw, value] = part;
result.push({ type, value });
text = text.slice(raw.length);
} else {
const match = first(text.match(/^[\w\d]+ ?/i)) || text[0];
buffer += match;
text = text.slice(match.length);
}
}
if (buffer) {
result.push({ type: TEXT, value: buffer });
}
return result;
};
|