xiangjsoncraft 1.0.0 → 1.0.1
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 +315 -0
- package/package.json +1 -1
package/Readme.md
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
### XiangJsonCraft - 轻量级JSON配置与模板渲染工具
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
# XiangJsonCraft
|
|
6
|
+
|
|
7
|
+
XiangJsonCraft是一个简单而强大的JSON配置与HTML模板渲染工具,专为前端开发者设计。通过简洁的API,它允许你使用JSON配置文件来定义样式和内容,并将其应用到HTML模板中,实现动态页面的快速构建。
|
|
8
|
+
|
|
9
|
+
## 特性
|
|
10
|
+
|
|
11
|
+
- **简单易用**:只需几行代码即可完成模板渲染
|
|
12
|
+
- **灵活配置**:通过JSON文件定义样式和内容
|
|
13
|
+
- **轻量级**:无额外依赖,体积小巧
|
|
14
|
+
- **高效渲染**:支持批量处理和动态更新
|
|
15
|
+
- **类型安全**:提供完整的TypeScript类型定义
|
|
16
|
+
|
|
17
|
+
## 安装
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install xiangjsoncraft
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 快速开始
|
|
24
|
+
|
|
25
|
+
### 1. 创建JSON配置文件
|
|
26
|
+
|
|
27
|
+
首先,创建一个JSON配置文件,定义页面的样式和内容:
|
|
28
|
+
|
|
29
|
+
```json
|
|
30
|
+
// config.json
|
|
31
|
+
{
|
|
32
|
+
"styles": {
|
|
33
|
+
"body": {
|
|
34
|
+
"margin": "0",
|
|
35
|
+
"padding": "0",
|
|
36
|
+
"fontFamily": "Arial, sans-serif"
|
|
37
|
+
},
|
|
38
|
+
"header": {
|
|
39
|
+
"backgroundColor": "#3498db",
|
|
40
|
+
"color": "white",
|
|
41
|
+
"padding": "20px",
|
|
42
|
+
"textAlign": "center"
|
|
43
|
+
},
|
|
44
|
+
"content": {
|
|
45
|
+
"maxWidth": "800px",
|
|
46
|
+
"margin": "0 auto",
|
|
47
|
+
"padding": "20px"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"content": {
|
|
51
|
+
"title": "欢迎使用XiangJsonCraft",
|
|
52
|
+
"subtitle": "简单高效的JSON配置与模板渲染工具",
|
|
53
|
+
"paragraph": "XiangJsonCraft让你可以通过JSON配置文件轻松定义页面样式和内容,无需编写复杂的CSS和JavaScript代码。"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 2. 创建HTML模板
|
|
59
|
+
|
|
60
|
+
然后,创建一个HTML模板文件,使用占位符来表示动态内容:
|
|
61
|
+
|
|
62
|
+
```html
|
|
63
|
+
<!-- template.html -->
|
|
64
|
+
<!DOCTYPE html>
|
|
65
|
+
<html lang="zh-CN">
|
|
66
|
+
<head>
|
|
67
|
+
<meta charset="UTF-8">
|
|
68
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
69
|
+
<title>{{title}}</title>
|
|
70
|
+
<style id="dynamic-styles"></style>
|
|
71
|
+
</head>
|
|
72
|
+
<body>
|
|
73
|
+
<header class="header">
|
|
74
|
+
<h1>{{title}}</h1>
|
|
75
|
+
<p>{{subtitle}}</p>
|
|
76
|
+
</header>
|
|
77
|
+
<div class="content">
|
|
78
|
+
<p>{{paragraph}}</p>
|
|
79
|
+
</div>
|
|
80
|
+
</body>
|
|
81
|
+
</html>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 3. 使用XiangJsonCraft渲染模板
|
|
85
|
+
|
|
86
|
+
最后,在JavaScript代码中使用XiangJsonCraft来渲染模板:
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
const { renderTemplate } = require('xiangjsoncraft');
|
|
90
|
+
const fs = require('fs');
|
|
91
|
+
const path = require('path');
|
|
92
|
+
|
|
93
|
+
// 读取配置文件和模板文件
|
|
94
|
+
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'), 'utf8'));
|
|
95
|
+
const template = fs.readFileSync(path.join(__dirname, 'template.html'), 'utf8');
|
|
96
|
+
|
|
97
|
+
// 渲染模板
|
|
98
|
+
const renderedHtml = renderTemplate(template, config);
|
|
99
|
+
|
|
100
|
+
// 输出渲染结果
|
|
101
|
+
console.log(renderedHtml);
|
|
102
|
+
|
|
103
|
+
// 保存渲染结果到文件
|
|
104
|
+
fs.writeFileSync(path.join(__dirname, 'output.html'), renderedHtml);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## API文档
|
|
108
|
+
|
|
109
|
+
### `renderTemplate(template: string, config: Config): string`
|
|
110
|
+
|
|
111
|
+
将HTML模板和JSON配置文件渲染为最终的HTML字符串。
|
|
112
|
+
|
|
113
|
+
#### 参数
|
|
114
|
+
|
|
115
|
+
- `template`: 字符串,HTML模板内容
|
|
116
|
+
- `config`: 对象,包含样式和内容配置
|
|
117
|
+
|
|
118
|
+
#### 返回值
|
|
119
|
+
|
|
120
|
+
- 字符串,渲染后的HTML内容
|
|
121
|
+
|
|
122
|
+
#### 示例
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
const renderedHtml = renderTemplate('<div class="container">{{title}}</div>', {
|
|
126
|
+
styles: {
|
|
127
|
+
container: {
|
|
128
|
+
backgroundColor: 'lightblue',
|
|
129
|
+
padding: '10px'
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
content: {
|
|
133
|
+
title: 'Hello, XiangJsonCraft!'
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
console.log(renderedHtml);
|
|
138
|
+
// 输出: <div class="container" style="background-color: lightblue; padding: 10px;">Hello, XiangJsonCraft!</div>
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 配置文件格式
|
|
142
|
+
|
|
143
|
+
配置文件是一个JSON对象,包含两个主要属性:
|
|
144
|
+
|
|
145
|
+
#### `styles`
|
|
146
|
+
|
|
147
|
+
定义CSS样式的对象,每个键对应一个HTML元素的类名或ID,值是CSS属性的键值对。
|
|
148
|
+
|
|
149
|
+
```json
|
|
150
|
+
{
|
|
151
|
+
"styles": {
|
|
152
|
+
"myClass": {
|
|
153
|
+
"color": "red",
|
|
154
|
+
"fontSize": "16px"
|
|
155
|
+
},
|
|
156
|
+
"#myId": {
|
|
157
|
+
"backgroundColor": "blue",
|
|
158
|
+
"width": "100px"
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
#### `content`
|
|
165
|
+
|
|
166
|
+
定义动态内容的对象,键对应HTML模板中的占位符(如`{{title}}`),值是要替换的内容。
|
|
167
|
+
|
|
168
|
+
```json
|
|
169
|
+
{
|
|
170
|
+
"content": {
|
|
171
|
+
"title": "页面标题",
|
|
172
|
+
"description": "这是一个描述文本"
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## 高级用法
|
|
178
|
+
|
|
179
|
+
### 批量处理多个模板
|
|
180
|
+
|
|
181
|
+
你可以使用XiangJsonCraft批量处理多个模板文件:
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
const { renderTemplate } = require('xiangjsoncraft');
|
|
185
|
+
const fs = require('fs');
|
|
186
|
+
const path = require('path');
|
|
187
|
+
|
|
188
|
+
// 读取配置文件
|
|
189
|
+
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'), 'utf8'));
|
|
190
|
+
|
|
191
|
+
// 读取模板目录中的所有文件
|
|
192
|
+
const templatesDir = path.join(__dirname, 'templates');
|
|
193
|
+
const outputDir = path.join(__dirname, 'output');
|
|
194
|
+
|
|
195
|
+
// 确保输出目录存在
|
|
196
|
+
if (!fs.existsSync(outputDir)) {
|
|
197
|
+
fs.mkdirSync(outputDir);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 读取所有模板文件并渲染
|
|
201
|
+
fs.readdirSync(templatesDir).forEach(file => {
|
|
202
|
+
if (file.endsWith('.html')) {
|
|
203
|
+
const templatePath = path.join(templatesDir, file);
|
|
204
|
+
const template = fs.readFileSync(templatePath, 'utf8');
|
|
205
|
+
|
|
206
|
+
// 渲染模板
|
|
207
|
+
const renderedHtml = renderTemplate(template, config);
|
|
208
|
+
|
|
209
|
+
// 保存渲染结果
|
|
210
|
+
const outputPath = path.join(outputDir, file);
|
|
211
|
+
fs.writeFileSync(outputPath, renderedHtml);
|
|
212
|
+
|
|
213
|
+
console.log(`已渲染并保存: ${file}`);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### 在浏览器中使用
|
|
219
|
+
|
|
220
|
+
XiangJsonCraft也可以在浏览器环境中使用:
|
|
221
|
+
|
|
222
|
+
```html
|
|
223
|
+
<!DOCTYPE html>
|
|
224
|
+
<html lang="zh-CN">
|
|
225
|
+
<head>
|
|
226
|
+
<meta charset="UTF-8">
|
|
227
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
228
|
+
<title>浏览器中使用XiangJsonCraft</title>
|
|
229
|
+
<script src="https://cdn.jsdelivr.net/npm/xiangjsoncraft/dist/xiangjsoncraft.min.js"></script>
|
|
230
|
+
</head>
|
|
231
|
+
<body>
|
|
232
|
+
<div id="app"></div>
|
|
233
|
+
|
|
234
|
+
<script>
|
|
235
|
+
// 定义配置
|
|
236
|
+
const config = {
|
|
237
|
+
styles: {
|
|
238
|
+
app: {
|
|
239
|
+
maxWidth: '600px',
|
|
240
|
+
margin: '0 auto',
|
|
241
|
+
padding: '20px',
|
|
242
|
+
border: '1px solid #ddd',
|
|
243
|
+
borderRadius: '5px'
|
|
244
|
+
},
|
|
245
|
+
title: {
|
|
246
|
+
color: '#3498db',
|
|
247
|
+
textAlign: 'center'
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
content: {
|
|
251
|
+
title: '欢迎使用XiangJsonCraft',
|
|
252
|
+
message: '这是在浏览器中使用XiangJsonCraft的示例。'
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// 定义模板
|
|
257
|
+
const template = `
|
|
258
|
+
<div class="app">
|
|
259
|
+
<h1 class="title">{{title}}</h1>
|
|
260
|
+
<p>{{message}}</p>
|
|
261
|
+
</div>
|
|
262
|
+
`;
|
|
263
|
+
|
|
264
|
+
// 渲染模板
|
|
265
|
+
const renderedHtml = XiangJsonCraft.renderTemplate(template, config);
|
|
266
|
+
|
|
267
|
+
// 将渲染结果插入到DOM中
|
|
268
|
+
document.getElementById('app').innerHTML = renderedHtml;
|
|
269
|
+
</script>
|
|
270
|
+
</body>
|
|
271
|
+
</html>
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## 贡献指南
|
|
275
|
+
|
|
276
|
+
我们欢迎任何人贡献代码或提出建议。如果你想参与开发,请遵循以下步骤:
|
|
277
|
+
|
|
278
|
+
1. 克隆仓库:`git clone https://github.com/yourusername/xiangjsoncraft.git`
|
|
279
|
+
2. 创建新分支:`git checkout -b feature/your-feature`
|
|
280
|
+
3. 提交代码:`git commit -m "Add your feature"`
|
|
281
|
+
4. 推送分支:`git push origin feature/your-feature`
|
|
282
|
+
5. 创建Pull Request
|
|
283
|
+
|
|
284
|
+
## 许可证
|
|
285
|
+
|
|
286
|
+
MIT License
|
|
287
|
+
|
|
288
|
+
Copyright (c) 2023 xiang
|
|
289
|
+
|
|
290
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
291
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
292
|
+
in the Software without restriction, including without limitation the rights
|
|
293
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
294
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
295
|
+
furnished to do so, subject to the following conditions:
|
|
296
|
+
|
|
297
|
+
The above copyright notice and this permission notice shall be included in all
|
|
298
|
+
copies or substantial portions of the Software.
|
|
299
|
+
|
|
300
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
301
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
302
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
303
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
304
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
305
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
306
|
+
SOFTWARE.
|
|
307
|
+
|
|
308
|
+
## 联系我们
|
|
309
|
+
|
|
310
|
+
如果你有任何问题或建议,请通过以下方式联系我们:
|
|
311
|
+
|
|
312
|
+
- GitHub Issues: https://github.com/yourusername/xiangjsoncraft/issues
|
|
313
|
+
- Email: xiang@example.com
|
|
314
|
+
|
|
315
|
+
感谢使用XiangJsonCraft!
|