janpaul123-valle_tmp_09983323334565619759844043742776.web.val.run
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
import { blob } from "https://esm.town/v/std/blob";
import { Hono } from "npm:hono";
import { html } from "https://esm.town/v/stevekrouse/html";
// Initialize sample stories and store them in blob storage
const SAMPLE_STORIES_KEY = "hn_sample_stories";
async function initializeSampleStories() {
const existingStories = await blob.getJSON(SAMPLE_STORIES_KEY);
if (!existingStories) {
const sampleStories = [
{ id: 1, title: "Show HN: This website recreates the feeling of playing a roguelike in a terminal", url: "https://example.com/story1", votes: 75 },
{ id: 2, title: "Ask HN: How do I learn Deno?", url: "https://example.com/story2", votes: 150 },
{ id: 3, title: "Tell HN: I built a new static site generator", url: "https://example.com/story3", votes: 200 },
{ id: 4, title: "Launch HN: We're Kicking off our New AI Service for Developers", url: "https://example.com/story4", votes: 50 },
// ... (add more sample stories as needed)
];
await blob.setJSON(SAMPLE_STORIES_KEY, sampleStories);
}
}
await initializeSampleStories();
// Hono app setup
const app = new Hono();
// Serve main page with stories
app.get("/", async (c) => {
const stories = await blob.getJSON(SAMPLE_STORIES_KEY) || [];
const storiesHtml = stories
.sort((a, b) => b.votes - a.votes)
.map(story => `<tr class="athing">
<td align="right" valign="top" class="title">${story.votes}</td>
<td valign="top" class="votelinks">
<form method="POST" action="/vote" style="display:inline;">
<input type="hidden" name="id" value="${story.id}" />
<button type="submit" class="votearrow" title="upvote">▲</button>
</form>
</td>
<td class="title">
<a href="${story.url}" class="storylink">${story.title}</a>
</td>
</tr>`).join("");
return html(`
<html>
<head>
<title>Hacker News</title>
<style>
body {
background-color: #f6f6ef;
font-family: Verdana, Geneva, sans-serif;
}
.hnhead {
background-color: #ff6600;
padding: 2px;
color: white;
}
.hnheader {
font-size: 14px;
}
.storylink {
color: #000;
text-decoration: none;
}
.athing:hover {
background-color: #fdf5e6;
}
.title {
padding: 2px;
}
.votearrow {
cursor: pointer;
border: none;
background: none;
outline: none;
font-size: 16px;
color: #ff6600;
}
.submit-button {
display: block;
margin: 15px 0;
font-size: 14px;
color: #ff6600;
text-decoration: none;
}
</style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="85%">
<tr>
<td bgcolor="#ff6600">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="padding: 2px;">
<tr>
<td style="width: 18px; padding-right: 4px;"><img src="https://news.ycombinator.com/y18.gif" alt="" /></td>
<td class="hnhead"><a href="/" class="hnheader">Hacker News</a></td>
<td align="right" class="hnhead"><a href="/submit" class="hnheader">submit</a></td>
</tr>
</table>
</td>
</tr>
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 17, 2024