Skip to content

Commit 4c2b717

Browse files
authored
Merge pull request #13 from arthurfiorette/main
Draft: Improve jsx integration, add `autoDetect`, `autoDoctype`, `contentType` and `isHtml`.
2 parents e4f8783 + 5308b18 commit 4c2b717

17 files changed

+546
-708
lines changed

.eslintrc.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
module.exports = {
2-
"env": {
3-
"es2021": true,
4-
"node": true
5-
},
6-
"extends": [
7-
"eslint:recommended",
8-
"plugin:@typescript-eslint/recommended"
9-
],
10-
"parser": "@typescript-eslint/parser",
11-
"parserOptions": {
12-
"ecmaVersion": "latest",
13-
"sourceType": "module"
14-
},
15-
"plugins": [
16-
"@typescript-eslint"
17-
],
18-
"rules": {
19-
"@typescript-eslint/ban-types": 'off',
20-
'@typescript-eslint/no-explicit-any': 'off'
21-
},
22-
"ignorePatterns": ["example/*", "tests/**/*"]
2+
env: {
3+
es2021: true,
4+
node: true
5+
},
6+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
7+
parser: '@typescript-eslint/parser',
8+
parserOptions: {
9+
ecmaVersion: 'latest',
10+
sourceType: 'module'
11+
},
12+
plugins: ['@typescript-eslint'],
13+
rules: {
14+
'@typescript-eslint/ban-types': 'off',
15+
'@typescript-eslint/no-explicit-any': 'off'
16+
},
17+
ignorePatterns: ['example/*', 'tests/**/*']
2318
}

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"useTabs": true,
3+
"tabWidth": 4,
4+
"semi": false,
5+
"singleQuote": true,
6+
"trailingComma": "none"
7+
}

README.md

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
11
# @elysiajs/html
2-
Plugin for [elysia](https://github.com/elysiajs/elysia) that add support for returning html.
3-
4-
## Installation
5-
```bash
6-
bun add @elysiajs/html
7-
```
8-
9-
## Example
10-
```typescript
11-
import { Elysia } from 'elysia'
12-
import { html } from '@elysiajs/html'
132

14-
const page = `<!DOCTYPE HTML>
15-
<html lang="en">
16-
<head>
17-
<title>Hello World</title>
18-
</head>
19-
<body>
20-
<h1>Hello World</h1>
21-
</body>
22-
</html>`
23-
24-
const app = new Elysia()
25-
.use(html())
26-
.get('/', () => page)
27-
.get('/html', ({ html }) => html(page))
28-
.listen(8080)
29-
```
3+
Plugin for [elysia](https://github.com/elysiajs/elysia) that add support for returning html.
304

31-
## API
32-
This plugin detects HTML string and adds `html` method to `Context`.
5+
<br />
336

34-
If your response is start with `<!DOCTYPE` (case insensitive), `Content-Type` will be set to `text/html`.
7+
<h3 align=center>
8+
Documentation at <a href="https://elysiajs.com/plugins/html.html">elysiajs.com/plugins/html</a>
9+
</h3>
3510

36-
Or if you want to manually return HTML, simply use the newly added `html` function like this:
37-
```typescript
38-
app.get('/html', ({ html }) => html(page))
39-
```
11+
<br />

bun.lockb

5.48 KB
Binary file not shown.

example/index.tsx

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
1-
import { Elysia } from 'elysia'
2-
import { html, Fragment } from '../src/index'
1+
import { Elysia, t } from 'elysia'
2+
import { html } from '../src'
33

4-
const page = `<html lang="en">
5-
<head>
6-
<title>Hello World</title>
7-
</head>
8-
<body>
9-
<h1>Hello World</h1>
10-
</body>
11-
</html>`
4+
function page({ name }: { name: string }): string {
5+
return `
6+
<html lang="en">
7+
<head>
8+
<title>Hello ${name}!</title>
9+
</head>
10+
<body>
11+
<h1>Hello ${name}!</h1>
12+
</body>
13+
</html>
14+
`
15+
}
1216

13-
const jsx = (
14-
<html lang="en">
15-
<head hx-a="A">
16-
<title>Hello World</title>
17-
</head>
18-
<body>
19-
<h1>Hello World</h1>
20-
</body>
21-
</html>
22-
)
17+
// https://elysiajs.com/plugins/html.html#jsx
18+
function tsxPage({ name }: { name: string }): string {
19+
return (
20+
<html lang="en">
21+
<head>
22+
<title>Hello ${name}!</title>
23+
</head>
24+
<body>
25+
<h1>Hello ${name}!</h1>
26+
</body>
27+
</html>
28+
)
29+
}
2330

24-
const app = new Elysia()
25-
.use(html())
26-
.get('/', () => page)
27-
.get('/jsx', () => jsx)
28-
.get('/html', ({ html }) => html(page))
29-
.get('/a', () => (
30-
<>
31-
<h1>Hello World</h1>
32-
</>
33-
))
34-
.listen(8080)
31+
export function createApp() {
32+
// https://xelysiajs.com/concept/schema.html
33+
const indexSchema = {
34+
params: t.Object({
35+
name: t.String({ default: 'World' })
36+
})
37+
}
38+
39+
return (
40+
new Elysia()
41+
// https://elysiajs.com/plugins/html.html#options
42+
.use(html())
43+
.get('/', ({ params }) => page(params), indexSchema)
44+
.get('/tsx', ({ params }) => tsxPage(params), indexSchema)
45+
.listen(8080)
46+
)
47+
}

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@
3232
"test": "bun test && npm run test:node",
3333
"test:node": "npm install --prefix ./test/node/cjs/ && npm install --prefix ./test/node/esm/ && node ./test/node/cjs/index.js && node ./test/node/esm/index.js",
3434
"build": "rimraf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json",
35-
"release": "npm run build && npm run test && npm publish --access public"
35+
"release": "npm run build && npm run test && npm publish --access public",
36+
"format": "prettier --write ."
3637
},
3738
"peerDependencies": {
38-
"elysia": ">= 0.6.0"
39+
"elysia": ">= 0.6.23"
3940
},
4041
"devDependencies": {
4142
"@types/dompurify": "^3.0.2",
42-
"@types/node": "^20.5.9",
43-
"bun-types": "^0.8.1",
44-
"elysia": "0.6.19",
45-
"eslint": "^8.48.0",
46-
"rimraf": "4.3.1",
43+
"@types/node": "^20.6.0",
44+
"bun-types": "^1.0.1",
45+
"elysia": "^0.6.23",
46+
"eslint": "^8.49.0",
47+
"rimraf": "^5.0.1",
4748
"typescript": "^5.2.2"
4849
},
4950
"dependencies": {
50-
"@kitajs/html": "^1.4.6",
51-
"dompurify": "^3.0.5"
51+
"@kitajs/html": "^2.2.1"
5252
}
5353
}

0 commit comments

Comments
 (0)