@burger-editor/utils 4.0.0-alpha.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2024 D-ZERO Co., Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # utils
2
+
3
+ [![npm version](https://badge.fury.io/js/@burger-editor%2Futils.svg)](https://badge.fury.io/js/@burger-editor%2Futils)
@@ -0,0 +1,2 @@
1
+ export * from './markdown.js';
2
+ export * from './utils.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './markdown.js';
2
+ export * from './utils.js';
@@ -0,0 +1,10 @@
1
+ /**
2
+ *
3
+ * @param markdown
4
+ */
5
+ export declare function markdownToHtml(markdown: string): string;
6
+ /**
7
+ *
8
+ * @param html
9
+ */
10
+ export declare function htmlToMarkdown(html: string): string;
@@ -0,0 +1,23 @@
1
+ import { marked } from 'marked';
2
+ import TurndownService from 'turndown';
3
+ /**
4
+ *
5
+ * @param markdown
6
+ */
7
+ export function markdownToHtml(markdown) {
8
+ return marked(markdown, {
9
+ async: false,
10
+ });
11
+ }
12
+ /**
13
+ *
14
+ * @param html
15
+ */
16
+ export function htmlToMarkdown(html) {
17
+ const turndownService = new TurndownService({
18
+ headingStyle: 'atx',
19
+ bulletListMarker: '-',
20
+ codeBlockStyle: 'fenced',
21
+ });
22
+ return turndownService.turndown(html);
23
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * 現在のURLのオリジン
3
+ */
4
+ export declare function origin(): string;
5
+ /**
6
+ * 改行コードを改行タグに変換
7
+ * @param text 対象のテキスト
8
+ * @returns 変換されたテキスト
9
+ */
10
+ export declare function nl2br(text: string): string;
11
+ /**
12
+ * 改行タグを改行コードに変換
13
+ * @param html
14
+ * @returns 変換されたテキスト
15
+ */
16
+ export declare function br2nl(html: string): string;
17
+ /**
18
+ * 数値をバイトサイズ単位にフォーマットする
19
+ * @param byteSize 対象の数値
20
+ * @param digits 小数点の桁数
21
+ * @param autoFormat SI接頭辞をつけるかどうか
22
+ * @returns フォーマットされた文字列
23
+ */
24
+ export declare function formatByteSize(byteSize: number, digits?: number, autoFormat?: boolean): string;
25
+ /**
26
+ * Unixタイムスタンプを日付文字列に変換する
27
+ * @param timestamp Unixタイムスタンプ
28
+ * @param format フォーマット
29
+ */
30
+ export declare function formatDate(timestamp: number, format: string): string;
31
+ /**
32
+ * YouTubeの動画URLからIDを抽出する
33
+ *
34
+ * 何も抽出できなかった場合 空文字列を返す
35
+ *
36
+ * 参考: http://stackoverflow.com/questions/6903823/regex-for-youtube-id
37
+ * 以下の形式が対応可能
38
+ * http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/FJUvudQsKCM
39
+ * http://youtu.be/NLqAF9hrVbY
40
+ * http://www.youtube.com/embed/NLqAF9hrVbY
41
+ * https://www.youtube.com/embed/NLqAF9hrVbY
42
+ * http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
43
+ * http://www.youtube.com/watch?v=NLqAF9hrVbY
44
+ * http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
45
+ * http://www.youtube.com/watch?v=JYArUl0TzhA&feature=featured
46
+ * @param idOrUrl YouTubeのURLもしくはID
47
+ * @returns 抽出したID
48
+ */
49
+ export declare function parseYTId(idOrUrl: string): string;
50
+ /**
51
+ * 正しいCSSクラス名かどうかチェックする
52
+ * @param className チェック対象
53
+ * @returns 結果
54
+ */
55
+ export declare function isValidAsClassName(className: string): boolean;
56
+ /**
57
+ * background-imageからパスを取得する
58
+ * @param value
59
+ * @returns 結果
60
+ */
61
+ export declare function getBackgroundImagePath(value: string): string;
62
+ /**
63
+ *
64
+ * @param html
65
+ */
66
+ export declare function strToDOM(html: string): HTMLElement;
package/dist/utils.js ADDED
@@ -0,0 +1,117 @@
1
+ import dayjs from 'dayjs';
2
+ /**
3
+ * 現在のURLのオリジン
4
+ */
5
+ export function origin() {
6
+ return `${location.protocol}//${location.hostname}${location.port ? ':' + location.port : ''}`;
7
+ }
8
+ /**
9
+ * 改行コードを改行タグに変換
10
+ * @param text 対象のテキスト
11
+ * @returns 変換されたテキスト
12
+ */
13
+ export function nl2br(text) {
14
+ return `${text}`.replaceAll(/\r\n|\n\r|\r|\n/g, '<br />');
15
+ }
16
+ /**
17
+ * 改行タグを改行コードに変換
18
+ * @param html
19
+ * @returns 変換されたテキスト
20
+ */
21
+ export function br2nl(html) {
22
+ return `${html}`.replaceAll(/<\s*br\s*\/?>/g, '\r\n');
23
+ }
24
+ /**
25
+ * 数値をバイトサイズ単位にフォーマットする
26
+ * @param byteSize 対象の数値
27
+ * @param digits 小数点の桁数
28
+ * @param autoFormat SI接頭辞をつけるかどうか
29
+ * @returns フォーマットされた文字列
30
+ */
31
+ export function formatByteSize(byteSize, digits = 2, autoFormat = true) {
32
+ let compute = byteSize;
33
+ let counter = 0;
34
+ const unit = ['byte', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB'];
35
+ if (autoFormat) {
36
+ while (compute > 1024) {
37
+ compute /= 1024;
38
+ counter += 1;
39
+ }
40
+ if (counter === 0) {
41
+ digits = 0;
42
+ }
43
+ return compute.toFixed(digits) + unit[counter];
44
+ }
45
+ else {
46
+ return byteSize + unit[0];
47
+ }
48
+ }
49
+ /**
50
+ * Unixタイムスタンプを日付文字列に変換する
51
+ * @param timestamp Unixタイムスタンプ
52
+ * @param format フォーマット
53
+ */
54
+ export function formatDate(timestamp, format) {
55
+ return dayjs.unix(timestamp).format(format);
56
+ }
57
+ /**
58
+ * YouTubeの動画URLからIDを抽出する
59
+ *
60
+ * 何も抽出できなかった場合 空文字列を返す
61
+ *
62
+ * 参考: http://stackoverflow.com/questions/6903823/regex-for-youtube-id
63
+ * 以下の形式が対応可能
64
+ * http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/FJUvudQsKCM
65
+ * http://youtu.be/NLqAF9hrVbY
66
+ * http://www.youtube.com/embed/NLqAF9hrVbY
67
+ * https://www.youtube.com/embed/NLqAF9hrVbY
68
+ * http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
69
+ * http://www.youtube.com/watch?v=NLqAF9hrVbY
70
+ * http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
71
+ * http://www.youtube.com/watch?v=JYArUl0TzhA&feature=featured
72
+ * @param idOrUrl YouTubeのURLもしくはID
73
+ * @returns 抽出したID
74
+ */
75
+ export function parseYTId(idOrUrl) {
76
+ let id = '';
77
+ if (!idOrUrl) {
78
+ return id;
79
+ }
80
+ const match = idOrUrl.match(/(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/user\/\S+|\/ytscreeningroom\?v=))([\w-]{10,12})\b/);
81
+ if (match) {
82
+ id = match[1] ?? '';
83
+ }
84
+ else {
85
+ id = idOrUrl;
86
+ }
87
+ return id;
88
+ }
89
+ /**
90
+ * 正しいCSSクラス名かどうかチェックする
91
+ * @param className チェック対象
92
+ * @returns 結果
93
+ */
94
+ export function isValidAsClassName(className) {
95
+ const validClassName = /^-?[_a-z][\w-]*$/i;
96
+ return validClassName.test(className);
97
+ }
98
+ /**
99
+ * background-imageからパスを取得する
100
+ * @param value
101
+ * @returns 結果
102
+ */
103
+ export function getBackgroundImagePath(value) {
104
+ return decodeURI(value.replace(/^url\(["']?([^"']+)["']?\)$/i, '$1').replace(origin(), ''));
105
+ }
106
+ /**
107
+ *
108
+ * @param html
109
+ */
110
+ export function strToDOM(html) {
111
+ const doc = new DOMParser().parseFromString(html, 'text/html');
112
+ const el = doc.body.firstElementChild;
113
+ if (!el || !(el instanceof HTMLElement)) {
114
+ throw new Error(`Element not found: ${html}`);
115
+ }
116
+ return el;
117
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@burger-editor/utils",
3
+ "version": "4.0.0-alpha.1",
4
+ "description": "BurgerEditor Editor Utility Functions",
5
+ "author": "D-ZERO",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "url": "https://github.com/d-zero-dev/BurgerEditor.git"
9
+ },
10
+ "private": false,
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "type": "module",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./dist/index.js",
18
+ "types": "./dist/index.d.ts"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsc --project tsconfig.build.json",
26
+ "dev": "tsc --watch --project tsconfig.build.json",
27
+ "clean": "tsc --build --clean"
28
+ },
29
+ "dependencies": {
30
+ "dayjs": "1.11.13",
31
+ "marked": "15.0.7",
32
+ "turndown": "7.2.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/turndown": "5.0.5"
36
+ },
37
+ "gitHead": "1d6fef96516914352bff60f3319fc9884d882f77"
38
+ }