-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.js
More file actions
55 lines (47 loc) · 1.28 KB
/
menu.js
File metadata and controls
55 lines (47 loc) · 1.28 KB
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
console.log("Loading...");
const result = fetch("/api/content")
.then(res => res.json())
.then(result => {
display(result);
});
function display(submissions) {
const list = document.getElementById("results");
list.innerHTML = "";
submissions.forEach(addItemTo(list));
}
function addItemTo(list) {
return r => {
const a = document.createElement("a");
a.innerHTML = render(r.content);
a.href = r.url;
addVotes(a, r);
const li = document.createElement("li");
li.appendChild(a);
list.appendChild(li);
};
}
function addVotes(to, r) {
const votes = div("votes");
votes.appendChild(div("upvotes", `↑${r.upvotes}`));
votes.appendChild(div("downvotes", `↓${r.downvotes}`));
to.appendChild(votes);
}
function div(className, content) {
const div = document.createElement("div");
div.className = className;
div.innerHTML = content || "";
return div;
}
function render(content) {
return marked(
decodeBlockquotes(
removeAutoLinks(content)), {
breaks: true
});
}
function decodeBlockquotes(content) {
return content.replace(/>+/g, '>');
}
function removeAutoLinks(content) {
return content.replace(/<http:\/\/.+\|(.+)>/g, '$1');
}