@onivoro/server-html 24.30.12 → 24.30.13
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 +160 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
# @onivoro/server-html
|
|
2
2
|
|
|
3
|
-
A TypeScript library for server-side HTML generation with a functional approach.
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- **Functional HTML Generation**: Create HTML elements using functional composition
|
|
8
|
-
- **Type-Safe**: Full TypeScript support with proper type definitions
|
|
9
|
-
- **Server-Side Focused**: Optimized for server-side rendering scenarios
|
|
3
|
+
A TypeScript library for server-side HTML generation with a functional approach. Provides a simple, type-safe way to generate HTML strings on the server without any compilation or JSX.
|
|
10
4
|
|
|
11
5
|
## Installation
|
|
12
6
|
|
|
@@ -14,27 +8,173 @@ A TypeScript library for server-side HTML generation with a functional approach.
|
|
|
14
8
|
npm install @onivoro/server-html
|
|
15
9
|
```
|
|
16
10
|
|
|
17
|
-
##
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
This library provides functional HTML element creators that generate HTML strings. Each element is a function that takes content and optional attributes.
|
|
14
|
+
|
|
15
|
+
## Core API
|
|
18
16
|
|
|
19
|
-
|
|
17
|
+
### Element Functions
|
|
18
|
+
|
|
19
|
+
The library exports element functions for common HTML tags:
|
|
20
20
|
|
|
21
21
|
```typescript
|
|
22
|
-
import {
|
|
22
|
+
import { div, h1, p, button, anchor } from '@onivoro/server-html';
|
|
23
|
+
|
|
24
|
+
// Basic usage
|
|
25
|
+
const html = div(['Hello World']);
|
|
26
|
+
// Output: <div>Hello World</div>
|
|
23
27
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
$h1({ textContent: 'Server-Side Rendering', style: { fontWeight: 700 }}),
|
|
29
|
-
$p({ textContent: 'This markup was generated on the server.' })
|
|
30
|
-
]
|
|
28
|
+
// With attributes
|
|
29
|
+
const heading = h1(['Welcome'], {
|
|
30
|
+
id: 'main-title',
|
|
31
|
+
style: { 'font-size': '2rem', color: 'blue' }
|
|
31
32
|
});
|
|
33
|
+
// Output: <h1 id="main-title" style="font-size: 2rem; color: blue;">Welcome</h1>
|
|
34
|
+
|
|
35
|
+
// Nested elements
|
|
36
|
+
const card = div([
|
|
37
|
+
h2(['Card Title']),
|
|
38
|
+
p(['This is the card content']),
|
|
39
|
+
button(['Click me'], { onclick: 'handleClick()' })
|
|
40
|
+
]);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Available Elements
|
|
44
|
+
|
|
45
|
+
Regular elements:
|
|
46
|
+
- `anchor`, `body`, `button`, `div`
|
|
47
|
+
- `h1`, `h2`, `h3`, `h4`, `h5`, `h6`
|
|
48
|
+
- `head`, `header`, `htm` (html), `main`
|
|
49
|
+
- `p`, `pre`, `style`
|
|
50
|
+
- `tab` (table), `tbody`, `td`, `th`, `thead`, `tr`
|
|
51
|
+
|
|
52
|
+
Self-closing elements:
|
|
53
|
+
- `img`
|
|
54
|
+
|
|
55
|
+
### Special Styling
|
|
56
|
+
|
|
57
|
+
The `anchor` and `button` elements come with default button styles that can be overridden:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const styledButton = button(['Submit'], {
|
|
61
|
+
style: {
|
|
62
|
+
// Default button styles are applied automatically
|
|
63
|
+
// You can override them here
|
|
64
|
+
'background-color': 'green'
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Utility Functions
|
|
70
|
+
|
|
71
|
+
### Table Generator
|
|
72
|
+
|
|
73
|
+
Create HTML tables easily:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { table } from '@onivoro/server-html';
|
|
77
|
+
|
|
78
|
+
const columns = ['Name', 'Age', 'City'];
|
|
79
|
+
const rows = [
|
|
80
|
+
['John', '30', 'New York'],
|
|
81
|
+
['Jane', '25', 'London'],
|
|
82
|
+
['Bob', '35', 'Paris']
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
const htmlTable = table(columns, rows);
|
|
86
|
+
// Generates a complete table with alternating row colors
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Email Body Generator
|
|
90
|
+
|
|
91
|
+
Create formatted email bodies with consistent styling:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { emailBody } from '@onivoro/server-html';
|
|
95
|
+
|
|
96
|
+
const html = emailBody(
|
|
97
|
+
'Welcome!', // title
|
|
98
|
+
'Thanks for signing up', // subtitle
|
|
99
|
+
[ // content
|
|
100
|
+
p(['Your account is ready']),
|
|
101
|
+
button(['Get Started'], { href: 'https://example.com' })
|
|
102
|
+
],
|
|
103
|
+
'https://example.com/logo.png', // optional logo URL
|
|
104
|
+
{ 'font-family': 'Arial' } // optional extra styles
|
|
105
|
+
);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Low-Level Functions
|
|
109
|
+
|
|
110
|
+
For more control, use the low-level element creation functions:
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import { element, selfClosingElement } from '@onivoro/server-html';
|
|
114
|
+
|
|
115
|
+
// Create custom elements
|
|
116
|
+
const section = element('section', ['Content'], { class: 'my-section' });
|
|
117
|
+
const br = selfClosingElement('br', {});
|
|
32
118
|
```
|
|
33
119
|
|
|
34
|
-
##
|
|
120
|
+
## Deprecated API
|
|
121
|
+
|
|
122
|
+
The library includes deprecated functions prefixed with underscore (`_div`, `_h1`, etc.) that use the older `tag` and `selfClosingTag` functions. These are maintained for backward compatibility but should not be used in new code.
|
|
123
|
+
|
|
124
|
+
## Type Safety
|
|
125
|
+
|
|
126
|
+
All functions are fully typed with TypeScript:
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { TAttributes, TElementRenderer } from '@onivoro/server-html';
|
|
35
130
|
|
|
36
|
-
|
|
131
|
+
// Attributes type for element properties
|
|
132
|
+
const attrs: TAttributes = {
|
|
133
|
+
id: 'my-id',
|
|
134
|
+
className: 'my-class',
|
|
135
|
+
style: {
|
|
136
|
+
display: 'flex',
|
|
137
|
+
'align-items': 'center'
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// Element renderers return strings
|
|
142
|
+
const myDiv: string = div(['Content'], attrs);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Examples
|
|
146
|
+
|
|
147
|
+
### Complete HTML Document
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { htm, head, body, h1, div, p } from '@onivoro/server-html';
|
|
151
|
+
|
|
152
|
+
const document = htm([
|
|
153
|
+
head([
|
|
154
|
+
'<meta charset="UTF-8">',
|
|
155
|
+
'<title>My Page</title>'
|
|
156
|
+
]),
|
|
157
|
+
body([
|
|
158
|
+
h1(['Welcome to My Site']),
|
|
159
|
+
div([
|
|
160
|
+
p(['This is a paragraph']),
|
|
161
|
+
p(['Another paragraph'])
|
|
162
|
+
], { class: 'content' })
|
|
163
|
+
])
|
|
164
|
+
]);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Dynamic Content
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
const items = ['Apple', 'Banana', 'Orange'];
|
|
171
|
+
|
|
172
|
+
const list = div([
|
|
173
|
+
h2(['Fruits']),
|
|
174
|
+
...items.map(item => div([item], { class: 'list-item' }))
|
|
175
|
+
]);
|
|
176
|
+
```
|
|
37
177
|
|
|
38
178
|
## License
|
|
39
179
|
|
|
40
|
-
|
|
180
|
+
MIT
|