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 will serve an HTML page emulating a Hacker News clone.
// The common data structure for stories and comments is used to render both the main page and individual story pages.
interface Comment {
user: string;
text: string;
}
interface Story {
title: string;
content: string;
comments: Comment[];
}
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." },
],
},
};
export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const pathname = url.pathname;
if (pathname.startsWith("/story/")) {
const storyId = pathname.split("/")[2];
return new Response(renderStoryPage(storyId), {
headers: {
"Content-Type": "text/html",
},
});
}
return new Response(renderHomePage(), {
headers: {
"Content-Type": "text/html",
},
});
}
function renderStoryPage(storyId: string): string {
const story = stories[storyId] || { title: "Story Not Found", content: "The story you are looking for does not exist.", comments: [] };
const commentsHtml = story.comments.map(comment => `
<div class="comment">
<div class="comment-user">${comment.user}</div>
<div class="comment-text">${comment.text}</div>
</div>
`).join("");
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">
${commentsHtml}
</div>
</div>