Skip to content

Commit 4b49460

Browse files
authored
Switch human output to Hono JSX (#171)
1 parent 72ef5b9 commit 4b49460

File tree

6 files changed

+73
-17
lines changed

6 files changed

+73
-17
lines changed

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ export default defineConfig(
3131
},
3232
{
3333
name: 'custom/ignores',
34-
ignores: ['dist-worker/**', 'types/worker.d.ts'],
34+
ignores: ['dist-worker/**', '.wrangler/**', 'types/worker.d.ts'],
3535
},
3636
);

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as Sentry from '@sentry/cloudflare';
22
import { env } from 'cloudflare:workers';
33
import { Hono } from 'hono';
44
import { cors } from 'hono/cors';
5+
import { jsxRenderer } from 'hono/jsx-renderer';
56
import { logger } from 'hono/logger';
67

78
import errorRoutes from './routes/errors.ts';
@@ -11,11 +12,13 @@ import libraryRoutes from './routes/library.ts';
1112
import statsRoutes from './routes/stats.ts';
1213
import whitelistRoutes from './routes/whitelist.ts';
1314
import corsOptions from './utils/cors.ts';
15+
import layout from './utils/jsx/layout.tsx';
1416

1517
// Create the base app
1618
const app = new Hono();
1719
if (!env.DISABLE_LOGGING) app.use('*', logger());
1820
app.use('*', cors(corsOptions));
21+
app.use('*', jsxRenderer(layout));
1922

2023
// Load the routes
2124
indexRoutes(app);

src/utils/jsx/json.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Standard cdnjs HTML layout for pretty-printing JSON data.
3+
*
4+
* @param props Component props.
5+
* @param props.json Data to be pretty-printed on the page.
6+
*/
7+
export default ({ json }: { json: unknown }) => (
8+
<>
9+
<link
10+
rel="stylesheet"
11+
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/default.min.css"
12+
integrity="sha512-hasIneQUHlh06VNBe7f6ZcHmeRTLIaQWFd43YriJ0UND19bvYRauxthDg8E4eVNPm9bRUhr5JGeqH7FRFXQu5g=="
13+
crossorigin="anonymous"
14+
referrerpolicy="no-referrer"
15+
/>
16+
17+
<pre>
18+
<code class="language-json">{JSON.stringify(json, null, 2)}</code>
19+
</pre>
20+
21+
<script
22+
src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"
23+
integrity="sha512-EBLzUL8XLl+va/zAsmXwS7Z2B1F9HUHkZwyS/VKwh3S7T/U0nF4BaU29EP/ZSf6zgiIxYAnKLu6bJ8dqpmX5uw=="
24+
crossorigin="anonymous"
25+
referrerpolicy="no-referrer"
26+
/>
27+
<script
28+
src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/languages/json.min.js"
29+
integrity="sha512-f2/ljYb/tG4fTHu6672tyNdoyhTIpt4N1bGrBE8ZjwIgrjDCd+rljLpWCZ2Vym9PBWQy2Tl9O22Pp2rMOMvH4g=="
30+
crossorigin="anonymous"
31+
referrerpolicy="no-referrer"
32+
/>
33+
<script
34+
defer
35+
dangerouslySetInnerHTML={{
36+
__html: 'hljs.highlightAll();',
37+
}}
38+
/>
39+
</>
40+
);

src/utils/jsx/layout.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Child } from 'hono/jsx';
2+
3+
/**
4+
* Standard cdnjs HTML layout.
5+
*
6+
* @param props Component props.
7+
* @param props.children Content to be included in the body of the page.
8+
*/
9+
export default ({ children }: { children?: Child }) => (
10+
<html>
11+
<head>
12+
<meta name="robots" content="noindex" />
13+
</head>
14+
<body>
15+
{children}
16+
<script
17+
defer
18+
dangerouslySetInnerHTML={{
19+
__html: 'console.log("%cThanks for using cdnjs! 😊", "font-size: 5em; font-family: ui-sans-serif, system-ui, sans-serif; color: #e95420;");',
20+
}}
21+
/>
22+
</body>
23+
</html>
24+
);

src/utils/respond.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Context } from 'hono';
22

33
import event from './event.ts';
4+
import Json from './jsx/json.tsx';
45

56
/**
67
* Generate an HTML response with pretty-printed data.
@@ -11,22 +12,8 @@ import event from './event.ts';
1112
const human = (ctx: Context, data: unknown) => {
1213
event('human-output', ctx);
1314

14-
ctx.header('Content-Type', 'text/html');
1515
ctx.header('X-Robots-Tag', 'noindex');
16-
return ctx.html(
17-
'<!doctype><html>' +
18-
'<head><meta name="robots" content="noindex"/><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css" integrity="sha256-Zd1icfZ72UBmsId/mUcagrmN7IN5Qkrvh75ICHIQVTk=" crossorigin="anonymous"/></head><body>' +
19-
'<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js" integrity="sha256-/BfiIkHlHoVihZdc6TFuj7MmJ0TWcWsMXkeDFwhi0zw=" crossorigin="anonymous"></script>' +
20-
'<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/json.min.js" integrity="sha256-KPdGtw3AdDen/v6+9ue/V3m+9C2lpNiuirroLsHrJZM=" crossorigin="anonymous" defer></script>' +
21-
'<script src="https://cdnjs.cloudflare.com/ajax/libs/json2/20160511/json2.min.js" integrity="sha256-Fsw5X9ZUnlJb302irkG8pKCRwerGfxSArAw22uG/QkQ=" crossorigin="anonymous"></script>' +
22-
'<script defer>hljs.initHighlightingOnLoad();</script>' +
23-
'<script defer>var output=' +
24-
JSON.stringify(data) +
25-
'; ' +
26-
'document.write("<pre><code class=\'json\'>" + JSON.stringify(output,null,2) + "</code></pre>");</script>' +
27-
'<script defer>console.log("%cThanks for using cdnjs! 😊", "font: 5em roboto; color: #e95420;");</script>' +
28-
'</body></html>',
29-
);
16+
return ctx.render(Json({ json: data }));
3017
};
3118

3219
/**

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
"erasableSyntaxOnly": true,
1010
"verbatimModuleSyntax": true,
1111
"esModuleInterop": true,
12+
"jsx": "react-jsx",
13+
"jsxImportSource": "hono/jsx",
1214
"noEmit": true,
1315
"strict": true,
1416
"skipLibCheck": true,
1517
"types": ["@cloudflare/vitest-pool-workers"]
1618
},
17-
"include": ["**/*.ts", "**/*.js"],
19+
"include": ["**/*.ts", "**/*.tsx", "**/*.js"],
1820
"exclude": ["node_modules", "dist-worker"]
1921
}

0 commit comments

Comments
 (0)