@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 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&quot;quote">I'm "going" to &lt;script>alert('hack')&lt;/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 &lt;b>generated&lt;/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 {};
@@ -0,0 +1,7 @@
1
+ export declare function elysiaJsx(): import("elysia").default<"", {
2
+ request: {};
3
+ store: {};
4
+ }, {
5
+ type: {};
6
+ error: {};
7
+ }, {}, {}, false>;
package/entityMap.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ declare const entityMap: Record<string, string>;
2
+ export default entityMap;
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
+ */