flex-html-render 1.0.0

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.
Files changed (3) hide show
  1. package/README.md +299 -0
  2. package/index.d.ts +11 -0
  3. package/package.json +42 -0
package/README.md ADDED
@@ -0,0 +1,299 @@
1
+ # flex-html-render
2
+
3
+ `flex-html-render` 是一個將 HTML 字串轉換為 LINE Flex Message JSON 結構的工具,方便開發者以 HTML 標記語法設計 Flex Message,並自動轉換為 LINE Messaging API 所需的格式。
4
+
5
+ ## Demo 頁面
6
+ - [Flex Message HTML Simulator](https://spa.maju-web.club/flex-message-html-simulator/)
7
+
8
+ ## 特色
9
+ - 以 HTML 字串描述 Flex Message 結構
10
+ - 自動解析 HTML 並轉換為 Flex Message JSON
11
+ - 支援多種 Flex Message 元素
12
+ - 易於整合於 Node.js 或前端專案
13
+
14
+ ## 安裝
15
+
16
+ ```bash
17
+ npm install flex-html-render
18
+ ```
19
+
20
+ ## 使用方式
21
+
22
+ ```js
23
+ import convertHtmlToFlexMessage from 'flex-html-render';
24
+
25
+ const htmlString = `
26
+ <bubble>
27
+ <body>
28
+ <box layout="vertical">
29
+ <text>這是一個 Flex Message</text>
30
+ </box>
31
+ </body>
32
+ </bubble>
33
+ `;
34
+
35
+ const flexMessage = convertHtmlToFlexMessage(htmlString);
36
+ console.log(JSON.stringify(flexMessage, null, 2));
37
+ ```
38
+
39
+ ## API
40
+
41
+ ### `convertHtmlToFlexMessage(htmlString)`
42
+ - **參數**:`htmlString` (string) - Flex Message 對應的 HTML 字串
43
+ - **回傳**:Flex Message JSON 物件陣列
44
+
45
+ ### 其他匯出
46
+ - `officialDemoString`:官方範例 HTML 字串
47
+
48
+ ## 支援的 Flex Message 元素
49
+ 本工具支援大部分 [LINE Flex Message](https://developers.line.biz/en/reference/messaging-api/#flex-message) 元素,包括:
50
+ - bubble
51
+ - carousel
52
+ - box
53
+ - text
54
+ - image
55
+ - video
56
+ - button
57
+ - icon
58
+ - separator
59
+
60
+ 詳細 Flex Message 元素與屬性請參考 [LINE 官方文件](https://developers.line.biz/en/reference/messaging-api/#flex-message)。
61
+
62
+ ## 標籤類型說明 (types.js)
63
+
64
+ ### 容器標籤 (Container Tags)
65
+ - `<bubble>` - Flex Message 的基本容器,只能包含 top-level 區塊標籤
66
+ - `<carousel>` - 輪播容器,只能包含 `<bubble>` 標籤,最多 12 個
67
+
68
+ ### 頂層區塊標籤 (Top-level Section Tags)
69
+ 這些標籤只能直接放在 `<bubble>` 內,且每個只能有一個子元素:
70
+ - `<header>` - 標題區塊
71
+ - `<hero>` - 英雄圖區塊
72
+ - `<body>` - 主體內容區塊
73
+ - `<footer>` - 頁尾區塊
74
+
75
+ ### 元件標籤 (Component Tags)
76
+ - `<box>` - 容器元件,需指定 `layout` 屬性(vertical、horizontal、baseline)
77
+ - `<text>` - 文字元件,可包含多個 `<span>` 子元素
78
+ - `<span>` - 文字片段元件,只能有一個文字子節點
79
+ - `<image>` - 圖片元件,需指定 `url` 屬性
80
+ - `<video>` - 影片元件,必須包含一個 `<box>` 或 `<image>` 作為 `altContent`
81
+ - `<icon>` - 圖示元件,需指定 `url` 屬性
82
+ - `<separator>` - 分隔線元件
83
+ - `<button>` - 按鈕元件,必須包含一個 `<action>` 子元素
84
+
85
+ ### 便捷標籤 (Convenience Tags)
86
+ 這些是對常用設定的簡寫標籤:
87
+ - `<baseline>` - 等同於 `<box layout="baseline">`
88
+ - `<row>` - 等同於 `<box layout="horizontal">`
89
+ - `<vertical>` - 等同於 `<box layout="vertical">`
90
+ - `<strong>` - 自動設定 `weight="bold"` 的文字標籤
91
+ - `<space size="n">` - 產生 n 個空格的 span 元件(預設 size=1)
92
+
93
+ ### 特殊功能標籤
94
+ - `<action>` - 動作設定標籤,必須放在需要添加動作的元件內(如 `<button>`、`<box>`、`<image>` 等)
95
+ - `<background>` - 背景設定標籤,只能放在 Box 相關標籤內(`<box>`、`<baseline>`、`<row>`、`<vertical>`)
96
+
97
+ ## 特殊標籤使用規則
98
+
99
+ ### 1. `<bubble>` 容器規則
100
+ `<bubble>` 只能包含 top-level 區塊標籤(header、hero、body、footer),不能直接包含其他元件。
101
+
102
+ ```html
103
+ <!-- ✅ 正確 -->
104
+ <bubble>
105
+ <header>
106
+ <box layout="vertical">
107
+ <text>標題</text>
108
+ </box>
109
+ </header>
110
+ <body>
111
+ <box layout="vertical">
112
+ <text>內容</text>
113
+ </box>
114
+ </body>
115
+ </bubble>
116
+
117
+ <!-- ❌ 錯誤 - 不能直接包含 box -->
118
+ <bubble>
119
+ <box layout="vertical">
120
+ <text>內容</text>
121
+ </box>
122
+ </bubble>
123
+ ```
124
+
125
+ ### 2. `<action>` 使用規則
126
+ `<action>` 標籤必須放在需要添加互動行為的元件內部,會自動從子元素中提取並設定為該元件的 action 屬性。
127
+
128
+ ```html
129
+ <!-- 按鈕必須包含 action -->
130
+ <button>
131
+ <action type="uri" uri="https://example.com" />
132
+ </button>
133
+
134
+ <!-- Box 元件添加點擊動作 -->
135
+ <box layout="vertical">
136
+ <text>點擊這個 Box</text>
137
+ <action type="uri" uri="https://example.com" />
138
+ </box>
139
+
140
+ <!-- Image 元件添加點擊動作 -->
141
+ <image url="https://example.com/image.png">
142
+ <action type="uri" uri="https://example.com" />
143
+ </image>
144
+ ```
145
+
146
+ ### 3. `<background>` 使用規則
147
+ `<background>` 標籤只能放在 Box 相關標籤內(`<box>`、`<baseline>`、`<row>`、`<vertical>`),用於設定背景。
148
+
149
+ ```html
150
+ <!-- ✅ 正確 - 在 box 內使用 -->
151
+ <box layout="vertical">
152
+ <background type="linearGradient" angle="90deg" startColor="#ff0000" endColor="#0000ff" />
153
+ <text>有漸層背景的 Box</text>
154
+ </box>
155
+
156
+ <!-- ✅ 正確 - 在 vertical 內使用 -->
157
+ <vertical>
158
+ <background type="linearGradient" angle="0deg" startColor="#ffff00" endColor="#00ff00" />
159
+ <text>有背景的垂直容器</text>
160
+ </vertical>
161
+
162
+ <!-- ❌ 錯誤 - 不能在 text 內使用 -->
163
+ <text>
164
+ <background type="linearGradient" angle="0deg" startColor="#ffff00" endColor="#00ff00" />
165
+ 文字
166
+ </text>
167
+ ```
168
+
169
+ ### 4. `<strong>` 使用方式
170
+ `<strong>` 標籤會自動轉換為 `<text weight="bold">`,用於顯示粗體文字。
171
+
172
+ ```html
173
+ <box layout="vertical">
174
+ <strong>這是粗體文字</strong>
175
+ <text>這是一般文字</text>
176
+ </box>
177
+ ```
178
+
179
+ ### 5. `<space>` 使用方式
180
+ `<space>` 標籤用於在文字中插入空格,可透過 `size` 屬性指定空格數量。
181
+
182
+ ```html
183
+ <text>
184
+ <span>Hello</span>
185
+ <space size="3" />
186
+ <span>World</span>
187
+ </text>
188
+ ```
189
+
190
+ ### 6. 便捷 Box 標籤(`<baseline>`、`<row>`、`<vertical>`)
191
+ 這些標籤是 `<box>` 的簡寫形式,省去了 `layout` 屬性的設定。
192
+
193
+ ```html
194
+ <!-- baseline: 基線對齊佈局 -->
195
+ <baseline>
196
+ <text>左側文字</text>
197
+ <text>右側文字</text>
198
+ </baseline>
199
+
200
+ <!-- row: 水平佈局 -->
201
+ <row>
202
+ <image url="https://example.com/icon.png" />
203
+ <text>橫向排列</text>
204
+ </row>
205
+
206
+ <!-- vertical: 垂直佈局 -->
207
+ <vertical>
208
+ <text>第一行</text>
209
+ <text>第二行</text>
210
+ </vertical>
211
+ ```
212
+
213
+ ### 7. `<carousel>` 使用規則
214
+ `<carousel>` 只能包含 `<bubble>` 子元素,且最多 12 個。
215
+
216
+ ```html
217
+ <carousel>
218
+ <bubble>
219
+ <body>
220
+ <box layout="vertical">
221
+ <text>第一個 Bubble</text>
222
+ </box>
223
+ </body>
224
+ </bubble>
225
+ <bubble>
226
+ <body>
227
+ <box layout="vertical">
228
+ <text>第二個 Bubble</text>
229
+ </box>
230
+ </body>
231
+ </bubble>
232
+ </carousel>
233
+ ```
234
+
235
+ ### 8. `<video>` 使用規則
236
+ `<video>` 必須包含一個 `<box>` 或 `<image>` 作為 altContent(影片無法播放時的替代內容)。
237
+
238
+ ```html
239
+ <video url="https://example.com/video.mp4" previewUrl="https://example.com/preview.jpg">
240
+ <image url="https://example.com/alt-image.jpg" />
241
+ </video>
242
+ ```
243
+
244
+ ## 完整範例
245
+
246
+ ```html
247
+ <bubble>
248
+ <header>
249
+ <vertical>
250
+ <background type="linearGradient" angle="90deg" startColor="#667eea" endColor="#764ba2" />
251
+ <strong>商品資訊</strong>
252
+ </vertical>
253
+ </header>
254
+ <body>
255
+ <vertical>
256
+ <image url="https://example.com/product.jpg" aspectRatio="4:3" size="full" />
257
+ <baseline>
258
+ <text>價格:</text>
259
+ <space size="2" />
260
+ <strong>NT$ 1,000</strong>
261
+ </baseline>
262
+ <separator />
263
+ <text>這是商品的詳細說明文字</text>
264
+ </vertical>
265
+ </body>
266
+ <footer>
267
+ <row>
268
+ <button>
269
+ <action type="uri" uri="https://example.com/buy" label="立即購買" />
270
+ </button>
271
+ </row>
272
+ </footer>
273
+ </bubble>
274
+ ```
275
+
276
+ ## 範例
277
+
278
+ ```js
279
+ const html = `
280
+ <bubble>
281
+ <body>
282
+ <box layout="vertical">
283
+ <text color="#ff0000">Hello, Flex!</text>
284
+ <image url="https://example.com/image.png" />
285
+ </box>
286
+ </body>
287
+ </bubble>
288
+ `;
289
+
290
+ const result = convertHtmlToFlexMessage(html);
291
+ console.log(result);
292
+ ```
293
+
294
+ ## 注意事項
295
+ - 請確保 HTML 結構正確,否則可能無法正確轉換。
296
+ - 轉換後的 JSON 結構請依照 [LINE Flex Message 規範](https://developers.line.biz/en/reference/messaging-api/#flex-message) 使用。
297
+
298
+ ## 授權
299
+ MIT License
package/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * 將 HTML 字串轉換為 LINE Flex Message JSON 結構
3
+ * @param htmlString - Flex Message 對應的 HTML 字串
4
+ * @returns Flex Message JSON 物件陣列
5
+ */
6
+ export default function convertHtmlToFlexMessage(htmlString: string): any[];
7
+
8
+ /**
9
+ * 官方範例 HTML 字串
10
+ */
11
+ export const officialDemoString: string;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "flex-html-render",
3
+ "version": "1.0.0",
4
+ "description": "將 HTML 字串轉換為 LINE Flex Message JSON 結構的工具",
5
+ "type": "module",
6
+ "main": "dist/flex-html-render.cjs.js",
7
+ "module": "dist/flex-html-render.es.js",
8
+ "types": "index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./index.d.ts",
12
+ "import": "./dist/flex-html-render.es.js",
13
+ "require": "./dist/flex-html-render.cjs.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist/*.js",
18
+ "README.md",
19
+ "index.d.ts"
20
+ ],
21
+ "keywords": [
22
+ "line",
23
+ "flex-message",
24
+ "html",
25
+ "converter",
26
+ "messaging-api"
27
+ ],
28
+ "author": "johnnywang1994",
29
+ "license": "MIT",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/johnnywang1994"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/johnnywang1994"
36
+ },
37
+ "homepage": "https://github.com/johnnywang1994",
38
+ "peerDependencies": {},
39
+ "dependencies": {
40
+ "htmlparser2": "^10.0.0"
41
+ }
42
+ }