-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
40 lines (34 loc) · 1.14 KB
/
Copy pathapp.js
File metadata and controls
40 lines (34 loc) · 1.14 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
// chovy.com — render the feed of short (<=240 char) posts, terminal style.
const MAX_LEN = 240;
function escapeHtml(str) {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
// Highlight inline shell snippets like `$ command` for a little terminal flavor.
function highlight(text) {
return escapeHtml(text).replace(/(\$\s[^\n]+)/g, '<span class="sh">$1</span>');
}
function renderPost(post) {
const text = (post.text || "").slice(0, MAX_LEN);
return `
<div class="post">
<div class="meta"><span class="hash">#${post.id}</span> ${escapeHtml(post.date)} · ${text.length}/${MAX_LEN}</div>
<div class="text">${highlight(text)}</div>
</div>`;
}
async function loadFeed() {
const feed = document.getElementById("feed");
try {
const res = await fetch("posts.json", { cache: "no-cache" });
const posts = await res.json();
posts.sort((a, b) => b.id - a.id);
feed.innerHTML = posts.map(renderPost).join("");
} catch (err) {
feed.innerHTML = '<p class="line muted">error: could not load feed.log</p>';
console.error(err);
}
}
loadFeed();