Skip to content

Commit cafa9bc

Browse files
committed
Merge Read Hacker News site into bot project
Build the directory from source alongside the bot, replacing checked-in HTML with generated assets. Add single-page SEO metadata and crawl files while preserving historical short links. Use the typed awesome-hacker-news dataset with a compatible version range and verify rendering, builds and HTTP delivery in CI.
1 parent 6cccd42 commit cafa9bc

29 files changed

Lines changed: 758 additions & 28 deletions

‎.github/workflows/ci.yml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: CI
2+
on: [push, pull_request]
3+
permissions:
4+
contents: read
5+
jobs:
6+
verify:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
- uses: oven-sh/setup-bun@v2
11+
with:
12+
bun-version: 1.4.4
13+
- run: bun install --frozen-lockfile
14+
- run: bun run test
15+
- run: bun run check
16+
- run: bun run build:prod
17+
# Verify the production-minified bytes too; no credentials or database files.
18+
- run: bun --no-env-file test tests/site.test.ts

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ node_modules/
1010
lib/
1111
app.yaml
1212
setup.cfg
13+
14+
# Generated site: run bun run build before start/deployment.
15+
/static/

‎README.md‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# hackernewsbot
22

3-
Telegram bot that posts hot Hacker News stories to
4-
[@hacker_news_feed](https://telegram.me/hacker_news_feed).
3+
The [Read Hacker News](https://readhacker.news/) directory and the Telegram bot
4+
behind [@hacker_news_feed](https://telegram.me/hacker_news_feed), built and served
5+
from one Bun/TypeScript project.
6+
7+
You can [preview the directory](#read-hacker-news-directory) without bot credentials
8+
or a database.
59

610
## Backend
711

@@ -39,7 +43,7 @@ Uses an internal shortener at `readhacker.news`.
3943
project-root `.env`; local/mode env files are not loaded.
4044
- For a new installation, run `bun run db:init` once.
4145
It refuses to overwrite an existing database.
42-
- Run `bun run start` for HTTP and schedule `bun run cron`
46+
- Run `bun run build:prod`, then `bun run start` for HTTP and schedule `bun run cron`
4347
every 10 minutes from the project root, using a non-overlap lock.
4448
There is no HTTP `/cron` endpoint.
4549
- Keep this bot's database on persistent storage, shared by HTTP and cron
@@ -52,10 +56,24 @@ Uses an internal shortener at `readhacker.news`.
5256
Use a separate database, `BASE_URL=http://localhost:8080`, and a test
5357
channel such as `@hacker_news_feed_st`.
5458

55-
Run `bun run dev`, `bun test` for offline tests, and
59+
Run `bun run dev`, `bun run test` for offline tests, and
5660
`bun run check` for typechecking.
5761

5862
## See also
5963

64+
- [awesome-hacker-news](https://github.com/phil-r/awesome-hacker-news)
6065
- [designernewsbot](https://github.com/phil-r/designernewsbot)
6166
- [asciifacesbot](https://github.com/phil-r/asciifacesbot)
67+
68+
## Read Hacker News directory
69+
70+
Site source lives in `app/` and builds into `static/`, served by the bot's HTTP server.
71+
72+
```sh
73+
bun run site:dev # preview on http://localhost:9000; rebuilds on edits
74+
bun run build:prod # build static/ before starting the HTTP server
75+
```
76+
77+
For a different domain, set `SITE_ORIGIN` when building:
78+
`SITE_ORIGIN=https://directory.example bun run build:prod`. It sets the site's
79+
canonical URL and is separate from the bot's `BASE_URL`; builds do not read `.env`.

‎app/images/github.svg‎

Lines changed: 1 addition & 0 deletions
Loading

‎app/images/logo.svg‎

Lines changed: 1 addition & 0 deletions
Loading

‎app/render.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expect, test } from "bun:test";
2+
import news from "awesome-hacker-news";
3+
import { renderPage } from "./render.ts";
4+
5+
type Source = typeof news.sources[number]["items"][number];
6+
const render = (entry: Source) => renderPage("", [{ name: "Test", items: [entry] }]);
7+
8+
test("renders the published active dataset, including Gopher, without graveyard entries", async () => {
9+
const html = await renderPage("", news.sources);
10+
expect(html.match(/<h2>/g)).toHaveLength(news.sources.length);
11+
expect(html.match(/<li>/g)).toHaveLength(news.sources.reduce((count, group) => count + group.items.length, 0));
12+
expect(html).toContain("HN Gopher: gopher://hngopher.com/1</li>");
13+
expect(html).not.toContain('href="gopher:');
14+
expect(html).not.toContain("TiledHN");
15+
expect(html).not.toContain("graveyard");
16+
});
17+
18+
test("text references and names are escaped plain text, never anchors", async () => {
19+
const name = '<img src=x onerror="alert(1)"> &';
20+
const textReference = 'gopher://example/<script>"&';
21+
const html = await render({ name, textReference });
22+
expect(html).toContain(`<li>${Bun.escapeHTML(name)}: ${Bun.escapeHTML(textReference)}</li>`);
23+
expect(html).not.toContain(`<img src=x`);
24+
expect(html).not.toContain(`href="${Bun.escapeHTML(textReference)}"`);
25+
});
26+
27+
test("actual links still require HTTP(S), including author and repository URLs", async () => {
28+
for (const url of ["javascript:alert(1)", "data:text/html,unsafe", "gopher://example/1", "not a URL"]) {
29+
await expect(render({ name: "Bad", url })).rejects.toThrow();
30+
await expect(render({ name: "Bad", textReference: "safe text", url } as unknown as Source)).rejects.toThrow();
31+
await expect(render({ name: "Bad", url: "https://example.com", author: { name: "Bad", url } })).rejects.toThrow();
32+
await expect(render({ name: "Bad", textReference: "safe text", source: { type: "github", url } })).rejects.toThrow();
33+
}
34+
});
35+
36+
test("broken entries fail rather than disappearing or falling back to text", async () => {
37+
await expect(render({ name: "Missing" } as Source)).rejects.toThrow("Missing dataset URL or text reference");
38+
await expect(render({ name: "Broken", url: undefined, textReference: "fallback" } as unknown as Source)).rejects.toThrow();
39+
});

‎app/render.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type news from "awesome-hacker-news";
2+
import { siteCanonicalURL, description, title } from "./seo.ts";
3+
4+
type Sources = typeof news.sources;
5+
type Source = Sources[number]["items"][number];
6+
7+
function href(url: string): string {
8+
const { protocol } = new URL(url);
9+
if (protocol !== "http:" && protocol !== "https:") {
10+
throw new Error(`Unsafe dataset URL: ${url}`);
11+
}
12+
// Validate the scheme without normalizing the dataset's original URL.
13+
return Bun.escapeHTML(url);
14+
}
15+
16+
function sourceLink(source: Source, github: string): string {
17+
const repository = source.source?.type === "github"
18+
? `<a class="icon-link" aria-label="Source code for ${Bun.escapeHTML(source.name)}" target="_blank" href="${href(source.source.url)}">${github}</a>`
19+
: "";
20+
const author = source.author
21+
? ` by ${source.author.url
22+
? `<a class="link" target="_blank" href="${href(source.author.url)}">${Bun.escapeHTML(source.author.name)}</a>`
23+
: Bun.escapeHTML(source.author.name)}`
24+
: "";
25+
if (!("url" in source) && typeof source.textReference !== "string") {
26+
throw new Error("Missing dataset URL or text reference");
27+
}
28+
const name = Bun.escapeHTML(source.name);
29+
const entry = "url" in source
30+
? `<a class="link" target="_blank" href="${href(source.url!)}">${name}</a>`
31+
: `${name}: ${Bun.escapeHTML(source.textReference)}`;
32+
return `<li>${entry}${repository}${author}</li>`;
33+
}
34+
35+
export async function renderPage(css: string, sources: Sources, canonicalURL = siteCanonicalURL()): Promise<string> {
36+
const [logo, github, datasetLicense] = await Promise.all([
37+
Bun.file(new URL("./images/logo.svg", import.meta.url)).text(),
38+
Bun.file(new URL("./images/github.svg", import.meta.url)).text(),
39+
Bun.file(Bun.resolveSync("awesome-hacker-news/LICENSE", import.meta.dir)).text(),
40+
]);
41+
const groups = sources.map(group =>
42+
`<div class="columns-panel"><div class="columns-panel-content">` +
43+
`<h2>${Bun.escapeHTML(group.name)}</h2><ul>` +
44+
group.items.map(source => sourceLink(source, github)).join("") +
45+
`</ul></div></div>`
46+
).join("");
47+
48+
// CSS and local SVG files are trusted build inputs, not dataset text.
49+
return `<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><!-- awesome-hacker-news dataset license:
50+
${datasetLicense}
51+
-->` +
52+
`<meta name="viewport" content="width=device-width, initial-scale=1">` +
53+
`<title>${Bun.escapeHTML(title)}</title>` +
54+
`<meta name="description" content="${Bun.escapeHTML(description)}">` +
55+
`<link rel="canonical" href="${Bun.escapeHTML(canonicalURL)}">` +
56+
`<meta property="og:title" content="${Bun.escapeHTML(title)}">` +
57+
`<meta property="og:description" content="${Bun.escapeHTML(description)}">` +
58+
`<meta property="og:url" content="${Bun.escapeHTML(canonicalURL)}">` +
59+
`<meta property="og:type" content="website">` +
60+
`<meta name="twitter:card" content="summary">` +
61+
`<meta name="twitter:title" content="${Bun.escapeHTML(title)}">` +
62+
`<meta name="twitter:description" content="${Bun.escapeHTML(description)}">` +
63+
`<style>${css}</style></head><body>${logo}` +
64+
`<header class="message"><h1>Read Hacker News</h1>` +
65+
`<p>Explore Hacker News readers, apps, RSS feeds, newsletters, and command-line clients.</p></header>` +
66+
`<main class="columns" aria-label="Hacker News directory">${groups}</main>` +
67+
`<footer>Created by <a class="link" target="_blank" href="https://rukin.me">Phil Rukin</a> <br>` +
68+
`<a class="link" target="_blank" href="https://github.com/phil-r/awesome-hacker-news">Contribute directory entries</a> · ` +
69+
`<a class="link" href="https://github.com/phil-r/hackernewsbot">Website source</a>` +
70+
`<!-- GitHub icon: Font Awesome (https://fontawesome.com/), CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/); SVG CSS and ARIA attributes modified. -->` +
71+
`</footer></body></html>`;
72+
}

‎app/seo.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Build-only origin, deliberately independent of the bot's BASE_URL and .env loader.
2+
export function siteCanonicalURL(origin = "https://readhacker.news"): string {
3+
const url = new URL(origin);
4+
if (!["http:", "https:"].includes(url.protocol) || url.username || url.password ||
5+
url.pathname !== "/" || url.search || url.hash || /[?#]/.test(origin)) {
6+
throw new Error("SITE_ORIGIN must be an HTTP(S) origin without credentials, path, query or fragment");
7+
}
8+
return url.href;
9+
}
10+
export const title = "Hacker News Readers & Apps | Read Hacker News";
11+
export const description = "Find Hacker News readers for web and iOS, plus RSS feeds, email newsletters, Telegram channels, and command-line clients.";

‎app/styles/columns.css‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.columns {
2+
column-count: 4;
3+
column-gap: 0;
4+
}
5+
6+
.columns-panel {
7+
break-inside: avoid;
8+
padding: 5px;
9+
}
10+
11+
.columns-panel-content {
12+
padding: 10px;
13+
background-color: rgb(249, 249, 249);
14+
}
15+
16+
@media (max-width: 1500px) {
17+
.columns {
18+
column-count: 3;
19+
}
20+
}
21+
22+
@media (max-width: 1023px) {
23+
.columns {
24+
column-count: 2;
25+
}
26+
}
27+
28+
@media (max-width: 500px) {
29+
.columns {
30+
column-count: 1;
31+
}
32+
}

0 commit comments

Comments
 (0)