@contello/rich-text 8.21.0 → 8.21.2

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 ADDED
@@ -0,0 +1,69 @@
1
+ # @contello/rich-text
2
+
3
+ TypeScript types and helpers for working with Contello CMS rich text documents (TipTap/ProseMirror JSON format).
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @contello/rich-text
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Parsing a rich text document
14
+
15
+ ```ts
16
+ import { parseRichTextDocument } from '@contello/rich-text';
17
+
18
+ const doc = parseRichTextDocument(jsonString);
19
+ ```
20
+
21
+ ### Converting to plain text
22
+
23
+ ```ts
24
+ import { richTextDocumentToString } from '@contello/rich-text';
25
+
26
+ const text = richTextDocumentToString(doc);
27
+ ```
28
+
29
+ ### Checking if a document is empty
30
+
31
+ ```ts
32
+ import { isRichTextDocumentEmpty } from '@contello/rich-text';
33
+
34
+ if (isRichTextDocumentEmpty(doc)) {
35
+ // no content
36
+ }
37
+ ```
38
+
39
+ ### Creating a document from a string
40
+
41
+ ```ts
42
+ import { createRichTextDocumentFromString } from '@contello/rich-text';
43
+
44
+ const doc = createRichTextDocumentFromString('Hello world');
45
+ ```
46
+
47
+ ## API
48
+
49
+ ### Types
50
+
51
+ - `RichTextDocument` — root document node (`type: 'doc'`)
52
+ - `RichTextNode` — union of all block and inline node types
53
+ - `RichTextMark` — union of all mark types (bold, italic, link, etc.)
54
+ - `Maybe<T>` — `T | null | undefined`
55
+
56
+ ### Helpers
57
+
58
+ | Function | Description |
59
+ |----------|-------------|
60
+ | `parseRichTextDocument(text)` | Parse a JSON string into a `RichTextDocument`, returns empty doc on failure |
61
+ | `richTextDocumentToString(doc)` | Convert a document to plain text |
62
+ | `richTextNodesToString(nodes)` | Convert an array of nodes to plain text |
63
+ | `richTextNodeToString(node)` | Convert a single node to plain text |
64
+ | `isRichTextDocumentEmpty(doc)` | Check if a document has no meaningful content |
65
+ | `createRichTextDocumentFromString(text)` | Create a document with a single paragraph from a string |
66
+
67
+ ## License
68
+
69
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createRichTextDocumentFromString: () => createRichTextDocumentFromString,
24
+ isRichTextDocumentEmpty: () => isRichTextDocumentEmpty,
25
+ parseRichTextDocument: () => parseRichTextDocument,
26
+ richTextDocumentToString: () => richTextDocumentToString,
27
+ richTextNodeToString: () => richTextNodeToString,
28
+ richTextNodesToString: () => richTextNodesToString
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+
32
+ // src/helpers.ts
33
+ function richTextNodeToString(node) {
34
+ if (node.type === "text") {
35
+ return node.text;
36
+ }
37
+ switch (node.type) {
38
+ case "heading":
39
+ case "paragraph":
40
+ case "codeBlock":
41
+ case "blockquote":
42
+ return (node.content ?? []).map(richTextNodeToString).join("");
43
+ case "bulletList":
44
+ case "orderedList":
45
+ return (node.content ?? []).map((item) => (item.content ?? []).map(richTextNodeToString).join("")).join("");
46
+ case "listItem":
47
+ return (node.content ?? []).map(richTextNodeToString).join("");
48
+ case "horizontalRule":
49
+ return "---";
50
+ case "hardBreak":
51
+ return "\n";
52
+ case "table":
53
+ return (node.content ?? []).map(
54
+ (row) => (row.content ?? []).map((cell) => (cell.content ?? []).map(richTextNodeToString).join("")).join(" | ")
55
+ ).join("\n");
56
+ case "tableRow":
57
+ return (node.content ?? []).map((cell) => (cell.content ?? []).map(richTextNodeToString).join("")).join(" | ");
58
+ case "tableCell":
59
+ return (node.content ?? []).map(richTextNodeToString).join("");
60
+ case "tableHeader":
61
+ return (node.content ?? []).map(richTextNodeToString).join("");
62
+ }
63
+ }
64
+ function richTextNodesToString(nodes) {
65
+ return nodes.map(richTextNodeToString).join("\n");
66
+ }
67
+ function richTextDocumentToString(document) {
68
+ return richTextNodesToString(document.content ?? []);
69
+ }
70
+ function isRichTextDocumentEmpty(document) {
71
+ return (document.content ?? []).length === 0 || (document.content ?? []).every((node) => richTextNodeToString(node).trim() === "");
72
+ }
73
+ function createRichTextDocumentFromString(text) {
74
+ return {
75
+ type: "doc",
76
+ content: [
77
+ {
78
+ type: "paragraph",
79
+ content: text ? [{ type: "text", text }] : []
80
+ }
81
+ ]
82
+ };
83
+ }
84
+ function createEmptyRichTextDocument() {
85
+ return {
86
+ type: "doc",
87
+ content: []
88
+ };
89
+ }
90
+ function parseRichTextDocument(text) {
91
+ if (!text) {
92
+ return createEmptyRichTextDocument();
93
+ }
94
+ const parsed = JSON.parse(text);
95
+ if (parsed.type !== "doc") {
96
+ return createEmptyRichTextDocument();
97
+ }
98
+ return parsed;
99
+ }
100
+ // Annotate the CommonJS export names for ESM import in node:
101
+ 0 && (module.exports = {
102
+ createRichTextDocumentFromString,
103
+ isRichTextDocumentEmpty,
104
+ parseRichTextDocument,
105
+ richTextDocumentToString,
106
+ richTextNodeToString,
107
+ richTextNodesToString
108
+ });
@@ -0,0 +1,125 @@
1
+ type RichTextDocument = {
2
+ type: 'doc';
3
+ content?: Maybe<RichTextNode[]>;
4
+ };
5
+ type RichTextNode = RichTextHeading | RichTextParagraph | RichTextCodeBlock | RichTextBlockquote | RichTextHorizontalRule | RichTextHardBreak | RichTextBulletList | RichTextOrderedList | RichTextListItem | RichTextTable | RichTextTableRow | RichTextTableHeader | RichTextTableCell | RichTextText;
6
+ type RichTextHeading = {
7
+ type: 'heading';
8
+ attrs?: Maybe<{
9
+ textAlign?: Maybe<RichTextTextAlign>;
10
+ level?: Maybe<1 | 2 | 3 | 4 | 5 | 6>;
11
+ }>;
12
+ content?: Maybe<RichTextNode[]>;
13
+ };
14
+ type RichTextParagraph = {
15
+ type: 'paragraph';
16
+ attrs?: Maybe<{
17
+ textAlign?: Maybe<RichTextTextAlign>;
18
+ }>;
19
+ content?: Maybe<RichTextNode[]>;
20
+ };
21
+ type RichTextCodeBlock = {
22
+ type: 'codeBlock';
23
+ content?: Maybe<RichTextText[]>;
24
+ };
25
+ type RichTextBlockquote = {
26
+ type: 'blockquote';
27
+ content?: Maybe<RichTextText[]>;
28
+ };
29
+ type RichTextBulletList = {
30
+ type: 'bulletList';
31
+ content?: Maybe<RichTextListItem[]>;
32
+ };
33
+ type RichTextOrderedList = {
34
+ type: 'orderedList';
35
+ attrs?: Maybe<{
36
+ start?: Maybe<number>;
37
+ }>;
38
+ content?: Maybe<RichTextListItem[]>;
39
+ };
40
+ type RichTextListItem = {
41
+ type: 'listItem';
42
+ content?: Maybe<RichTextNode[]>;
43
+ };
44
+ type RichTextTable = {
45
+ type: 'table';
46
+ content?: Maybe<RichTextTableRow[]>;
47
+ };
48
+ type RichTextTableRow = {
49
+ type: 'tableRow';
50
+ content?: Maybe<(RichTextTableHeader | RichTextTableCell)[]>;
51
+ };
52
+ type RichTextTableHeader = {
53
+ type: 'tableHeader';
54
+ attrs?: Maybe<{
55
+ colspan: Maybe<number>;
56
+ rowspan: Maybe<number>;
57
+ colwidth: Maybe<number[]>;
58
+ }>;
59
+ content?: Maybe<RichTextNode[]>;
60
+ };
61
+ type RichTextTableCell = {
62
+ type: 'tableCell';
63
+ attrs?: Maybe<{
64
+ colspan: Maybe<number>;
65
+ rowspan: Maybe<number>;
66
+ colwidth: Maybe<number[]>;
67
+ }>;
68
+ content?: Maybe<RichTextNode[]>;
69
+ };
70
+ type RichTextHorizontalRule = {
71
+ type: 'horizontalRule';
72
+ };
73
+ type RichTextHardBreak = {
74
+ type: 'hardBreak';
75
+ };
76
+ type RichTextText = {
77
+ type: 'text';
78
+ text: string;
79
+ marks?: Maybe<RichTextMark[]>;
80
+ };
81
+ type RichTextUnderlineMark = {
82
+ type: 'underline';
83
+ };
84
+ type RichTextBoldMark = {
85
+ type: 'bold';
86
+ };
87
+ type RichTextItalicMark = {
88
+ type: 'italic';
89
+ };
90
+ type RichTextCodeMark = {
91
+ type: 'code';
92
+ };
93
+ type RichTextStrikeMark = {
94
+ type: 'strike';
95
+ };
96
+ type RichTextTextStyleMark = {
97
+ type: 'textStyle';
98
+ attrs?: Maybe<{
99
+ color?: Maybe<string>;
100
+ }>;
101
+ };
102
+ type RichTextLink<T extends Record<string, any> = Record<string, any>> = {
103
+ type: 'link';
104
+ attrs?: Maybe<{
105
+ data?: Maybe<T>;
106
+ }>;
107
+ };
108
+ type RichTextMark = RichTextUnderlineMark | RichTextBoldMark | RichTextItalicMark | RichTextCodeMark | RichTextStrikeMark | RichTextTextStyleMark | RichTextLink;
109
+ type RichTextTextAlign = 'left' | 'center' | 'right';
110
+ type Maybe<T> = T | null | undefined;
111
+
112
+ /** Converts a single rich text node to its plain text representation. */
113
+ declare function richTextNodeToString(node: RichTextNode): string;
114
+ /** Converts an array of rich text nodes to plain text, joining them with newlines. */
115
+ declare function richTextNodesToString(nodes: RichTextNode[]): string;
116
+ /** Converts a rich text document to its plain text representation. */
117
+ declare function richTextDocumentToString(document: RichTextDocument): string;
118
+ /** Returns `true` if the document has no content or only whitespace. */
119
+ declare function isRichTextDocumentEmpty(document: RichTextDocument): boolean;
120
+ /** Creates a rich text document containing a single paragraph with the given text. */
121
+ declare function createRichTextDocumentFromString(text: string | null | undefined): RichTextDocument;
122
+ /** Parses a JSON string into a {@link RichTextDocument}. Returns an empty document if parsing fails or the input is nullish. */
123
+ declare function parseRichTextDocument(text: string | null | undefined): RichTextDocument;
124
+
125
+ export { type Maybe, type RichTextBlockquote, type RichTextBoldMark, type RichTextBulletList, type RichTextCodeBlock, type RichTextCodeMark, type RichTextDocument, type RichTextHardBreak, type RichTextHeading, type RichTextHorizontalRule, type RichTextItalicMark, type RichTextLink, type RichTextListItem, type RichTextMark, type RichTextNode, type RichTextOrderedList, type RichTextParagraph, type RichTextStrikeMark, type RichTextTable, type RichTextTableCell, type RichTextTableHeader, type RichTextTableRow, type RichTextText, type RichTextTextAlign, type RichTextTextStyleMark, type RichTextUnderlineMark, createRichTextDocumentFromString, isRichTextDocumentEmpty, parseRichTextDocument, richTextDocumentToString, richTextNodeToString, richTextNodesToString };
package/dist/index.d.ts CHANGED
@@ -1,149 +1,125 @@
1
- export declare function createRichTextDocumentFromString(text: string | null | undefined): RichTextDocument;
2
-
3
- export declare function isRichTextDocumentEmpty(document: RichTextDocument): boolean;
4
-
5
- export declare type Maybe<T> = T | null | undefined;
6
-
7
- export declare function parseRichTextDocument(text: string | null | undefined): RichTextDocument;
8
-
9
- export declare type RichTextBlockquote = {
10
- type: 'blockquote';
11
- content?: Maybe<RichTextText[]>;
12
- };
13
-
14
- export declare type RichTextBoldMark = {
15
- type: 'bold';
16
- };
17
-
18
- export declare type RichTextBulletList = {
19
- type: 'bulletList';
20
- content?: Maybe<RichTextListItem[]>;
21
- };
22
-
23
- export declare type RichTextCodeBlock = {
24
- type: 'codeBlock';
25
- content?: Maybe<RichTextText[]>;
26
- };
27
-
28
- export declare type RichTextCodeMark = {
29
- type: 'code';
30
- };
31
-
32
- export declare type RichTextDocument = {
33
- type: 'doc';
34
- content?: Maybe<RichTextNode[]>;
35
- };
36
-
37
- export declare function richTextDocumentToString(document: RichTextDocument): string;
38
-
39
- export declare type RichTextHardBreak = {
40
- type: 'hardBreak';
41
- };
42
-
43
- export declare type RichTextHeading = {
44
- type: 'heading';
45
- attrs?: Maybe<{
46
- textAlign?: Maybe<RichTextTextAlign>;
47
- level?: Maybe<1 | 2 | 3 | 4 | 5 | 6>;
48
- }>;
49
- content?: Maybe<RichTextNode[]>;
50
- };
51
-
52
- export declare type RichTextHorizontalRule = {
53
- type: 'horizontalRule';
54
- };
55
-
56
- export declare type RichTextItalicMark = {
57
- type: 'italic';
58
- };
59
-
60
- export declare type RichTextLink<T extends Record<string, any> = Record<string, any>> = {
61
- type: 'link';
62
- attrs?: Maybe<{
63
- data?: Maybe<T>;
64
- }>;
65
- };
66
-
67
- export declare type RichTextListItem = {
68
- type: 'listItem';
69
- content?: Maybe<RichTextNode[]>;
70
- };
71
-
72
- export declare type RichTextMark = RichTextUnderlineMark | RichTextBoldMark | RichTextItalicMark | RichTextCodeMark | RichTextStrikeMark | RichTextTextStyleMark | RichTextLink;
73
-
74
- export declare type RichTextNode = RichTextHeading | RichTextParagraph | RichTextCodeBlock | RichTextBlockquote | RichTextHorizontalRule | RichTextHardBreak | RichTextBulletList | RichTextOrderedList | RichTextListItem | RichTextTable | RichTextTableRow | RichTextTableHeader | RichTextTableCell | RichTextText;
75
-
76
- export declare function richTextNodesToString(nodes: RichTextNode[]): string;
77
-
78
- export declare function richTextNodeToString(node: RichTextNode): string;
79
-
80
- export declare type RichTextOrderedList = {
81
- type: 'orderedList';
82
- attrs?: Maybe<{
83
- start?: Maybe<number>;
84
- }>;
85
- content?: Maybe<RichTextListItem[]>;
86
- };
87
-
88
- export declare type RichTextParagraph = {
89
- type: 'paragraph';
90
- attrs?: Maybe<{
91
- textAlign?: Maybe<RichTextTextAlign>;
92
- }>;
93
- content?: Maybe<RichTextNode[]>;
94
- };
95
-
96
- export declare type RichTextStrikeMark = {
97
- type: 'strike';
98
- };
99
-
100
- export declare type RichTextTable = {
101
- type: 'table';
102
- content?: Maybe<RichTextTableRow[]>;
103
- };
104
-
105
- export declare type RichTextTableCell = {
106
- type: 'tableCell';
107
- attrs?: Maybe<{
108
- colspan: Maybe<number>;
109
- rowspan: Maybe<number>;
110
- colwidth: Maybe<number[]>;
111
- }>;
112
- content?: Maybe<RichTextNode[]>;
113
- };
114
-
115
- export declare type RichTextTableHeader = {
116
- type: 'tableHeader';
117
- attrs?: Maybe<{
118
- colspan: Maybe<number>;
119
- rowspan: Maybe<number>;
120
- colwidth: Maybe<number[]>;
121
- }>;
122
- content?: Maybe<RichTextNode[]>;
123
- };
124
-
125
- export declare type RichTextTableRow = {
126
- type: 'tableRow';
127
- content?: Maybe<(RichTextTableHeader | RichTextTableCell)[]>;
128
- };
129
-
130
- export declare type RichTextText = {
131
- type: 'text';
132
- text: string;
133
- marks?: Maybe<RichTextMark[]>;
134
- };
135
-
136
- export declare type RichTextTextAlign = 'left' | 'center' | 'right';
137
-
138
- export declare type RichTextTextStyleMark = {
139
- type: 'textStyle';
140
- attrs?: Maybe<{
141
- color?: Maybe<string>;
142
- }>;
143
- };
144
-
145
- export declare type RichTextUnderlineMark = {
146
- type: 'underline';
147
- };
148
-
149
- export { }
1
+ type RichTextDocument = {
2
+ type: 'doc';
3
+ content?: Maybe<RichTextNode[]>;
4
+ };
5
+ type RichTextNode = RichTextHeading | RichTextParagraph | RichTextCodeBlock | RichTextBlockquote | RichTextHorizontalRule | RichTextHardBreak | RichTextBulletList | RichTextOrderedList | RichTextListItem | RichTextTable | RichTextTableRow | RichTextTableHeader | RichTextTableCell | RichTextText;
6
+ type RichTextHeading = {
7
+ type: 'heading';
8
+ attrs?: Maybe<{
9
+ textAlign?: Maybe<RichTextTextAlign>;
10
+ level?: Maybe<1 | 2 | 3 | 4 | 5 | 6>;
11
+ }>;
12
+ content?: Maybe<RichTextNode[]>;
13
+ };
14
+ type RichTextParagraph = {
15
+ type: 'paragraph';
16
+ attrs?: Maybe<{
17
+ textAlign?: Maybe<RichTextTextAlign>;
18
+ }>;
19
+ content?: Maybe<RichTextNode[]>;
20
+ };
21
+ type RichTextCodeBlock = {
22
+ type: 'codeBlock';
23
+ content?: Maybe<RichTextText[]>;
24
+ };
25
+ type RichTextBlockquote = {
26
+ type: 'blockquote';
27
+ content?: Maybe<RichTextText[]>;
28
+ };
29
+ type RichTextBulletList = {
30
+ type: 'bulletList';
31
+ content?: Maybe<RichTextListItem[]>;
32
+ };
33
+ type RichTextOrderedList = {
34
+ type: 'orderedList';
35
+ attrs?: Maybe<{
36
+ start?: Maybe<number>;
37
+ }>;
38
+ content?: Maybe<RichTextListItem[]>;
39
+ };
40
+ type RichTextListItem = {
41
+ type: 'listItem';
42
+ content?: Maybe<RichTextNode[]>;
43
+ };
44
+ type RichTextTable = {
45
+ type: 'table';
46
+ content?: Maybe<RichTextTableRow[]>;
47
+ };
48
+ type RichTextTableRow = {
49
+ type: 'tableRow';
50
+ content?: Maybe<(RichTextTableHeader | RichTextTableCell)[]>;
51
+ };
52
+ type RichTextTableHeader = {
53
+ type: 'tableHeader';
54
+ attrs?: Maybe<{
55
+ colspan: Maybe<number>;
56
+ rowspan: Maybe<number>;
57
+ colwidth: Maybe<number[]>;
58
+ }>;
59
+ content?: Maybe<RichTextNode[]>;
60
+ };
61
+ type RichTextTableCell = {
62
+ type: 'tableCell';
63
+ attrs?: Maybe<{
64
+ colspan: Maybe<number>;
65
+ rowspan: Maybe<number>;
66
+ colwidth: Maybe<number[]>;
67
+ }>;
68
+ content?: Maybe<RichTextNode[]>;
69
+ };
70
+ type RichTextHorizontalRule = {
71
+ type: 'horizontalRule';
72
+ };
73
+ type RichTextHardBreak = {
74
+ type: 'hardBreak';
75
+ };
76
+ type RichTextText = {
77
+ type: 'text';
78
+ text: string;
79
+ marks?: Maybe<RichTextMark[]>;
80
+ };
81
+ type RichTextUnderlineMark = {
82
+ type: 'underline';
83
+ };
84
+ type RichTextBoldMark = {
85
+ type: 'bold';
86
+ };
87
+ type RichTextItalicMark = {
88
+ type: 'italic';
89
+ };
90
+ type RichTextCodeMark = {
91
+ type: 'code';
92
+ };
93
+ type RichTextStrikeMark = {
94
+ type: 'strike';
95
+ };
96
+ type RichTextTextStyleMark = {
97
+ type: 'textStyle';
98
+ attrs?: Maybe<{
99
+ color?: Maybe<string>;
100
+ }>;
101
+ };
102
+ type RichTextLink<T extends Record<string, any> = Record<string, any>> = {
103
+ type: 'link';
104
+ attrs?: Maybe<{
105
+ data?: Maybe<T>;
106
+ }>;
107
+ };
108
+ type RichTextMark = RichTextUnderlineMark | RichTextBoldMark | RichTextItalicMark | RichTextCodeMark | RichTextStrikeMark | RichTextTextStyleMark | RichTextLink;
109
+ type RichTextTextAlign = 'left' | 'center' | 'right';
110
+ type Maybe<T> = T | null | undefined;
111
+
112
+ /** Converts a single rich text node to its plain text representation. */
113
+ declare function richTextNodeToString(node: RichTextNode): string;
114
+ /** Converts an array of rich text nodes to plain text, joining them with newlines. */
115
+ declare function richTextNodesToString(nodes: RichTextNode[]): string;
116
+ /** Converts a rich text document to its plain text representation. */
117
+ declare function richTextDocumentToString(document: RichTextDocument): string;
118
+ /** Returns `true` if the document has no content or only whitespace. */
119
+ declare function isRichTextDocumentEmpty(document: RichTextDocument): boolean;
120
+ /** Creates a rich text document containing a single paragraph with the given text. */
121
+ declare function createRichTextDocumentFromString(text: string | null | undefined): RichTextDocument;
122
+ /** Parses a JSON string into a {@link RichTextDocument}. Returns an empty document if parsing fails or the input is nullish. */
123
+ declare function parseRichTextDocument(text: string | null | undefined): RichTextDocument;
124
+
125
+ export { type Maybe, type RichTextBlockquote, type RichTextBoldMark, type RichTextBulletList, type RichTextCodeBlock, type RichTextCodeMark, type RichTextDocument, type RichTextHardBreak, type RichTextHeading, type RichTextHorizontalRule, type RichTextItalicMark, type RichTextLink, type RichTextListItem, type RichTextMark, type RichTextNode, type RichTextOrderedList, type RichTextParagraph, type RichTextStrikeMark, type RichTextTable, type RichTextTableCell, type RichTextTableHeader, type RichTextTableRow, type RichTextText, type RichTextTextAlign, type RichTextTextStyleMark, type RichTextUnderlineMark, createRichTextDocumentFromString, isRichTextDocumentEmpty, parseRichTextDocument, richTextDocumentToString, richTextNodeToString, richTextNodesToString };
package/dist/index.js CHANGED
@@ -1,73 +1,76 @@
1
- function n(t) {
2
- if (t.type === "text")
3
- return t.text;
4
- switch (t.type) {
1
+ // src/helpers.ts
2
+ function richTextNodeToString(node) {
3
+ if (node.type === "text") {
4
+ return node.text;
5
+ }
6
+ switch (node.type) {
5
7
  case "heading":
6
8
  case "paragraph":
7
9
  case "codeBlock":
8
10
  case "blockquote":
9
- return (t.content ?? []).map(n).join("");
11
+ return (node.content ?? []).map(richTextNodeToString).join("");
10
12
  case "bulletList":
11
13
  case "orderedList":
12
- return (t.content ?? []).map((e) => (e.content ?? []).map(n).join("")).join("");
14
+ return (node.content ?? []).map((item) => (item.content ?? []).map(richTextNodeToString).join("")).join("");
13
15
  case "listItem":
14
- return (t.content ?? []).map(n).join("");
16
+ return (node.content ?? []).map(richTextNodeToString).join("");
15
17
  case "horizontalRule":
16
18
  return "---";
17
19
  case "hardBreak":
18
- return `
19
- `;
20
+ return "\n";
20
21
  case "table":
21
- return (t.content ?? []).map(
22
- (e) => (e.content ?? []).map((c) => (c.content ?? []).map(n).join("")).join(" | ")
23
- ).join(`
24
- `);
22
+ return (node.content ?? []).map(
23
+ (row) => (row.content ?? []).map((cell) => (cell.content ?? []).map(richTextNodeToString).join("")).join(" | ")
24
+ ).join("\n");
25
25
  case "tableRow":
26
- return (t.content ?? []).map((e) => (e.content ?? []).map(n).join("")).join(" | ");
26
+ return (node.content ?? []).map((cell) => (cell.content ?? []).map(richTextNodeToString).join("")).join(" | ");
27
27
  case "tableCell":
28
- return (t.content ?? []).map(n).join("");
28
+ return (node.content ?? []).map(richTextNodeToString).join("");
29
29
  case "tableHeader":
30
- return (t.content ?? []).map(n).join("");
30
+ return (node.content ?? []).map(richTextNodeToString).join("");
31
31
  }
32
32
  }
33
- function o(t) {
34
- return t.map(n).join(`
35
- `);
33
+ function richTextNodesToString(nodes) {
34
+ return nodes.map(richTextNodeToString).join("\n");
36
35
  }
37
- function a(t) {
38
- return o(t.content ?? []);
36
+ function richTextDocumentToString(document) {
37
+ return richTextNodesToString(document.content ?? []);
39
38
  }
40
- function i(t) {
41
- return (t.content ?? []).length === 0 || (t.content ?? []).every((e) => n(e).trim() === "");
39
+ function isRichTextDocumentEmpty(document) {
40
+ return (document.content ?? []).length === 0 || (document.content ?? []).every((node) => richTextNodeToString(node).trim() === "");
42
41
  }
43
- function u(t) {
42
+ function createRichTextDocumentFromString(text) {
44
43
  return {
45
44
  type: "doc",
46
45
  content: [
47
46
  {
48
47
  type: "paragraph",
49
- content: t ? [{ type: "text", text: t }] : []
48
+ content: text ? [{ type: "text", text }] : []
50
49
  }
51
50
  ]
52
51
  };
53
52
  }
54
- function r() {
53
+ function createEmptyRichTextDocument() {
55
54
  return {
56
55
  type: "doc",
57
56
  content: []
58
57
  };
59
58
  }
60
- function p(t) {
61
- if (!t)
62
- return r();
63
- const e = JSON.parse(t);
64
- return e.type !== "doc" ? r() : e;
59
+ function parseRichTextDocument(text) {
60
+ if (!text) {
61
+ return createEmptyRichTextDocument();
62
+ }
63
+ const parsed = JSON.parse(text);
64
+ if (parsed.type !== "doc") {
65
+ return createEmptyRichTextDocument();
66
+ }
67
+ return parsed;
65
68
  }
66
69
  export {
67
- u as createRichTextDocumentFromString,
68
- i as isRichTextDocumentEmpty,
69
- p as parseRichTextDocument,
70
- a as richTextDocumentToString,
71
- n as richTextNodeToString,
72
- o as richTextNodesToString
70
+ createRichTextDocumentFromString,
71
+ isRichTextDocumentEmpty,
72
+ parseRichTextDocument,
73
+ richTextDocumentToString,
74
+ richTextNodeToString,
75
+ richTextNodesToString
73
76
  };
package/package.json CHANGED
@@ -1,28 +1,21 @@
1
1
  {
2
2
  "name": "@contello/rich-text",
3
- "description": "Contello Rich Text library",
4
- "scripts": {
5
- "start": "vite build --watch",
6
- "build": "rm -rf dist/ && vite build",
7
- "lint": "eslint . --report-unused-disable-directives --max-warnings 0",
8
- "patch": "node ci/patcher.js"
3
+ "version": "8.21.2",
4
+ "description": "TypeScript types and helpers for working with Contello CMS rich text documents (TipTap/ProseMirror JSON format)",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "admin@entwico.com",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/entwico/contello-js",
11
+ "directory": "packages/rich-text"
9
12
  },
10
- "devDependencies": {
11
- "@types/node": "^24.0.10",
12
- "@typescript-eslint/eslint-plugin": "^8.35.1",
13
- "eslint": "^9.30.1",
14
- "eslint-config-prettier": "^10.1.5",
15
- "eslint-plugin-import": "^2.32.0",
16
- "eslint-plugin-prettier": "^5.5.1",
17
- "npm-run-all": "^4.1.5",
18
- "typescript": "^5.8.3",
19
- "typescript-eslint": "^8.35.1",
20
- "vite": "^7.0.2",
21
- "vite-plugin-dts": "^4.5.4"
13
+ "scripts": {
14
+ "build": "tsup",
15
+ "lint": "eslint .",
16
+ "lint:fix": "eslint . --fix",
17
+ "typecheck": "tsc --noEmit"
22
18
  },
23
- "author": "admin@entwico.com",
24
- "license": "MIT",
25
- "type": "module",
26
19
  "files": [
27
20
  "dist"
28
21
  ],
@@ -30,12 +23,11 @@
30
23
  "exports": {
31
24
  ".": {
32
25
  "import": "./dist/index.js",
33
- "require": "./dist/index.umd.cjs"
26
+ "require": "./dist/index.cjs"
34
27
  }
35
28
  },
36
29
  "publishConfig": {
37
30
  "access": "public",
38
31
  "registry": "https://registry.npmjs.org"
39
- },
40
- "version": "8.21.0"
41
- }
32
+ }
33
+ }
@@ -1,4 +0,0 @@
1
- (function(n,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(n=typeof globalThis<"u"?globalThis:n||self,e(n.ContelloRichText={}))})(this,function(n){"use strict";function e(t){if(t.type==="text")return t.text;switch(t.type){case"heading":case"paragraph":case"codeBlock":case"blockquote":return(t.content??[]).map(e).join("");case"bulletList":case"orderedList":return(t.content??[]).map(c=>(c.content??[]).map(e).join("")).join("");case"listItem":return(t.content??[]).map(e).join("");case"horizontalRule":return"---";case"hardBreak":return`
2
- `;case"table":return(t.content??[]).map(c=>(c.content??[]).map(m=>(m.content??[]).map(e).join("")).join(" | ")).join(`
3
- `);case"tableRow":return(t.content??[]).map(c=>(c.content??[]).map(e).join("")).join(" | ");case"tableCell":return(t.content??[]).map(e).join("");case"tableHeader":return(t.content??[]).map(e).join("")}}function r(t){return t.map(e).join(`
4
- `)}function i(t){return r(t.content??[])}function a(t){return(t.content??[]).length===0||(t.content??[]).every(c=>e(c).trim()==="")}function u(t){return{type:"doc",content:[{type:"paragraph",content:t?[{type:"text",text:t}]:[]}]}}function o(){return{type:"doc",content:[]}}function p(t){if(!t)return o();const c=JSON.parse(t);return c.type!=="doc"?o():c}n.createRichTextDocumentFromString=u,n.isRichTextDocumentEmpty=a,n.parseRichTextDocument=p,n.richTextDocumentToString=i,n.richTextNodeToString=e,n.richTextNodesToString=r,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});