-
Notifications
You must be signed in to change notification settings - Fork 4
/
mod.tsx
137 lines (126 loc) · 3.33 KB
/
mod.tsx
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/** @jsx h */
import { h, type VNode } from "https://esm.sh/preact@10.11.2";
import {
renderToString,
} from "https://esm.sh/preact-render-to-string@5.2.6?deps=preact@10.11.2";
const plugins: Plugin[] = [];
export interface HtmlOptions {
lang?: string;
classes?: {
html?: string[];
body?: string[];
};
title?: string;
meta?: Record<string, string | null | undefined>;
links?: { [key: string]: string; href: string; rel: string }[];
styles?: (string | { href: string; id?: string })[];
scripts?: (string | {
src: string;
type?: string;
id?: string;
async?: boolean;
defer?: boolean;
})[];
}
export interface PluginContext extends HtmlOptions {
body: string;
status: number;
headers: Headers;
}
export interface Plugin {
(props: PluginContext): void | Promise<void>;
}
export interface Options extends HtmlOptions {
body: VNode;
status?: number;
headers?: HeadersInit;
}
export async function html(options: Options): Promise<Response> {
const { body, status = 200, headers: headersInit, ...rest } = options;
const bodyHtml = renderToString(body);
const headers = new Headers(headersInit);
headers.append("Content-Type", "text/html; charset=utf-8");
const context: PluginContext = {
...rest,
body: bodyHtml,
status,
headers,
};
for (const plugin of plugins) {
await plugin(context);
}
return new Response(
`<!DOCTYPE html>` +
renderToString(
<Html {...context} />,
),
{
status: context.status,
headers: context.headers,
},
);
}
interface HtmlProps extends HtmlOptions {
body: string;
}
function Html({
lang,
classes,
title,
meta,
links,
styles,
scripts,
body,
}: HtmlProps) {
return (
<html
lang={lang ?? "en"}
class={classes?.html?.join(" ") || undefined}
>
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
{title && <title>{title}</title>}
{meta &&
Object.entries(meta).filter(([name, content]) => !!name && !!content)
.map(([name, content]) => (
name.startsWith("og:")
? <meta property={name} content={String(content)} />
: <meta name={name} content={String(content)} />
))}
{links &&
links.map(({ rel, href, ...rest }) => (
<link rel={rel} href={href} {...rest} />
))}
{styles && styles.map((style) => (
typeof style === "string"
? <style dangerouslySetInnerHTML={{ __html: style }} />
: <link rel="stylesheet" href={style.href} id={style.id} />
))}
{scripts && scripts.map((script) => (
typeof script === "string"
? <script dangerouslySetInnerHTML={{ __html: script }} />
: (
<script
src={script.src}
type={script.type}
id={script.id}
async={script.async}
defer={script.defer}
>
</script>
)
))}
</head>
<body
class={classes?.body?.join(" ") || undefined}
dangerouslySetInnerHTML={{ __html: body }}
/>
</html>
);
}
html.use = (...plugin: Plugin[]) => {
plugins.push(...plugin);
};
export * from "https://esm.sh/preact@10.11.2";