@dvirus-js/utils 0.0.7 → 0.0.14
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/CHANGELOG.md +54 -0
- package/index.js +666 -534
- package/lib/get-empty-keys.d.ts +29 -0
- package/lib/html-text-parser/api.d.ts +62 -0
- package/lib/html-text-parser/constant.d.ts +0 -1
- package/lib/html-text-parser/dom-renderer.d.ts +17 -0
- package/lib/html-text-parser/grouping.d.ts +2 -0
- package/lib/html-text-parser/index.d.ts +2 -2
- package/lib/html-text-parser/parsing.d.ts +26 -0
- package/lib/html-text-parser/segment.d.ts +2 -0
- package/lib/html-text-parser/stylesheet.d.ts +21 -0
- package/lib/html-text-parser/tag-regex.d.ts +3 -0
- package/lib/html-text-parser/tags.d.ts +43 -0
- package/lib/html-text-parser/types.d.ts +37 -19
- package/lib/http.d.ts +146 -16
- package/package.json +1 -1
- package/lib/html-text-parser/html-text-parser.d.ts +0 -70
- package/lib/html-text-parser/methods.d.ts +0 -51
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the keys whose values are considered empty.
|
|
3
|
+
*
|
|
4
|
+
* Empty values are:
|
|
5
|
+
* - empty string (`''`)
|
|
6
|
+
* - `null`
|
|
7
|
+
* - `undefined`
|
|
8
|
+
*
|
|
9
|
+
* @template T Object type to inspect.
|
|
10
|
+
* @param obj Object to scan.
|
|
11
|
+
* @returns Array of keys from `obj` whose values are empty.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* getEmptyKeys({ title: '', count: 0, note: undefined });
|
|
15
|
+
* // ['title', 'note']
|
|
16
|
+
*/
|
|
17
|
+
export declare function getEmptyKeys<T extends object>(obj: T): (keyof T)[];
|
|
18
|
+
/**
|
|
19
|
+
* Joins an array of strings into a human-readable sentence.
|
|
20
|
+
*
|
|
21
|
+
* Example:
|
|
22
|
+
* ```ts
|
|
23
|
+
* joinToSentence(["Title", "Content"]) // "Title and Content"
|
|
24
|
+
* joinToSentence(["Title", "Content", "Author"]) // "Title, Content and Author"
|
|
25
|
+
* ```
|
|
26
|
+
* @param arr - Array of strings to join
|
|
27
|
+
* @returns A human-readable sentence
|
|
28
|
+
*/
|
|
29
|
+
export declare function joinToSentence(arr: string[]): string;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { setCssClassPrefix } from './constant';
|
|
2
|
+
import { appendToDom } from './dom-renderer';
|
|
3
|
+
import { generateStylesheet, getStylesheet, setOverrideStyles } from './stylesheet';
|
|
4
|
+
import { groupSegments, parseSegments } from './parsing';
|
|
5
|
+
import { removeBlockTag, setBlockTag, setSelfClosingTag, setTag } from './tags';
|
|
6
|
+
import { BlockGroup, TextSegment, RichTextOverrideStyles } from './types';
|
|
7
|
+
type _TextSegment = TextSegment;
|
|
8
|
+
type _BlockGroup = BlockGroup;
|
|
9
|
+
type _RichTextOverrideStyles = RichTextOverrideStyles;
|
|
10
|
+
export interface HtmlTextParser {
|
|
11
|
+
/**
|
|
12
|
+
* Parse an input string and append safe DOM nodes into the target element.
|
|
13
|
+
* This avoids `innerHTML` and only creates known elements explicitly.
|
|
14
|
+
*/
|
|
15
|
+
appendToDom: typeof appendToDom;
|
|
16
|
+
/** Parse input into flat text segments with active tag context. */
|
|
17
|
+
parseSegments: typeof parseSegments;
|
|
18
|
+
/** Group flat segments by their active block/container tags. */
|
|
19
|
+
groupSegments: typeof groupSegments;
|
|
20
|
+
/** Register custom inline/block tags and optional style mappings. */
|
|
21
|
+
setTag: typeof setTag;
|
|
22
|
+
/** Register custom self-closing tags and optional style mappings. */
|
|
23
|
+
setSelfClosingTag: typeof setSelfClosingTag;
|
|
24
|
+
/** Mark a tag as block/container. */
|
|
25
|
+
setBlockTag: typeof setBlockTag;
|
|
26
|
+
/** Remove block/container behavior for a tag. */
|
|
27
|
+
removeBlockTag: typeof removeBlockTag;
|
|
28
|
+
/** Set the CSS class prefix used by generated classes (default: `rtp-`). */
|
|
29
|
+
setCssClassPrefix: typeof setCssClassPrefix;
|
|
30
|
+
/** Get the generated stylesheet content as a string. */
|
|
31
|
+
getStylesheet: typeof getStylesheet;
|
|
32
|
+
/** Inject or refresh the parser stylesheet in `document.head`. */
|
|
33
|
+
generateStylesheet: typeof generateStylesheet;
|
|
34
|
+
/** Override per-tag stylesheet fragments (merged with `!important`). */
|
|
35
|
+
overrideStyles: typeof setOverrideStyles;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Rich text parser toolkit.
|
|
39
|
+
* Useful for dialog or toast content, or any time you want to allow rich text input without trusting raw HTML.
|
|
40
|
+
*
|
|
41
|
+
* Includes safe DOM rendering (`appendToDom`), parsing utilities,
|
|
42
|
+
* tag registration, and stylesheet customization methods.
|
|
43
|
+
*/
|
|
44
|
+
export declare const htmlTextParser: HtmlTextParser;
|
|
45
|
+
/**
|
|
46
|
+
* Namespace-style type access for html-text-parser.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* const segment: HtmlTextParser.TextSegment = {
|
|
50
|
+
* text: 'Hello',
|
|
51
|
+
* tagNames: '',
|
|
52
|
+
* tagNamesList: [],
|
|
53
|
+
* containerTagNames: [],
|
|
54
|
+
* cssClass: '',
|
|
55
|
+
* };
|
|
56
|
+
*/
|
|
57
|
+
export declare namespace HtmlTextParser {
|
|
58
|
+
type TextSegment = _TextSegment;
|
|
59
|
+
type BlockGroup = _BlockGroup;
|
|
60
|
+
type OverrideStyles = _RichTextOverrideStyles;
|
|
61
|
+
}
|
|
62
|
+
export {};
|
|
@@ -16,4 +16,3 @@ export declare function setCssClassPrefix(prefix: string): void;
|
|
|
16
16
|
* Example: `<br/>` creates an empty segment with the `br` style key.
|
|
17
17
|
*/
|
|
18
18
|
export declare const SELF_CLOSING_TAGS: Map<string, string>;
|
|
19
|
-
export declare const styleTagMap: Map<string, Map<string, string>>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a rich-text string and appends the result as real DOM nodes.
|
|
3
|
+
* This is a **safe alternative to `innerHTML`** — no HTML parsing happens in the browser.
|
|
4
|
+
*
|
|
5
|
+
* Supported tags: `<b>/<strong>`, `<i>/<em>`, `<u>`, `<s>`, `<mark>`, `<small>`,
|
|
6
|
+
* `<sup>`, `<sub>`, `<h1>`–`<h6>`, `<code>`, `<span>`, `<p>`, `<div>`,
|
|
7
|
+
* `<ul>`, `<ol>`, `<li>`, `<br/>`, `<hr/>`
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // replaces: element.innerHTML = 'Hello <b>world</b>!';
|
|
11
|
+
* htmlTextParser.appendToDom(element, 'Hello <b>world</b>!');
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Disable generated classes entirely
|
|
15
|
+
* htmlTextParser.appendToDom(element, '<mark>highlight</mark>', { useClasses: false });
|
|
16
|
+
*/
|
|
17
|
+
export declare function appendToDom<T extends object>(htmlElement: T, input: string): void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export
|
|
1
|
+
export * from './api';
|
|
2
|
+
export * from './types';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BlockGroup, TextSegment } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Parses a rich text string into a flat array of typed segments.
|
|
4
|
+
* Each segment carries its text, active tag names, CSS class, and inline style info.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* parseSegments('Hello <b>world</b>!')
|
|
8
|
+
* // [
|
|
9
|
+
* // { text: 'Hello ', tagNamesList: [], cssClass: '' },
|
|
10
|
+
* // { text: 'world', tagNamesList: ['b'], cssClass: 'rtp-b' },
|
|
11
|
+
* // { text: '!', tagNamesList: [], cssClass: '' },
|
|
12
|
+
* // ]
|
|
13
|
+
*/
|
|
14
|
+
export declare function parseSegments(input: string): TextSegment[];
|
|
15
|
+
/**
|
|
16
|
+
* Groups a flat segment array by block/container context.
|
|
17
|
+
* Consecutive segments sharing the same container tags are merged into one `BlockGroup`.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* groupSegments(parseSegments('<p>Hello <b>world</b></p> outside'))
|
|
21
|
+
* // [
|
|
22
|
+
* // { containerTagNames: ['p'], segments: [...] },
|
|
23
|
+
* // { containerTagNames: [], segments: [...] },
|
|
24
|
+
* // ]
|
|
25
|
+
*/
|
|
26
|
+
export declare function groupSegments(segments: TextSegment[]): BlockGroup[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { RichTextOverrideStyles } from './types';
|
|
2
|
+
/** Returns the full CSS stylesheet string for all registered tags. */
|
|
3
|
+
export declare function getStylesheet(): string;
|
|
4
|
+
/**
|
|
5
|
+
* Injects (or refreshes) a `<style>` tag in the document `<head>`.
|
|
6
|
+
* Safe to call multiple times — updates the existing tag if already present.
|
|
7
|
+
*/
|
|
8
|
+
export declare function generateStylesheet(): void;
|
|
9
|
+
/**
|
|
10
|
+
* Overrides default CSS for specific tags.
|
|
11
|
+
* Values are CSS property strings, e.g. `"color: red; font-size: 1.2em;"`.
|
|
12
|
+
* All overrides are automatically marked `!important`.
|
|
13
|
+
* Calling this in a browser re-injects the stylesheet immediately.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* setOverrideStyles({
|
|
17
|
+
* mark: 'background-color: pink; color: black;',
|
|
18
|
+
* code: 'font-family: "Fira Code", monospace;',
|
|
19
|
+
* });
|
|
20
|
+
*/
|
|
21
|
+
export declare function setOverrideStyles(newStyles: RichTextOverrideStyles): void;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
type TagConfig = {
|
|
2
|
+
tag: string;
|
|
3
|
+
tagName?: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Register a new supported tag for the parser.
|
|
7
|
+
*
|
|
8
|
+
* Use this when you want the parser to recognize custom tags and optionally map
|
|
9
|
+
* them to another normalized tag key.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* registerTag({ tag: 'kbd' });
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* registerTag([
|
|
16
|
+
* { tag: 'kbd' },
|
|
17
|
+
* { tag: 'del', tagName: 's' },
|
|
18
|
+
* ]);
|
|
19
|
+
*/
|
|
20
|
+
export declare function setTag(tag: TagConfig): void;
|
|
21
|
+
export declare function setTag(tags: TagConfig[]): void;
|
|
22
|
+
/**
|
|
23
|
+
* Register a self-closing tag (for example `<br/>`, `<hr/>`) so it can be
|
|
24
|
+
* parsed as an element even when there is no text content.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* setSelfClosingTag({
|
|
28
|
+
* tag: 'divider',
|
|
29
|
+
* tagName: 'hr',
|
|
30
|
+
* });
|
|
31
|
+
*/
|
|
32
|
+
export declare function setSelfClosingTag(tag: TagConfig): void;
|
|
33
|
+
export declare function setSelfClosingTag(tags: TagConfig[]): void;
|
|
34
|
+
/**
|
|
35
|
+
* Mark a tag as a block/container tag.
|
|
36
|
+
* Block tags wrap grouped segments (e.g. `p`, `div`, `ul`, `li`).
|
|
37
|
+
*/
|
|
38
|
+
export declare function setBlockTag(tag: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* Remove a tag from block/container behavior and treat it as inline.
|
|
41
|
+
*/
|
|
42
|
+
export declare function removeBlockTag(tag: string): void;
|
|
43
|
+
export {};
|
|
@@ -1,35 +1,53 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* A parsed run of text with its active tag context.
|
|
3
3
|
*/
|
|
4
4
|
export interface TextSegment {
|
|
5
|
-
/**
|
|
5
|
+
/** Plain text content of this run */
|
|
6
6
|
text: string;
|
|
7
|
-
/**
|
|
7
|
+
/** All active tag names as a comma-separated string (e.g. 'b,i') */
|
|
8
8
|
tagNames: string;
|
|
9
|
-
/**
|
|
9
|
+
/** All active tag names as an array (e.g. ['b', 'i']) */
|
|
10
10
|
tagNamesList: string[];
|
|
11
|
-
/**
|
|
12
|
-
style: string;
|
|
13
|
-
/** Style object — only inline tag styles, excludes block/container styles */
|
|
14
|
-
styleObject?: Record<string, string>;
|
|
15
|
-
/** Array of active block/container tag names (e.g. ['div', 'p']) */
|
|
11
|
+
/** Active block/container tag names for this run (e.g. ['div', 'p']) */
|
|
16
12
|
containerTagNames: string[];
|
|
17
|
-
/** CSS class string for inline tags
|
|
13
|
+
/** CSS class string for inline tags (e.g. 'rtp-b rtp-i') */
|
|
18
14
|
cssClass: string;
|
|
19
15
|
}
|
|
20
16
|
/**
|
|
21
|
-
* A group of
|
|
22
|
-
*
|
|
17
|
+
* A group of segments that share the same block/container context.
|
|
18
|
+
* Produced by `groupSegments()`.
|
|
23
19
|
*/
|
|
24
20
|
export interface BlockGroup {
|
|
25
|
-
/** Block
|
|
21
|
+
/** Block tag names wrapping this group (e.g. ['ul', 'li']) */
|
|
26
22
|
containerTagNames: string[];
|
|
27
|
-
/** CSS
|
|
23
|
+
/** CSS classes for the block wrapper (e.g. 'rtp-ul rtp-li') */
|
|
28
24
|
cssClass: string;
|
|
29
|
-
/**
|
|
30
|
-
style: string;
|
|
31
|
-
/** Style object for block-level tags */
|
|
32
|
-
styleObject: Record<string, string>;
|
|
33
|
-
/** The text segments inside this block group */
|
|
25
|
+
/** The text segments inside this block */
|
|
34
26
|
segments: TextSegment[];
|
|
35
27
|
}
|
|
28
|
+
/** Override default CSS for specific tags. Values are CSS property strings. */
|
|
29
|
+
export type RichTextOverrideStyles = Partial<{
|
|
30
|
+
b: string;
|
|
31
|
+
i: string;
|
|
32
|
+
u: string;
|
|
33
|
+
s: string;
|
|
34
|
+
mark: string;
|
|
35
|
+
small: string;
|
|
36
|
+
sup: string;
|
|
37
|
+
sub: string;
|
|
38
|
+
h1: string;
|
|
39
|
+
h2: string;
|
|
40
|
+
h3: string;
|
|
41
|
+
h4: string;
|
|
42
|
+
h5: string;
|
|
43
|
+
h6: string;
|
|
44
|
+
code: string;
|
|
45
|
+
span: string;
|
|
46
|
+
p: string;
|
|
47
|
+
div: string;
|
|
48
|
+
ul: string;
|
|
49
|
+
ol: string;
|
|
50
|
+
li: string;
|
|
51
|
+
br: string;
|
|
52
|
+
hr: string;
|
|
53
|
+
}>;
|
package/lib/http.d.ts
CHANGED
|
@@ -1,18 +1,148 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
statusText?: string;
|
|
4
|
-
body?: string;
|
|
5
|
-
code?: string;
|
|
6
|
-
isNetworkError?: boolean;
|
|
7
|
-
cause?: unknown;
|
|
8
|
-
};
|
|
1
|
+
export interface HttpError<TData> extends Error, _Response<TData> {
|
|
2
|
+
}
|
|
9
3
|
export interface RequestInit extends globalThis.RequestInit {
|
|
10
4
|
parse?: 'JSON' | 'TEXT' | 'BLOB' | 'ARRAYBUFFER';
|
|
11
5
|
}
|
|
6
|
+
interface _Response<T> extends Omit<globalThis.Response, 'body' | 'json' | 'text' | 'blob' | 'arrayBuffer'> {
|
|
7
|
+
data: T;
|
|
8
|
+
}
|
|
9
|
+
export type Response<T> = _Response<T>;
|
|
10
|
+
export declare namespace Http {
|
|
11
|
+
type Response<T> = _Response<T>;
|
|
12
|
+
}
|
|
13
|
+
export declare const HttpCodeNames: {
|
|
14
|
+
100: string;
|
|
15
|
+
101: string;
|
|
16
|
+
102: string;
|
|
17
|
+
103: string;
|
|
18
|
+
200: string;
|
|
19
|
+
201: string;
|
|
20
|
+
202: string;
|
|
21
|
+
203: string;
|
|
22
|
+
204: string;
|
|
23
|
+
205: string;
|
|
24
|
+
206: string;
|
|
25
|
+
207: string;
|
|
26
|
+
208: string;
|
|
27
|
+
226: string;
|
|
28
|
+
300: string;
|
|
29
|
+
301: string;
|
|
30
|
+
302: string;
|
|
31
|
+
303: string;
|
|
32
|
+
304: string;
|
|
33
|
+
305: string;
|
|
34
|
+
307: string;
|
|
35
|
+
308: string;
|
|
36
|
+
400: string;
|
|
37
|
+
401: string;
|
|
38
|
+
402: string;
|
|
39
|
+
403: string;
|
|
40
|
+
404: string;
|
|
41
|
+
405: string;
|
|
42
|
+
406: string;
|
|
43
|
+
407: string;
|
|
44
|
+
408: string;
|
|
45
|
+
409: string;
|
|
46
|
+
410: string;
|
|
47
|
+
411: string;
|
|
48
|
+
412: string;
|
|
49
|
+
413: string;
|
|
50
|
+
414: string;
|
|
51
|
+
415: string;
|
|
52
|
+
416: string;
|
|
53
|
+
417: string;
|
|
54
|
+
418: string;
|
|
55
|
+
421: string;
|
|
56
|
+
422: string;
|
|
57
|
+
423: string;
|
|
58
|
+
424: string;
|
|
59
|
+
425: string;
|
|
60
|
+
426: string;
|
|
61
|
+
428: string;
|
|
62
|
+
429: string;
|
|
63
|
+
431: string;
|
|
64
|
+
451: string;
|
|
65
|
+
500: string;
|
|
66
|
+
501: string;
|
|
67
|
+
502: string;
|
|
68
|
+
503: string;
|
|
69
|
+
504: string;
|
|
70
|
+
505: string;
|
|
71
|
+
506: string;
|
|
72
|
+
507: string;
|
|
73
|
+
508: string;
|
|
74
|
+
510: string;
|
|
75
|
+
511: string;
|
|
76
|
+
};
|
|
12
77
|
/**
|
|
13
78
|
* A utility object for making HTTP requests.
|
|
14
79
|
*/
|
|
15
|
-
export declare const
|
|
80
|
+
export declare const Http: {
|
|
81
|
+
setBaseUrl: (url: string) => void;
|
|
82
|
+
CodeNames: {
|
|
83
|
+
100: string;
|
|
84
|
+
101: string;
|
|
85
|
+
102: string;
|
|
86
|
+
103: string;
|
|
87
|
+
200: string;
|
|
88
|
+
201: string;
|
|
89
|
+
202: string;
|
|
90
|
+
203: string;
|
|
91
|
+
204: string;
|
|
92
|
+
205: string;
|
|
93
|
+
206: string;
|
|
94
|
+
207: string;
|
|
95
|
+
208: string;
|
|
96
|
+
226: string;
|
|
97
|
+
300: string;
|
|
98
|
+
301: string;
|
|
99
|
+
302: string;
|
|
100
|
+
303: string;
|
|
101
|
+
304: string;
|
|
102
|
+
305: string;
|
|
103
|
+
307: string;
|
|
104
|
+
308: string;
|
|
105
|
+
400: string;
|
|
106
|
+
401: string;
|
|
107
|
+
402: string;
|
|
108
|
+
403: string;
|
|
109
|
+
404: string;
|
|
110
|
+
405: string;
|
|
111
|
+
406: string;
|
|
112
|
+
407: string;
|
|
113
|
+
408: string;
|
|
114
|
+
409: string;
|
|
115
|
+
410: string;
|
|
116
|
+
411: string;
|
|
117
|
+
412: string;
|
|
118
|
+
413: string;
|
|
119
|
+
414: string;
|
|
120
|
+
415: string;
|
|
121
|
+
416: string;
|
|
122
|
+
417: string;
|
|
123
|
+
418: string;
|
|
124
|
+
421: string;
|
|
125
|
+
422: string;
|
|
126
|
+
423: string;
|
|
127
|
+
424: string;
|
|
128
|
+
425: string;
|
|
129
|
+
426: string;
|
|
130
|
+
428: string;
|
|
131
|
+
429: string;
|
|
132
|
+
431: string;
|
|
133
|
+
451: string;
|
|
134
|
+
500: string;
|
|
135
|
+
501: string;
|
|
136
|
+
502: string;
|
|
137
|
+
503: string;
|
|
138
|
+
504: string;
|
|
139
|
+
505: string;
|
|
140
|
+
506: string;
|
|
141
|
+
507: string;
|
|
142
|
+
508: string;
|
|
143
|
+
510: string;
|
|
144
|
+
511: string;
|
|
145
|
+
};
|
|
16
146
|
/**
|
|
17
147
|
* Makes a GET request to the specified URL.
|
|
18
148
|
*
|
|
@@ -21,7 +151,7 @@ export declare const http: {
|
|
|
21
151
|
* @returns {Promise<any>} The response data.
|
|
22
152
|
* @throws {Error} If the response is not ok.
|
|
23
153
|
*/
|
|
24
|
-
get: <R = any>(url: string, options?: RequestInit) => Promise<R
|
|
154
|
+
get: <R = any>(url: string, options?: RequestInit) => Promise<Response<R>>;
|
|
25
155
|
/**
|
|
26
156
|
* Makes a POST request to the specified URL with the given data.
|
|
27
157
|
*
|
|
@@ -32,18 +162,17 @@ export declare const http: {
|
|
|
32
162
|
* @returns {Promise<R>} The response data.
|
|
33
163
|
* @throws {Error} If the response is not ok.
|
|
34
164
|
*/
|
|
35
|
-
post: <R = any>(url: string, data: unknown, options?: RequestInit) => Promise<R
|
|
165
|
+
post: <R = any>(url: string, data: unknown, options?: RequestInit) => Promise<Response<R>>;
|
|
36
166
|
/**
|
|
37
|
-
* Makes a DELETE request to the specified URL
|
|
167
|
+
* Makes a DELETE request to the specified URL.
|
|
38
168
|
*
|
|
39
169
|
* @template R
|
|
40
170
|
* @param {string} url - The URL to send the DELETE request to.
|
|
41
|
-
* @param {string} id - The ID to send in the request body.
|
|
42
171
|
* @param {RequestInit} [options] - Optional request options.
|
|
43
172
|
* @returns {Promise<R>} The response data.
|
|
44
173
|
* @throws {Error} If the response is not ok.
|
|
45
174
|
*/
|
|
46
|
-
delete: <R = any>(url: string,
|
|
175
|
+
delete: <R = any>(url: string, options?: RequestInit) => Promise<Response<R>>;
|
|
47
176
|
/**
|
|
48
177
|
* Makes a PATCH request to the specified URL with the given data.
|
|
49
178
|
*
|
|
@@ -54,7 +183,7 @@ export declare const http: {
|
|
|
54
183
|
* @returns {Promise<R>} The response data.
|
|
55
184
|
* @throws {Error} If the response is not ok.
|
|
56
185
|
*/
|
|
57
|
-
patch: <R = any>(url: string, data: unknown, options?: RequestInit) => Promise<R
|
|
186
|
+
patch: <R = any>(url: string, data: unknown, options?: RequestInit) => Promise<Response<R>>;
|
|
58
187
|
/**
|
|
59
188
|
* Makes a PUT request to the specified URL with the given data.
|
|
60
189
|
*
|
|
@@ -65,5 +194,6 @@ export declare const http: {
|
|
|
65
194
|
* @returns {Promise<R>} The response data.
|
|
66
195
|
* @throws {Error} If the response is not ok.
|
|
67
196
|
*/
|
|
68
|
-
put: <R = any>(url: string, data: unknown, options?: RequestInit) => Promise<R
|
|
197
|
+
put: <R = any>(url: string, data: unknown, options?: RequestInit) => Promise<Response<R>>;
|
|
69
198
|
};
|
|
199
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { TextSegment, BlockGroup } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Parses a string containing supported inline tags into block groups.
|
|
4
|
-
* Supports nested tags. All other content is treated as plain text.
|
|
5
|
-
* Returns segments grouped by their block/container context.
|
|
6
|
-
*
|
|
7
|
-
* For a flat array of segments without block grouping, use `parseRichText.flat()`.
|
|
8
|
-
*
|
|
9
|
-
* Supported tags (add more in `RICH_TEXT_TAGS`):
|
|
10
|
-
* `<b>` / `<strong>`, `<i>` / `<em>`, `<u>`, `<s>`, `<mark>`, `<small>`,
|
|
11
|
-
* `<h1>` - `<h6>`, `<code>`, `<p>`, `<div>`
|
|
12
|
-
*
|
|
13
|
-
* Supported self-closing tags (add more in `SELF_CLOSING_TAGS`):
|
|
14
|
-
* `<br/>`
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* parseRichText('Hello <b>world</b>!')
|
|
18
|
-
* // [
|
|
19
|
-
* // { containerTagNames: [], cssClass: '', style: '', styleObject: {}, segments: [
|
|
20
|
-
* // { text: 'Hello ', ... },
|
|
21
|
-
* // { text: 'world', ... },
|
|
22
|
-
* // { text: '!', ... },
|
|
23
|
-
* // ]},
|
|
24
|
-
* // ]
|
|
25
|
-
*/
|
|
26
|
-
export declare function parseRichText(input: string): BlockGroup[];
|
|
27
|
-
export declare namespace parseRichText {
|
|
28
|
-
var setTag: typeof setRichTextTag;
|
|
29
|
-
var setSelfClosingTag: typeof import("./html-text-parser").setSelfClosingTag;
|
|
30
|
-
var setBlockTag: (tag: string) => Set<string>;
|
|
31
|
-
var removeBlockTag: (tag: string) => boolean;
|
|
32
|
-
var setCssClassPrefix: typeof import("./constant").setCssClassPrefix;
|
|
33
|
-
var getStylesheet: typeof import("./methods").getStylesheet;
|
|
34
|
-
var flat: typeof parseRichTextFlat;
|
|
35
|
-
var generateStylesheet: typeof import("./methods").generateStylesheet;
|
|
36
|
-
var appendToDom: typeof import("./methods").appendToDom;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Parses a string containing supported inline tags into a flat array of typed segments.
|
|
40
|
-
* Supports nested tags. All other content is treated as plain text.
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* parseRichTextFlat('Hello <b>world</b>!')
|
|
44
|
-
* // [
|
|
45
|
-
* // { text: 'Hello ', tagNames: '', cssClass: '' },
|
|
46
|
-
* // { text: 'world', tagNames: 'b', cssClass: 'rtp-b' },
|
|
47
|
-
* // { text: '!', tagNames: '', cssClass: '' },
|
|
48
|
-
* // ]
|
|
49
|
-
*/
|
|
50
|
-
export declare function parseRichTextFlat(input: string): TextSegment[];
|
|
51
|
-
export declare function setRichTextTag(tag: {
|
|
52
|
-
tag: string;
|
|
53
|
-
tagName?: string;
|
|
54
|
-
style?: Record<string, string>;
|
|
55
|
-
}): void;
|
|
56
|
-
export declare function setRichTextTag(tags: {
|
|
57
|
-
tag: string;
|
|
58
|
-
tagName?: string;
|
|
59
|
-
style?: Record<string, string>;
|
|
60
|
-
}[]): void;
|
|
61
|
-
export declare function setSelfClosingTag(tag: {
|
|
62
|
-
tag: string;
|
|
63
|
-
tagName?: string;
|
|
64
|
-
style?: Record<string, string>;
|
|
65
|
-
}): void;
|
|
66
|
-
export declare function setSelfClosingTag(tags: {
|
|
67
|
-
tag: string;
|
|
68
|
-
tagName?: string;
|
|
69
|
-
style?: Record<string, string>;
|
|
70
|
-
}[]): void;
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { TextSegment, BlockGroup } from './types';
|
|
2
|
-
export declare const createTagSegment: (activeTagsKeys: string[], input: string, lastIndex: number, index: number, text?: string) => TextSegment;
|
|
3
|
-
export declare const getTagNames: () => string;
|
|
4
|
-
export declare const getSelfClosingTagNames: () => string;
|
|
5
|
-
export declare const getTagRegex: () => RegExp;
|
|
6
|
-
export declare function setStyleMap(key: string, style: Record<string, string>): void;
|
|
7
|
-
/**
|
|
8
|
-
* Generates a CSS stylesheet string from all registered tags and their styles.
|
|
9
|
-
* Each tag gets a class like `.rtp-b { font-weight: bold; }`.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* const css = generateStylesheet();
|
|
13
|
-
* // '.rtp-b { font-weight: bold; }\n.rtp-i { font-style: italic; }\n...'
|
|
14
|
-
*/
|
|
15
|
-
export declare function getStylesheet(): string;
|
|
16
|
-
export declare function generateStylesheet(): void;
|
|
17
|
-
/**
|
|
18
|
-
* Groups a flat array of segments by their block/container context.
|
|
19
|
-
* Consecutive segments with the same `containerTagNames` are merged into one `BlockGroup`.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* const groups = groupByBlocks(parseRichText('<div>Hello <b>world</b></div> outside'));
|
|
23
|
-
* // [
|
|
24
|
-
* // { containerTagNames: ['div'], cssClass: 'rtp-div', segments: [...] },
|
|
25
|
-
* // { containerTagNames: [], cssClass: '', segments: [...] },
|
|
26
|
-
* // ]
|
|
27
|
-
*/
|
|
28
|
-
export declare function groupByBlocks(segments: TextSegment[]): BlockGroup[];
|
|
29
|
-
/**
|
|
30
|
-
* Appends parsed rich text segments to a DOM element, creating nested elements
|
|
31
|
-
* for each block group and its inline segments.
|
|
32
|
-
*
|
|
33
|
-
* @param htmlElement - The target DOM element to append content to. Must support `appendChild`.
|
|
34
|
-
* @param segments - An array of `BlockGroup` objects (e.g. from `groupByBlocks`).
|
|
35
|
-
* @param config - Optional configuration for styling behavior.
|
|
36
|
-
* @param config.useClasses - Whether to apply CSS class names to elements. Defaults to `true`.
|
|
37
|
-
* @param config.useStyles - Whether to apply inline styles to elements. Defaults to `false`.
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* const el = document.getElementById('output');
|
|
41
|
-
* const groups = groupByBlocks(parseRichText('<b>Hello</b> <i>world</i>'));
|
|
42
|
-
* appendToDom(el, groups);
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* // With inline styles instead of classes
|
|
46
|
-
* appendToDom(el, groups, { useClasses: false, useStyles: true });
|
|
47
|
-
*/
|
|
48
|
-
export declare function appendToDom<T extends object>(htmlElement: T, segments: BlockGroup[], config?: {
|
|
49
|
-
useClasses?: boolean;
|
|
50
|
-
useStyles?: boolean;
|
|
51
|
-
}): void;
|