@mpen/jsxhtml 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +154 -0
- package/custom-components.d.ts +22 -0
- package/dev.d.ts +1 -0
- package/elysia-plugin.d.ts +7 -0
- package/entityMap.d.ts +2 -0
- package/escape.d.ts +15 -0
- package/index.cjs +1846 -0
- package/index.d.ts +11 -0
- package/index.mjs +1812 -0
- package/jsx-dev-runtime.d.ts +4 -0
- package/jsx-elements.d.ts +41 -0
- package/jsx-node.d.ts +4 -0
- package/jsx-runtime.d.ts +6 -0
- package/package.json +12 -0
- package/render.d.ts +2 -0
- package/styleObjectToString.d.ts +14 -0
- package/util.d.ts +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# JsxHtml
|
|
2
|
+
|
|
3
|
+
JSX to HTML. No hydration.
|
|
4
|
+
|
|
5
|
+
## Example
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
console.log(<div class={["foo","bar"]} style={{color:'blue',border:1}}>Hello JsxHtml</div>.toString())
|
|
9
|
+
// <div class="foo bar" style="color:blue;border:1px;">Hello JsxHtml</div>
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Intro
|
|
13
|
+
|
|
14
|
+
JsxHtml is a jsx-runtime that converts your compiled JSX into static HTML. That means no virtual DOM, no hydration, no excess markup, no client-side JavaScript needed.
|
|
15
|
+
|
|
16
|
+
Unlike [other libraries](https://github.com/kitajs/html#the-safe-attribute), JsxHtml is *safe by default*. That means all your variables, whether they're used in attribute values or content, will be escaped.
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
const userGeneratedContent = `I'm "going" to <script>alert('hack')</script> you!`
|
|
20
|
+
const breakQuote = `break"quote`
|
|
21
|
+
console.log(<div class={breakQuote}>{userGeneratedContent}</div>.toString())
|
|
22
|
+
// <div class="break"quote">I'm "going" to <script>alert('hack')</script> you!</div>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
JsxHtml returns `JsxNode` objects with a `.toString()` method instead of returning a string directly. This has the benefit that it allows us to know which bits are safe or unsafe. For example, we can rewrite the above snippet to use JSX in the variable instead of a string:
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
const serverContent = <>I'm "going" to <script>alert('hack')</script> myself!</>
|
|
29
|
+
console.log(<div>{serverContent}</div>.toString())
|
|
30
|
+
// <div>I'm "going" to <script>alert('hack')</script> myself!</div>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Notice how the output is *not* escaped now, because the `serverContent` is JSX and can't have been written by a user. There is no need to annotate which pieces are "safe" or "unsafe" which is prone to human error.
|
|
34
|
+
|
|
35
|
+
## Escape Hatch
|
|
36
|
+
|
|
37
|
+
If you really want to allow unescaped HTML to be rendered out as-is, you can use the `<RawHtml>` component:
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
const html = "HTML <b>generated</b> from some WYSIWYG."
|
|
41
|
+
console.log('SAFE: ' + <div>{html}</div>)
|
|
42
|
+
console.log('NOT SAFE: ' + <RawHtml>{html}</RawHtml>)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```txt
|
|
46
|
+
SAFE: <div>HTML <b>generated</b> from some WYSIWYG.</div>
|
|
47
|
+
NOT SAFE: HTML <b>generated</b> from some WYSIWYG.
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Setup
|
|
51
|
+
|
|
52
|
+
Add these options to your `tsconfig.json` or Babel config.
|
|
53
|
+
|
|
54
|
+
```json
|
|
55
|
+
{
|
|
56
|
+
"compilerOptions": {
|
|
57
|
+
"jsx": "react-jsx",
|
|
58
|
+
"jsxImportSource": "@mpen/jsxhtml"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Elysia Integration
|
|
64
|
+
|
|
65
|
+
JsxHtml is primarily designed for server-side rendering. It pairs nicely with frameworks such as [Elysia](https://elysiajs.com/), so you can return a block of HTML with no fuss:
|
|
66
|
+
|
|
67
|
+
```tsx
|
|
68
|
+
import {elysiaJsx} from '@mpen/jsxhtml'
|
|
69
|
+
|
|
70
|
+
new Elysia()
|
|
71
|
+
.use(elysiaJsx())
|
|
72
|
+
.get('/', () => {
|
|
73
|
+
return (
|
|
74
|
+
<HtmlDocument lang="en">
|
|
75
|
+
<head>
|
|
76
|
+
<title>Hello JsxHtml</title>
|
|
77
|
+
</head>
|
|
78
|
+
<body>
|
|
79
|
+
Hi there!
|
|
80
|
+
</body>
|
|
81
|
+
</HtmlDocument>
|
|
82
|
+
)
|
|
83
|
+
})
|
|
84
|
+
.listen(3000)
|
|
85
|
+
```
|
|
86
|
+
```txt
|
|
87
|
+
<!DOCTYPE html><html lang="en"><head><title>Hello JsxHtml</title></head><body>Hi there!</body></html>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
It should be just as easy to integrate with [Express](https://expressjs.com/) or any other JavaScript server, because JsxHtml compiles to an object with a `.toString()` method -- so if your framework allows you to send a string in the response body, you're good to go.
|
|
91
|
+
|
|
92
|
+
For reference, so you can see how easy this is, the entire Elysia plugin is this:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import {isJsxNode} from './jsx-nodes'
|
|
96
|
+
|
|
97
|
+
export function elysiaJsx() {
|
|
98
|
+
const {Elysia} = require('elysia') as typeof import('elysia')
|
|
99
|
+
return new Elysia()
|
|
100
|
+
.onAfterHandle(({response}) => {
|
|
101
|
+
if(isJsxNode(response)) {
|
|
102
|
+
return new Response(String(response), {
|
|
103
|
+
headers: {
|
|
104
|
+
'content-type': 'text/html; charset=utf8'
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
i.e., it's just checking if you're returning a JsxHtml node and then converts the return value to a string and adds the `Content-Type` header.
|
|
113
|
+
|
|
114
|
+
## Client-side
|
|
115
|
+
|
|
116
|
+
If you *really* want, you can ship the compiled JSX to the client. It looks like this:
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
_jsx("div", { children: "hello" });
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
But then you will need to send the `@mpen/jsxhtml/jsx-runtime` to the client too. But then you can render out the HTML in the browser, which will allow for more dynamic behavior, but you won't get reactive elements, hooks, or state management or anything of the sort. If you want that, try [React](https://react.dev/).
|
|
123
|
+
|
|
124
|
+
## jsx-dev-runtime
|
|
125
|
+
|
|
126
|
+
JsxHtml includes `jsx-dev-runtime`. There is no equivalent [React Dev Tools](https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi), because again, there is no client-side JS included here, it's just HTML, *but*, we can add some good old-fashioned HTML comments to the output, so you can see which bits of HTML were generated from custom components:
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
function BlueBox(props: CommonProps) {
|
|
130
|
+
return (
|
|
131
|
+
<div class="cr-blue-box">
|
|
132
|
+
{props.children}
|
|
133
|
+
</div>
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
new Elysia()
|
|
138
|
+
.use(elysiaJsx())
|
|
139
|
+
.get('/dev', () => {
|
|
140
|
+
return <BlueBox>box</BlueBox>
|
|
141
|
+
})
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
When using `{"jsx": "react-jsxdev"}`, this will output:
|
|
145
|
+
|
|
146
|
+
```html
|
|
147
|
+
<!--<BlueBox>--><div class="cr-blue-box">box</div><!--</BlueBox>-->
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
When using `{"jsx": "react-jsx"}`, this will output:
|
|
151
|
+
|
|
152
|
+
```html
|
|
153
|
+
<div class="cr-blue-box">box</div>
|
|
154
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DocTypeProps, JsxComment, JsxDocType, JsxRawHtml } from './jsx-elements';
|
|
2
|
+
import { CommonProps, StringChildren } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Unescaped HTML.
|
|
5
|
+
*/
|
|
6
|
+
export declare function RawHtml({ children }: StringChildren): JsxRawHtml;
|
|
7
|
+
/**
|
|
8
|
+
* An HTML `<!-- comment -->`
|
|
9
|
+
*/
|
|
10
|
+
export declare function Comment({ children }: StringChildren): JsxComment;
|
|
11
|
+
/**
|
|
12
|
+
* The `<!DOCTYPE>` node.
|
|
13
|
+
*/
|
|
14
|
+
export declare function DocType(props: DocTypeProps): JsxDocType;
|
|
15
|
+
/**
|
|
16
|
+
* `<!DOCTYPE html><html ...>{children}</html>`
|
|
17
|
+
*/
|
|
18
|
+
export declare function HtmlDocument({ children, ...htmlAttrs }: CommonProps): any;
|
|
19
|
+
/**
|
|
20
|
+
* No output.
|
|
21
|
+
*/
|
|
22
|
+
export declare function Empty(): import("./jsx-elements").JsxEmpty;
|
package/dev.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/entityMap.d.ts
ADDED
package/escape.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Attributes, AttributeValue, Stringable } from './types';
|
|
2
|
+
export declare function tagName(string: Stringable): string;
|
|
3
|
+
export declare function attrName(string: Stringable): string;
|
|
4
|
+
export declare function attrValue(value: Stringable): string;
|
|
5
|
+
export declare function attrKvPair(rawAttr: string, rawVal: AttributeValue): string | null;
|
|
6
|
+
export declare function attrs(attributes: Attributes): string;
|
|
7
|
+
export declare function htmlContent(string: Stringable): string;
|
|
8
|
+
export declare function htmlComment(string: Stringable): string;
|
|
9
|
+
export declare function escapeScript(string: Stringable): string;
|
|
10
|
+
/**
|
|
11
|
+
* Generic HTML escape. Works for both attribute values and HTML content.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} str Text
|
|
14
|
+
* @returns {string} Escaped HTML
|
|
15
|
+
*/
|