@burger-editor/utils 4.0.0-alpha.50 → 4.0.0-alpha.52
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 +249 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,3 +1,251 @@
|
|
|
1
|
-
# utils
|
|
1
|
+
# @burger-editor/utils
|
|
2
2
|
|
|
3
3
|
[](https://badge.fury.io/js/@burger-editor%2Futils)
|
|
4
|
+
|
|
5
|
+
BurgerEditorで使用する共通ユーティリティ関数集です。
|
|
6
|
+
|
|
7
|
+
## 概要
|
|
8
|
+
|
|
9
|
+
`@burger-editor/utils`は、BurgerEditorのコアパッケージや関連パッケージで共有される汎用ユーティリティ関数を提供します。文字列変換、マークダウン処理、日付フォーマット、HTML操作などの機能を含みます。
|
|
10
|
+
|
|
11
|
+
## インストール
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @burger-editor/utils
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
または
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
yarn add @burger-editor/utils
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 提供される関数
|
|
24
|
+
|
|
25
|
+
### 文字列ケース変換
|
|
26
|
+
|
|
27
|
+
#### `camelCase(str: string): string`
|
|
28
|
+
|
|
29
|
+
ケバブケース文字列をキャメルケースに変換します。
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { camelCase } from '@burger-editor/utils';
|
|
33
|
+
|
|
34
|
+
camelCase('my-property-name'); // => 'myPropertyName'
|
|
35
|
+
camelCase('background-color'); // => 'backgroundColor'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
#### `kebabCase(str: string): string`
|
|
39
|
+
|
|
40
|
+
キャメルケース文字列をケバブケースに変換します。
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { kebabCase } from '@burger-editor/utils';
|
|
44
|
+
|
|
45
|
+
kebabCase('myPropertyName'); // => 'my-property-name'
|
|
46
|
+
kebabCase('backgroundColor'); // => 'background-color'
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### マークダウン変換
|
|
50
|
+
|
|
51
|
+
#### `markdownToHtml(markdown: string): string`
|
|
52
|
+
|
|
53
|
+
MarkdownテキストをHTMLに変換します。
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { markdownToHtml } from '@burger-editor/utils';
|
|
57
|
+
|
|
58
|
+
const html = markdownToHtml('# 見出し\n\n**太字**のテキスト');
|
|
59
|
+
// => '<h1>見出し</h1>\n<p><strong>太字</strong>のテキスト</p>'
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### `htmlToMarkdown(html: string): string`
|
|
63
|
+
|
|
64
|
+
HTMLをMarkdownテキストに変換します。
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { htmlToMarkdown } from '@burger-editor/utils';
|
|
68
|
+
|
|
69
|
+
const markdown = htmlToMarkdown('<h1>見出し</h1><p><strong>太字</strong></p>');
|
|
70
|
+
// => '# 見出し\n\n**太字**'
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### テキストフォーマット
|
|
74
|
+
|
|
75
|
+
#### `nl2br(text: string): string`
|
|
76
|
+
|
|
77
|
+
改行コードを`<br />`タグに変換します。
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { nl2br } from '@burger-editor/utils';
|
|
81
|
+
|
|
82
|
+
nl2br('行1\n行2\r\n行3');
|
|
83
|
+
// => '行1<br />行2<br />行3'
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### `br2nl(html: string): string`
|
|
87
|
+
|
|
88
|
+
`<br>`タグを改行コード(`\r\n`)に変換します。
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { br2nl } from '@burger-editor/utils';
|
|
92
|
+
|
|
93
|
+
br2nl('行1<br>行2<br />行3');
|
|
94
|
+
// => '行1\r\n行2\r\n行3'
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 数値・日付フォーマット
|
|
98
|
+
|
|
99
|
+
#### `formatByteSize(byteSize: number, digits?: number, autoFormat?: boolean): string`
|
|
100
|
+
|
|
101
|
+
バイト数を人間が読みやすい形式にフォーマットします。
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { formatByteSize } from '@burger-editor/utils';
|
|
105
|
+
|
|
106
|
+
formatByteSize(1024); // => '1.00kB'
|
|
107
|
+
formatByteSize(1048576); // => '1.00MB'
|
|
108
|
+
formatByteSize(1536, 1); // => '1.5kB'
|
|
109
|
+
formatByteSize(500, 2, false); // => '500byte'
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### `formatDate(timestamp: number, format: string): string`
|
|
113
|
+
|
|
114
|
+
Unixタイムスタンプを指定したフォーマットの日付文字列に変換します。
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
import { formatDate } from '@burger-editor/utils';
|
|
118
|
+
|
|
119
|
+
formatDate(1704067200, 'YYYY-MM-DD'); // => '2024-01-01'
|
|
120
|
+
formatDate(1704067200, 'YYYY年MM月DD日'); // => '2024年01月01日'
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### URL・パス処理
|
|
124
|
+
|
|
125
|
+
#### `origin(): string`
|
|
126
|
+
|
|
127
|
+
現在のURLのオリジン(プロトコル + ホスト + ポート)を取得します。
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { origin } from '@burger-editor/utils';
|
|
131
|
+
|
|
132
|
+
// ブラウザで http://localhost:3000/page にアクセスしている場合
|
|
133
|
+
origin(); // => 'http://localhost:3000'
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
#### `parseYTId(idOrUrl: string): string`
|
|
137
|
+
|
|
138
|
+
YouTube URLから動画IDを抽出します。
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
import { parseYTId } from '@burger-editor/utils';
|
|
142
|
+
|
|
143
|
+
parseYTId('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
|
144
|
+
// => 'dQw4w9WgXcQ'
|
|
145
|
+
|
|
146
|
+
parseYTId('https://youtu.be/dQw4w9WgXcQ');
|
|
147
|
+
// => 'dQw4w9WgXcQ'
|
|
148
|
+
|
|
149
|
+
parseYTId('dQw4w9WgXcQ');
|
|
150
|
+
// => 'dQw4w9WgXcQ'
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### `getBackgroundImagePath(value: string): string`
|
|
154
|
+
|
|
155
|
+
CSS `background-image` プロパティ値からパスを抽出します。
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
import { getBackgroundImagePath } from '@burger-editor/utils';
|
|
159
|
+
|
|
160
|
+
getBackgroundImagePath('url("/images/bg.jpg")');
|
|
161
|
+
// => '/images/bg.jpg'
|
|
162
|
+
|
|
163
|
+
getBackgroundImagePath("url('https://example.com/bg.png')");
|
|
164
|
+
// => 'https://example.com/bg.png'
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### バリデーション
|
|
168
|
+
|
|
169
|
+
#### `isValidAsClassName(className: string): boolean`
|
|
170
|
+
|
|
171
|
+
文字列が有効なCSSクラス名かどうかをチェックします。
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
import { isValidAsClassName } from '@burger-editor/utils';
|
|
175
|
+
|
|
176
|
+
isValidAsClassName('my-class'); // => true
|
|
177
|
+
isValidAsClassName('_private'); // => true
|
|
178
|
+
isValidAsClassName('123invalid'); // => false
|
|
179
|
+
isValidAsClassName('my class'); // => false
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### HTML操作
|
|
183
|
+
|
|
184
|
+
#### `strToDOM(html: string): HTMLElement`
|
|
185
|
+
|
|
186
|
+
HTML文字列をDOM要素に変換します。
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { strToDOM } from '@burger-editor/utils';
|
|
190
|
+
|
|
191
|
+
const element = strToDOM('<div class="container">コンテンツ</div>');
|
|
192
|
+
// => HTMLDivElement
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### `normalizeHtml(html: string): string`
|
|
196
|
+
|
|
197
|
+
HTMLを正規化します(空白の調整など)。
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
import { normalizeHtml } from '@burger-editor/utils';
|
|
201
|
+
|
|
202
|
+
const normalized = normalizeHtml('<div> content </div>');
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
#### `replaceCommentWithHTML(template: string, items: Record<string, string>, replacer: Function): string`
|
|
206
|
+
|
|
207
|
+
テンプレート内のコメントマーカーをHTMLに置き換えます。
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
import { replaceCommentWithHTML } from '@burger-editor/utils';
|
|
211
|
+
|
|
212
|
+
const template = '<div><!-- item:text --></div>';
|
|
213
|
+
const items = { text: '<p>テキスト</p>' };
|
|
214
|
+
const result = replaceCommentWithHTML(template, items, (_, itemHtml) => itemHtml);
|
|
215
|
+
// => '<div><p>テキスト</p></div>'
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### アイテム操作
|
|
219
|
+
|
|
220
|
+
#### `mergeItems(itemSeeds: Record<string, ItemSeed>, customItems?: Record<string, ItemSeed>): Record<string, ItemSeed>`
|
|
221
|
+
|
|
222
|
+
アイテム定義をマージします。
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
import { mergeItems } from '@burger-editor/utils';
|
|
226
|
+
|
|
227
|
+
const defaultItems = { text: textItemSeed };
|
|
228
|
+
const customItems = { custom: customItemSeed };
|
|
229
|
+
const merged = mergeItems(defaultItems, customItems);
|
|
230
|
+
// => { text: textItemSeed, custom: customItemSeed }
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## 依存関係
|
|
234
|
+
|
|
235
|
+
- **dayjs** - 日付フォーマット処理
|
|
236
|
+
- **marked** - Markdown to HTML変換
|
|
237
|
+
- **turndown** - HTML to Markdown変換
|
|
238
|
+
|
|
239
|
+
## 使用パッケージ
|
|
240
|
+
|
|
241
|
+
このパッケージは以下のBurgerEditorパッケージで使用されています:
|
|
242
|
+
|
|
243
|
+
- [@burger-editor/core](../core/)
|
|
244
|
+
- [@burger-editor/blocks](../blocks/)
|
|
245
|
+
- [@burger-editor/frozen-patty](../frozen-patty/)
|
|
246
|
+
- [@burger-editor/migrator](../migrator/)
|
|
247
|
+
- [@burger-editor/mcp-server](../mcp-server/)
|
|
248
|
+
|
|
249
|
+
## ライセンス
|
|
250
|
+
|
|
251
|
+
Dual Licensed under MIT OR Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@burger-editor/utils",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.52",
|
|
4
4
|
"description": "BurgerEditor Editor Utility Functions",
|
|
5
5
|
"author": "D-ZERO",
|
|
6
6
|
"license": "(MIT OR Apache-2.0)",
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/turndown": "5.0.6"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "992a7d22e8ae02654a084658360c3bdce90028d7"
|
|
37
37
|
}
|