You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/plugins/html.md
+90-95Lines changed: 90 additions & 95 deletions
Original file line number
Diff line number
Diff line change
@@ -35,16 +35,57 @@ new Elysia()
35
35
.get(
36
36
'/html',
37
37
() =>`
38
-
<html lang="en">
38
+
<html lang='en'>
39
+
<head>
40
+
<title>Hello World</title>
41
+
</head>
42
+
<body>
43
+
<h1>Hello World</h1>
44
+
</body>
45
+
</html>`
46
+
)
47
+
.get('/jsx', () => (
48
+
<htmllang='en'>
39
49
<head>
40
50
<title>Hello World</title>
41
51
</head>
42
52
<body>
43
53
<h1>Hello World</h1>
44
54
</body>
45
-
</html> `
46
-
)
47
-
.get('/jsx', () => (
55
+
</html>
56
+
))
57
+
.listen(3000)
58
+
```
59
+
60
+
This plugin will automatically add `Content-Type: text/html; charset=utf8` header to the response, add `<!doctype html>` and convert it into a Response object.
61
+
62
+
## JSX
63
+
Elysia HTML is based on [@kitajs/html](https://github.com/kitajs/html) allowing us to define JSX to string in compile time to archive high performance.
64
+
65
+
Name your file that need to use JSX to ends with affix **"x"**:
66
+
- .js -> .jsx
67
+
- .ts -> .tsx
68
+
69
+
To register TypeScript type, please appends the following to **tsconfig.json**:
70
+
```jsonc
71
+
// tsconfig.json
72
+
{
73
+
"compilerOptions": {
74
+
"jsx":"react",
75
+
"jsxFactory":"Html.createElement",
76
+
"jsxFragmentFactory":"Html.Fragment"
77
+
}
78
+
}
79
+
```
80
+
81
+
That's it, now you can JSX as your template engine:
82
+
```tsx
83
+
import { Elysia } from'elysia'
84
+
import { html } from'@elysiajs/html'// [!code ++]
85
+
86
+
newElysia()
87
+
.use(html()) // [!code ++]
88
+
.get('/', () => (
48
89
<htmllang="en">
49
90
<head>
50
91
<title>Hello World</title>
@@ -54,10 +95,53 @@ new Elysia()
54
95
</body>
55
96
</html>
56
97
))
57
-
.listen(8080)
98
+
.listen(3000)
58
99
```
59
100
60
-
This plugin will automatically add `Content-Type: text/html; charset=utf8` header to the response, add `<!doctype html>` and convert it into a Response object.
101
+
## XSS
102
+
Elysia HTML is based use with Kita HTML plugin to detect possible XSS attack in compile time.
103
+
104
+
You can use a dedicated `safe` attribute to sanitize user value to prevent XSS vulnerability.
105
+
```tsx
106
+
import { Elysia, t } from'elysia'
107
+
import { html } from'@elysiajs/html'
108
+
109
+
newElysia()
110
+
.use(html())
111
+
.post('/', ({ body }) => (
112
+
<htmllang="en">
113
+
<head>
114
+
<title>Hello World</title>
115
+
</head>
116
+
<body>
117
+
<h1safe>{body}</h1>
118
+
</body>
119
+
</html>
120
+
), {
121
+
body: t.String()
122
+
})
123
+
.listen(3000)
124
+
```
125
+
126
+
However, when are building a large-scale app, it's best to have a type reminder to detect possible XSS vulnerability in your codebase.
127
+
128
+
To add a type-safe reminder, please install:
129
+
```sh
130
+
bun add @kitajs/ts-html-plugin
131
+
```
132
+
133
+
Then appends the following **tsconfig.json
134
+
```jsonc
135
+
// tsconfig.json
136
+
{
137
+
"compilerOptions": {
138
+
"jsx":"react",
139
+
"jsxFactory":"Html.createElement",
140
+
"jsxFragmentFactory":"Html.Fragment",
141
+
"plugins": [{ "name":"@kitajs/ts-html-plugin" }]
142
+
}
143
+
}
144
+
```
61
145
62
146
## Options
63
147
@@ -100,92 +184,3 @@ app.get('/', ({ html }) => html('<html></html>'))
100
184
The function used to detect if a string is a html or not. Default implementation if length is greater than 7, starts with `<` and ends with `>`.
101
185
102
186
Keep in mind there's no real way to validate HTML, so the default implementation is a best guess.
103
-
104
-
## Jsx
105
-
106
-
This plugin re-exports [@kitajs/html](https://github.com/kitajs/html), which is a JSX factory for creating HTML strings from JSX. **Please report JSX related issues to that repository.**
107
-
108
-
To use JSX, first rename your file extension to either `.tsx` or `.jsx`.
109
-
110
-
Then, install basic dependencies and add the following to your `tsconfig.json`:
111
-
112
-
```sh
113
-
bun install @kitajs/html @kitajs/ts-html-plugin
114
-
```
115
-
116
-
```jsonc
117
-
// tsconfig.json
118
-
119
-
{
120
-
"compilerOptions": {
121
-
"jsx":"react",
122
-
"jsxFactory":"Html.createElement",
123
-
"jsxFragmentFactory":"Html.Fragment",
124
-
"plugins": [{ "name":"@kitajs/ts-html-plugin" }]
125
-
}
126
-
}
127
-
```
128
-
129
-
Then, you can simply use the JSX syntax to create HTML strings:
130
-
131
-
```tsx
132
-
// app.tsx
133
-
134
-
import { Elysia } from'elysia'
135
-
import { html } from'@elysiajs/html'
136
-
137
-
newElysia()
138
-
.use(html())
139
-
.get('/', () => (
140
-
<htmllang="en">
141
-
<head>
142
-
<title>Hello World</title>
143
-
</head>
144
-
<body>
145
-
<h1>Hello World</h1>
146
-
</body>
147
-
</html>
148
-
))
149
-
.listen(8080)
150
-
```
151
-
152
-
and that's it! 🎉
153
-
154
-
You can now use JSX to define your web page and Elysia will turns them to HTML automatically.
155
-
156
-
::: warning
157
-
Learn how to [sanitize](https://github.com/kitajs/html#sanitization) and avoid xss vulnerabilities in your code!
158
-
:::
159
-
160
-
::: tip
161
-
162
-
new Elysia()
163
-
.get('/unsafe', ({ sanitize }) => (
164
-
<h1>{sanitize(malicious)}</h1>
165
-
))
166
-
.listen(8080)
167
-
```
168
-
169
-
Read more about JSX in the [official documentation](https://github.com/kitajs/html) and learn how to add HTMX typings, compiling html, adding more JSX components and so on...
170
-
171
-
:::
172
-
173
-
## Sanitization
174
-
175
-
To keep this section up to date, please refer to the [sanitization](https://github.com/kitajs/html/tree/master#sanitization) section of the `@kitajs/html` repository.
176
-
177
-
## Manual handler
178
-
179
-
We recommend relying on automatic responses, but you can optionally disable `autoDetect` and explicitly only use the `html` function.
0 commit comments