Skip to content

Commit 4608b6d

Browse files
committed
🎉 feat: html plugin with jsx
1 parent 4c38e9c commit 4608b6d

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

docs/plugins/html.md

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ head:
1515
---
1616

1717
# HTML Plugin
18-
This plugin adds shortcut for returning HTML
18+
Return [**JSX**](#jsx), and HTML with proper HTML headers automatically
1919

2020
Install with:
2121
```bash
@@ -27,8 +27,7 @@ Then use it:
2727
import { Elysia } from 'elysia'
2828
import { html } from '@elysiajs/html'
2929

30-
const page = `<!DOCTYPE HTML>
31-
<html lang="en">
30+
const page = `<html lang="en">
3231
<head>
3332
<title>Hello World</title>
3433
</head>
@@ -40,11 +39,52 @@ const page = `<!DOCTYPE HTML>
4039
new Elysia()
4140
.use(html())
4241
.get('/', () => page)
43-
.get('/html', ({ html }) => html(page))
4442
.listen(8080)
4543
```
4644

47-
This plugin detects if the string is started with `<!DOCTYPE HTML>`, it will add `Content-Type: text/html; charset=utf8` to the response header
45+
If any html tag is returned, response will be treat as HTML response automatically
46+
47+
## JSX
48+
Starting from HTML plugin > 0.6.1, you can directly use JSX to create, and return HTML automatically.
49+
50+
### Setup
51+
To utilize JSX, modify the tsconfig.json as the following:
52+
```json
53+
{
54+
"compilerOptions": {
55+
"jsx": "react",
56+
"jsxImportSource": "ElysiaJSX"
57+
}
58+
}
59+
```
60+
61+
and that's it! 🎉
62+
63+
You can now use JSX to define your web page and Elysia will turns them to HTML automatically.
64+
65+
```tsx
66+
// app.tsx
67+
import { Elysia } from 'elysia'
68+
import { html } from '@elysiajs/html'
69+
70+
new Elysia()
71+
.use(html())
72+
.get('/', () => (
73+
<html lang="en">
74+
<head>
75+
<title>Hello World</title>
76+
</head>
77+
<body>
78+
<h1>Hello World</h1>
79+
</body>
80+
</html>
81+
))
82+
.listen(8080)
83+
```
84+
85+
::: tip
86+
To use JSX, don't forget to rename your file extension to either `.tsx` or `.jsx`
87+
:::
4888

4989
## Handler
5090
Below are the value added to the handler.
@@ -56,3 +96,25 @@ Type:
5696
```typescript
5797
html(value: string) => Response
5898
```
99+
100+
#### Example:
101+
Although we recommended relying on automatic response, but you can optionally you can return explictly return any string with HTML header.
102+
103+
```typescript
104+
import { Elysia } from 'elysia'
105+
import { html } from '@elysiajs/html'
106+
107+
const page = `<html lang="en">
108+
<head>
109+
<title>Hello World</title>
110+
</head>
111+
<body>
112+
<h1>Hello World</h1>
113+
</body>
114+
</html>`
115+
116+
new Elysia()
117+
.use(html())
118+
.get('/', ({ html }) => html(page))
119+
.listen(8080)
120+
```

0 commit comments

Comments
 (0)