-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository
/
Copy pathindex.tsx
More file actions
More file actions
66 lines (49 loc) · 1.4 KB
/
index.tsx
File metadata and controls
66 lines (49 loc) · 1.4 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
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
/**
* Data - A web service that provides various categories of data
*/
import { Hono } from 'hono';
import { jsxRenderer } from 'hono/jsx-renderer';
// Import JSX components
import { Home } from './components/Home';
import { Data } from './components/Data';
// Import functions that handle each category of data
import { ROUTES } from './routes';
const SITE_URL = new URL('https://data.wikinder.org/');
const app = new Hono();
app.get('*', jsxRenderer());
// Home page
app.get('/', (context) => {
const slugs = Object.keys(ROUTES);
const categories = Object.fromEntries(
slugs.map((slug) => [`${slug}/`, capitalize(slug)])
);
return context.render(
<Home categories={categories} canonicalUrl={SITE_URL} />
);
});
// Data page
app.get('/:category{[a-z]+}/:input', handler);
app.get('/:category{[a-z]+}/*', handler);
function handler(context) {
const { category, input } = context.req.param();
// Get the output
const output = ROUTES?.[category](input);
if (!output) {
return context.notFound();
}
let path = context.req.path;
if (input == null && !path.endsWith('/')) {
path += '/';
}
return context.render(
<Data
category={output.category ?? capitalize(category)}
canonicalUrl={new URL(path, SITE_URL)}
{...output}
/>
);
}
function capitalize(str: string) {
return str.replace(/^./, (first) => first.toUpperCase());
}
export default app;