-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.js
81 lines (81 loc) · 2.96 KB
/
helper.js
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
const $ = x => document.getElementById(x);
function mkel(tag, props, children) {
const element = document.createElement(tag);
Object.assign(element, props);
for (const child of children) {
if (child) {
element.append(child);
}
}
return element;
}
function htmlify(json) {
return mkel("div", {"className": "entry"}, [
mkel("dt", {}, [
json.warn ? mkel("span", {}, "⚠\ufe0f ") : null,
mkel("a", {
"className": "toa",
onclick() {navigate(json.head)}
}, [json.head]),
" • ",
mkel("a", {
"className": "scope",
onclick() {navigate("scope:" + json.scope)}
}, [json.scope]),
" ",
mkel("a", {onclick() {navigate("@" + json.user)}}, [json.user]),
" ",
mkel("span", {"className": "score"}, [
("" + json.score).replace(/^0$/, "±").replace(/^(\d)/, "+$1")
]),
" • ",
mkel("a", {onclick() {navigate("#" + json.id)}}, [json.date.slice(0, 10)]),
" ",
mkel("a", {"href": "https://toadua.uakci.space/#" + encodeURIComponent("#" + json.id)}, ["↗"]),
]),
mkel("dd", {}, replaceLinks(json.body)),
mkel("div", {"className": "notes indent"}, json.notes.flatMap(note => [
mkel("span", {"className": "score"}, [
mkel("a", {onclick() {navigate("@" + note.user)}}, [note.user]),
": "
]),
mkel("span", {}, replaceLinks(note.content)),
" ",
mkel("span", {"className": "scope"}, [/^\d/.test(note.date)
? note.date.slice(0, 10)
: new Date(note.date).toISOString().slice(0, 10)]),
mkel("br", {}, [])
]))
]);
}
function replaceLinks(str) {
// ugh why isn't /u a default regex flag
let parts = str
.replace(/\*\*/g, "📦")
.replace(/https?:\/\/([a-z0-9./#%?=&_:'-]+)/giu, "🌐$1🌐")
.replace(/(?<!🌐[^ ]*)(#[a-z0-9_-]{9,})(?=[^a-z0-9_-]|$)/giu, "🆔$1🆔")
.replace(/<((?![/ ])[^>]+(?<! ))>(?!.+<\/\1>)/giu, "📎$1📎")
.match(/([📦🆔🌐📎]).*?\1|[^📦🆔🌐📎]+/ug);
return parts.map(part => {
part = [...part];
let head = part[0], body = part.slice(1, -1).join("")
if (!"📦🆔🌐📎".includes(head)) return part.join("")
if (head === "🌐") {
return mkel("a", { href: body }, [body]);
}
let search = head === '📦' ? '=' + body.replace(/ /g, '|') : body;
return mkel("a", { onclick() { navigate(search) } }, [body]);
})
}
function load(res, page) {
if (!res) return;
const start = page * 100;
const end = (page + 1) * 100;
var nodes = [];
for (var i = start; i < end; i++) {
if (res[i]) {
nodes.push(htmlify(res[i][0]));
}
}
$`res`.append(...nodes);
}