Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
// This val serves HTML pages for a mock Hacker News clone, demonstrating both main story list
// and individual story pages, with comments data structured in a common format.
interface Comment {
user: string;
text: string;
}
interface Story {
title: string;
content: string;
comments: Comment[];
}
// Common data structure for stories and comments
const stories: Record<string, Story> = {
"1": {
title: "First Fake Story",
content: "This is the content of the first fake story. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
comments: [
{ user: "commenter1", text: "This is the first comment on the first fake story." },
{ user: "commenter2", text: "This is the second comment on the first fake story." },
],
},
"2": {
title: "Second Fake Story",
content: "This is the content of the second fake story. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
comments: [
{ user: "commenter3", text: "This is the first comment on the second fake story." },
{ user: "commenter4", text: "This is the second comment on the second fake story." },
],
},
"3": {
title: "Third Fake Story",
content: "This is the content of the third fake story. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
comments: [
{ user: "commenter5", text: "This is the first comment on the third fake story." },
{ user: "commenter6", text: "This is the second comment on the third fake story." },
],
},
};
function renderComments(comments: Comment[]): string {
return comments.map(comment => `
<div class="comment">
<div class="comment-user">${comment.user}</div>
<div class="comment-text">${comment.text}</div>
</div>
`).join("");
}
function renderStoryPage(storyId: string): string {
const story = stories[storyId] || { title: "Story Not Found", content: "The story you are looking for does not exist.", comments: [] };
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${story.title}</title>
<style>
body { font-family: Verdana, Geneva, sans-serif; margin: 0; padding: 0; background: #f6f6ef; }
#header { background: #ff6600; padding: 8px; }
#header a { color: black; font-size: 16px; text-decoration: none; font-weight: bold; }
.container { width: 85%; margin: auto; }
.story-title { font-size: 18px; font-weight: bold; margin: 10px 0; }
.story-content { font-size: 14px; color: #333; }
.comment { padding: 10px 0; border-bottom: 1px solid #ccc; }
.comment-user { font-size: 12px; font-weight: bold; color: #828282; }
.comment-text { font-size: 12px; }
</style>
</head>
<body>
<div id="header" class="container">
<a href="/">Hacker News Clone</a>
</div>
<div class="container">
<div class="story-title">${story.title}</div>
<div class="story-content">${story.content}</div>
<div class="comments">
${renderComments(story.comments)}
</div>
</div>
</body>
</html>
`;
}
function renderMainPage(): string {
const storyHtml = Object.entries(stories).map(([id, story]) => `
<div class="story">
<div class="story-title"><a href="/story/${id}">${story.title}</a></div>
<div class="story-details">100 points by user${id} 1 hour ago | ${story.comments.length} comments</div>
</div>
`).join("");
return `
<!DOCTYPE html>
<html lang="en">
janpaul123-valle_tmp_70764405948939838711130646455814.web.val.run
July 17, 2024