@3kles/richtext-formats 0.0.0-stage → 1.1.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.
- package/README.md +140 -2
- package/lib/context.d.ts +19 -0
- package/lib/context.js +85 -0
- package/lib/context.js.map +1 -0
- package/lib/html-export.d.ts +8 -0
- package/lib/html-export.js +128 -0
- package/lib/html-export.js.map +1 -0
- package/lib/html-import.d.ts +4 -0
- package/lib/html-import.js +315 -0
- package/lib/html-import.js.map +1 -0
- package/lib/html-parser.d.ts +7 -0
- package/lib/html-parser.js +128 -0
- package/lib/html-parser.js.map +1 -0
- package/lib/plain-text.d.ts +6 -0
- package/lib/plain-text.js +81 -0
- package/lib/plain-text.js.map +1 -0
- package/lib/selection.d.ts +4 -0
- package/lib/selection.js +177 -0
- package/lib/selection.js.map +1 -0
- package/lib/tiptap-import.d.ts +5 -0
- package/lib/tiptap-import.js +470 -0
- package/lib/tiptap-import.js.map +1 -0
- package/lib/tiptap-types.d.ts +22 -0
- package/lib/tiptap-types.js +9 -0
- package/lib/tiptap-types.js.map +1 -0
- package/lib/types.d.ts +39 -0
- package/lib/types.js +11 -0
- package/lib/types.js.map +1 -0
- package/package.json +22 -4
- package/public-api.d.ts +8 -0
- package/public-api.js +8 -0
- package/public-api.js.map +1 -0
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
import { hasControlCharacters, isSupportedLinkDestination, isWellFormedText, validateDocument, } from '@3kles/richtext-core';
|
|
2
|
+
import { FormatLimitError, formatOperation } from './context.js';
|
|
3
|
+
import { DEFAULT_TIPTAP_IMPORT_LIMITS } from './tiptap-types.js';
|
|
4
|
+
class SourceError extends Error {
|
|
5
|
+
code;
|
|
6
|
+
path;
|
|
7
|
+
constructor(code, path, message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.path = path;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function object(value) {
|
|
14
|
+
return typeof value === 'object' && value !== null;
|
|
15
|
+
}
|
|
16
|
+
function array(value) {
|
|
17
|
+
return Array.isArray(value);
|
|
18
|
+
}
|
|
19
|
+
function migrationLimits(options) {
|
|
20
|
+
const limits = { ...DEFAULT_TIPTAP_IMPORT_LIMITS, ...options.migrationLimits };
|
|
21
|
+
if (Object.values(limits).some((value) => !Number.isSafeInteger(value) || value < 1)) {
|
|
22
|
+
throw new FormatLimitError('Migration limits must be positive safe integers.');
|
|
23
|
+
}
|
|
24
|
+
if (limits.maxDepth > 512)
|
|
25
|
+
throw new FormatLimitError('Migration JSON depth cannot exceed 512.');
|
|
26
|
+
return limits;
|
|
27
|
+
}
|
|
28
|
+
/** Snapshots own data descriptors iteratively before structural conversion. */
|
|
29
|
+
class SourceSnapshot {
|
|
30
|
+
records = new WeakMap();
|
|
31
|
+
arrays = new WeakMap();
|
|
32
|
+
constructor(input, limits) {
|
|
33
|
+
const stack = [{ value: input, path: [], depth: 0, attribute: false }];
|
|
34
|
+
const ancestors = new WeakSet();
|
|
35
|
+
let nodes = 0;
|
|
36
|
+
let text = 0;
|
|
37
|
+
while (stack.length) {
|
|
38
|
+
const item = stack.pop();
|
|
39
|
+
if (!item)
|
|
40
|
+
break;
|
|
41
|
+
const { value, path, depth, attribute } = item;
|
|
42
|
+
if (item.leave) {
|
|
43
|
+
if (object(value))
|
|
44
|
+
ancestors.delete(value);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (++nodes > limits.maxNodes)
|
|
48
|
+
throw new SourceError('resourceLimit', path, 'Source JSON value count exceeds its limit.');
|
|
49
|
+
if (depth > limits.maxDepth)
|
|
50
|
+
throw new SourceError('resourceLimit', path, 'Source JSON depth exceeds its limit.');
|
|
51
|
+
if (typeof value === 'string') {
|
|
52
|
+
if (!isWellFormedText(value))
|
|
53
|
+
throw new SourceError('invalidUnicode', path, 'Source data contains an isolated UTF-16 surrogate.');
|
|
54
|
+
text += value.length;
|
|
55
|
+
if (text > limits.maxTextLength)
|
|
56
|
+
throw new SourceError('resourceLimit', path, 'Source string data exceeds its total limit.');
|
|
57
|
+
if (attribute && value.length > limits.maxAttributeLength)
|
|
58
|
+
throw new SourceError('resourceLimit', path, 'A source attribute exceeds its length limit.');
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (value === null || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value)))
|
|
62
|
+
continue;
|
|
63
|
+
if (!object(value))
|
|
64
|
+
throw new SourceError('invalidInput', path, 'Only JSON data values are accepted.');
|
|
65
|
+
if (ancestors.has(value))
|
|
66
|
+
throw new SourceError('invalidInput', path, 'Cyclic source data is not accepted.');
|
|
67
|
+
ancestors.add(value);
|
|
68
|
+
stack.push({ ...item, leave: true });
|
|
69
|
+
if (array(value)) {
|
|
70
|
+
let values = this.arrays.get(value);
|
|
71
|
+
if (!values) {
|
|
72
|
+
const prototype = Object.getPrototypeOf(value);
|
|
73
|
+
if (prototype !== Array.prototype && prototype !== null)
|
|
74
|
+
throw new SourceError('invalidInput', path, 'Only plain source arrays are accepted.');
|
|
75
|
+
const size = Object.getOwnPropertyDescriptor(value, 'length')?.value;
|
|
76
|
+
if (typeof size !== 'number' || size > limits.maxNodes)
|
|
77
|
+
throw new SourceError('resourceLimit', path, 'A source array exceeds its value limit.');
|
|
78
|
+
const keys = Reflect.ownKeys(value);
|
|
79
|
+
if (keys.length !== size + 1)
|
|
80
|
+
throw new SourceError('invalidInput', path, 'Sparse arrays and extra array properties are not accepted.');
|
|
81
|
+
const copied = [];
|
|
82
|
+
for (let index = 0; index < size; index++) {
|
|
83
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, String(index));
|
|
84
|
+
if (!descriptor || !('value' in descriptor) || !descriptor.enumerable)
|
|
85
|
+
throw new SourceError('invalidInput', [...path, index], 'Array entries must be own enumerable data values.');
|
|
86
|
+
const entry = descriptor.value;
|
|
87
|
+
copied.push(entry);
|
|
88
|
+
}
|
|
89
|
+
values = Object.freeze(copied);
|
|
90
|
+
this.arrays.set(value, values);
|
|
91
|
+
}
|
|
92
|
+
if (nodes + values.length > limits.maxNodes)
|
|
93
|
+
throw new SourceError('resourceLimit', path, 'A source array exceeds its remaining value budget.');
|
|
94
|
+
for (let index = values.length - 1; index >= 0; index--)
|
|
95
|
+
stack.push({ value: values[index], path: [...path, index], depth: depth + 1, attribute });
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
let record = this.records.get(value);
|
|
99
|
+
if (!record) {
|
|
100
|
+
const prototype = Object.getPrototypeOf(value);
|
|
101
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
102
|
+
throw new SourceError('invalidInput', path, 'Only plain source objects are accepted.');
|
|
103
|
+
const keys = Reflect.ownKeys(value);
|
|
104
|
+
if (keys.length > limits.maxAttributesPerObject)
|
|
105
|
+
throw new SourceError('resourceLimit', path, 'A source object exceeds its property count limit.');
|
|
106
|
+
const copied = new Map();
|
|
107
|
+
for (const key of keys) {
|
|
108
|
+
if (typeof key !== 'string')
|
|
109
|
+
throw new SourceError('invalidInput', path, 'Symbol properties are not JSON data.');
|
|
110
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
111
|
+
if (!descriptor || !('value' in descriptor) || !descriptor.enumerable)
|
|
112
|
+
throw new SourceError('invalidInput', path, 'Accessors and hidden properties are not accepted.');
|
|
113
|
+
if (!isWellFormedText(key))
|
|
114
|
+
throw new SourceError('invalidUnicode', path, 'A source property name is not valid Unicode.');
|
|
115
|
+
if (key.length > limits.maxAttributeLength)
|
|
116
|
+
throw new SourceError('resourceLimit', path, 'Source property names exceed their string limit.');
|
|
117
|
+
const field = descriptor.value;
|
|
118
|
+
copied.set(key, field);
|
|
119
|
+
}
|
|
120
|
+
record = copied;
|
|
121
|
+
this.records.set(value, record);
|
|
122
|
+
}
|
|
123
|
+
for (const [key, field] of record) {
|
|
124
|
+
text += key.length;
|
|
125
|
+
if (text > limits.maxTextLength)
|
|
126
|
+
throw new SourceError('resourceLimit', path, 'Source property names exceed their total string limit.');
|
|
127
|
+
stack.push({
|
|
128
|
+
value: field,
|
|
129
|
+
path: [...path, key],
|
|
130
|
+
depth: depth + 1,
|
|
131
|
+
attribute: attribute || key === 'attrs',
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
record(value, path) {
|
|
138
|
+
const record = object(value) ? this.records.get(value) : undefined;
|
|
139
|
+
if (!record)
|
|
140
|
+
throw new SourceError('invalidInput', path, 'Expected a source node, mark or attributes object.');
|
|
141
|
+
return record;
|
|
142
|
+
}
|
|
143
|
+
content(value, path) {
|
|
144
|
+
const values = object(value) ? this.arrays.get(value) : undefined;
|
|
145
|
+
if (!values)
|
|
146
|
+
throw new SourceError('invalidInput', path, 'Expected a source content or marks array.');
|
|
147
|
+
return values;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const KNOWN_NODES = new Set([
|
|
151
|
+
'doc',
|
|
152
|
+
'paragraph',
|
|
153
|
+
'text',
|
|
154
|
+
'heading',
|
|
155
|
+
'hardBreak',
|
|
156
|
+
'bulletList',
|
|
157
|
+
'orderedList',
|
|
158
|
+
'listItem',
|
|
159
|
+
'blockquote',
|
|
160
|
+
'codeBlock',
|
|
161
|
+
]);
|
|
162
|
+
const BLOCK_NODES = new Set(['paragraph', 'heading', 'bulletList', 'orderedList', 'blockquote', 'codeBlock']);
|
|
163
|
+
class TiptapReader {
|
|
164
|
+
snapshot;
|
|
165
|
+
context;
|
|
166
|
+
constructor(snapshot, context) {
|
|
167
|
+
this.snapshot = snapshot;
|
|
168
|
+
this.context = context;
|
|
169
|
+
}
|
|
170
|
+
validateFrames(input) {
|
|
171
|
+
const pending = [{ input, path: [] }];
|
|
172
|
+
while (pending.length) {
|
|
173
|
+
const frame = pending.pop();
|
|
174
|
+
if (!frame)
|
|
175
|
+
break;
|
|
176
|
+
const record = this.snapshot.record(frame.input, frame.path);
|
|
177
|
+
const type = record.get('type');
|
|
178
|
+
if (typeof type !== 'string' || !type)
|
|
179
|
+
throw new SourceError('invalidInput', [...frame.path, 'type'], 'A source node or mark needs a nonempty type.');
|
|
180
|
+
if (record.has('attrs'))
|
|
181
|
+
this.snapshot.record(record.get('attrs'), [...frame.path, 'attrs']);
|
|
182
|
+
if (frame.mark)
|
|
183
|
+
continue;
|
|
184
|
+
if (frame.parent && KNOWN_NODES.has(type)) {
|
|
185
|
+
const fits = frame.parent === 'inline'
|
|
186
|
+
? type === 'text' || type === 'hardBreak'
|
|
187
|
+
: frame.parent === 'code'
|
|
188
|
+
? type === 'text'
|
|
189
|
+
: frame.parent === 'list'
|
|
190
|
+
? type === 'listItem'
|
|
191
|
+
: BLOCK_NODES.has(type);
|
|
192
|
+
if (!fits)
|
|
193
|
+
throw new SourceError('invalidInput', frame.path, 'A known source node appears in an invalid parent position.');
|
|
194
|
+
}
|
|
195
|
+
if (type === 'text') {
|
|
196
|
+
if (typeof record.get('text') !== 'string' || !record.get('text'))
|
|
197
|
+
throw new SourceError('invalidInput', [...frame.path, 'text'], 'A source text run must contain nonempty text.');
|
|
198
|
+
}
|
|
199
|
+
else if (record.has('text'))
|
|
200
|
+
throw new SourceError('invalidInput', [...frame.path, 'text'], 'Only a source text node can own text.');
|
|
201
|
+
const content = this.children(record, frame.path);
|
|
202
|
+
if ((type === 'text' || type === 'hardBreak') && content.length)
|
|
203
|
+
throw new SourceError('invalidInput', [...frame.path, 'content'], 'This source inline leaf cannot contain children.');
|
|
204
|
+
if (['doc', 'blockquote', 'bulletList', 'orderedList', 'listItem'].includes(type) && !content.length)
|
|
205
|
+
throw new SourceError('invalidInput', [...frame.path, 'content'], 'This source structural node requires nonempty content.');
|
|
206
|
+
if (type === 'listItem' &&
|
|
207
|
+
this.snapshot.record(content[0], [...frame.path, 'content', 0]).get('type') !== 'paragraph')
|
|
208
|
+
throw new SourceError('invalidInput', [...frame.path, 'content', 0], 'A source list item must start with a paragraph.');
|
|
209
|
+
const parent = type === 'paragraph' || type === 'heading'
|
|
210
|
+
? 'inline'
|
|
211
|
+
: type === 'codeBlock'
|
|
212
|
+
? 'code'
|
|
213
|
+
: type === 'bulletList' || type === 'orderedList'
|
|
214
|
+
? 'list'
|
|
215
|
+
: type === 'doc' || type === 'blockquote' || type === 'listItem'
|
|
216
|
+
? type
|
|
217
|
+
: undefined;
|
|
218
|
+
content.forEach((child, index) => pending.push({ input: child, path: [...frame.path, 'content', index], ...(parent ? { parent } : {}) }));
|
|
219
|
+
if (record.has('marks')) {
|
|
220
|
+
const marks = this.snapshot.content(record.get('marks'), [...frame.path, 'marks']);
|
|
221
|
+
marks.forEach((mark, index) => pending.push({ input: mark, path: [...frame.path, 'marks', index], mark: true }));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
loss(code, path, message) {
|
|
226
|
+
this.context.add(code, 'warning', path, message);
|
|
227
|
+
}
|
|
228
|
+
keys(record, allowed, path) {
|
|
229
|
+
for (const key of record.keys())
|
|
230
|
+
if (!allowed.includes(key))
|
|
231
|
+
this.loss('unsupportedAttribute', [...path, key], 'A source property outside the fixed profile was omitted.');
|
|
232
|
+
}
|
|
233
|
+
attrs(record, allowed, path) {
|
|
234
|
+
if (!record.has('attrs'))
|
|
235
|
+
return new Map();
|
|
236
|
+
const attrs = this.snapshot.record(record.get('attrs'), [...path, 'attrs']);
|
|
237
|
+
this.keys(attrs, allowed, [...path, 'attrs']);
|
|
238
|
+
return attrs;
|
|
239
|
+
}
|
|
240
|
+
children(record, path) {
|
|
241
|
+
return record.has('content') ? this.snapshot.content(record.get('content'), [...path, 'content']) : [];
|
|
242
|
+
}
|
|
243
|
+
marks(record, path, allowed) {
|
|
244
|
+
if (!record.has('marks'))
|
|
245
|
+
return [];
|
|
246
|
+
const source = this.snapshot.content(record.get('marks'), [...path, 'marks']);
|
|
247
|
+
if (!allowed && source.length)
|
|
248
|
+
this.loss('unsupportedMark', [...path, 'marks'], 'Marks at this source position cannot be represented and were omitted.');
|
|
249
|
+
const marks = [];
|
|
250
|
+
source.forEach((item, index) => {
|
|
251
|
+
const markPath = [...path, 'marks', index];
|
|
252
|
+
const mark = this.snapshot.record(item, markPath);
|
|
253
|
+
this.keys(mark, ['type', 'attrs'], markPath);
|
|
254
|
+
const type = mark.get('type');
|
|
255
|
+
if (typeof type !== 'string' || !type)
|
|
256
|
+
throw new SourceError('invalidInput', [...markPath, 'type'], 'A source mark needs a nonempty type.');
|
|
257
|
+
const attrs = this.attrs(mark, type === 'link' ? ['href', 'title', 'target', 'rel', 'class'] : [], markPath);
|
|
258
|
+
let converted;
|
|
259
|
+
if (type === 'bold' || type === 'italic' || type === 'underline' || type === 'strike' || type === 'code')
|
|
260
|
+
converted = { type };
|
|
261
|
+
else if (type === 'link') {
|
|
262
|
+
const href = attrs.get('href');
|
|
263
|
+
if (typeof href !== 'string')
|
|
264
|
+
throw new SourceError('invalidInput', [...markPath, 'attrs', 'href'], 'A source link needs a string URL.');
|
|
265
|
+
for (const name of ['target', 'rel', 'class']) {
|
|
266
|
+
const value = attrs.get(name);
|
|
267
|
+
if (value !== undefined && value !== null && typeof value !== 'string')
|
|
268
|
+
throw new SourceError('invalidInput', [...markPath, 'attrs', name], 'Link metadata must be a string or null.');
|
|
269
|
+
if (value && !(name === 'target' && value === '_self'))
|
|
270
|
+
this.loss('unsupportedAttribute', [...markPath, 'attrs', name], 'Nonrepresentable link metadata was omitted.');
|
|
271
|
+
}
|
|
272
|
+
const title = attrs.get('title');
|
|
273
|
+
if (title !== undefined && title !== null && typeof title !== 'string')
|
|
274
|
+
throw new SourceError('invalidInput', [...markPath, 'attrs', 'title'], 'A source link title must be a string or null.');
|
|
275
|
+
if (!isSupportedLinkDestination(href, this.context.options.linkPolicy))
|
|
276
|
+
this.loss('unsafeUrl', [...markPath, 'attrs', 'href'], 'A source link outside the configured destination policy was omitted.');
|
|
277
|
+
else
|
|
278
|
+
converted = typeof title === 'string' ? { type: 'link', href, title } : { type: 'link', href };
|
|
279
|
+
}
|
|
280
|
+
else
|
|
281
|
+
this.loss('unsupportedMark', [...markPath, 'type'], 'A mark outside the fixed source profile was omitted.');
|
|
282
|
+
if (allowed && converted) {
|
|
283
|
+
const previous = marks.find((existing) => existing.type === converted.type);
|
|
284
|
+
if (previous &&
|
|
285
|
+
previous.type === 'link' &&
|
|
286
|
+
converted.type === 'link' &&
|
|
287
|
+
(previous.href !== converted.href || previous.title !== converted.title))
|
|
288
|
+
throw new SourceError('invalidInput', markPath, 'Conflicting source marks are not accepted.');
|
|
289
|
+
marks.push(converted);
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
return marks;
|
|
293
|
+
}
|
|
294
|
+
node(input, path, parent) {
|
|
295
|
+
const record = this.snapshot.record(input, path);
|
|
296
|
+
this.keys(record, ['type', 'attrs', 'content', 'marks', 'text'], path);
|
|
297
|
+
const type = record.get('type');
|
|
298
|
+
if (typeof type !== 'string' || !type)
|
|
299
|
+
throw new SourceError('invalidInput', [...path, 'type'], 'A source node needs a nonempty type.');
|
|
300
|
+
if (!KNOWN_NODES.has(type)) {
|
|
301
|
+
this.loss('unsupportedNode', [...path, 'type'], 'An unsupported source node and its entire subtree were omitted.');
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
const fits = parent === 'inline'
|
|
305
|
+
? type === 'text' || type === 'hardBreak'
|
|
306
|
+
: parent === 'code'
|
|
307
|
+
? type === 'text'
|
|
308
|
+
: parent === 'list'
|
|
309
|
+
? type === 'listItem'
|
|
310
|
+
: BLOCK_NODES.has(type);
|
|
311
|
+
if (!fits)
|
|
312
|
+
throw new SourceError('invalidInput', path, 'A known source node appears in an invalid parent position.');
|
|
313
|
+
const attrs = this.attrs(record, type === 'heading'
|
|
314
|
+
? ['level']
|
|
315
|
+
: type === 'orderedList'
|
|
316
|
+
? ['start', 'type']
|
|
317
|
+
: type === 'codeBlock'
|
|
318
|
+
? ['language']
|
|
319
|
+
: [], path);
|
|
320
|
+
const children = this.children(record, path);
|
|
321
|
+
const marks = this.marks(record, path, type === 'text' && parent !== 'code');
|
|
322
|
+
if (type !== 'text' && record.has('text'))
|
|
323
|
+
throw new SourceError('invalidInput', [...path, 'text'], 'Only a source text node can own text.');
|
|
324
|
+
if (type === 'text') {
|
|
325
|
+
if (children.length)
|
|
326
|
+
throw new SourceError('invalidInput', [...path, 'content'], 'Source text cannot contain children.');
|
|
327
|
+
const text = record.get('text');
|
|
328
|
+
if (typeof text !== 'string' || !text.length)
|
|
329
|
+
throw new SourceError('invalidInput', [...path, 'text'], 'A source text run must contain nonempty text.');
|
|
330
|
+
if (parent === 'code' ? /\r/u.test(text) : /[\r\n]/u.test(text))
|
|
331
|
+
throw new SourceError('invalidInput', [...path, 'text'], 'Source line endings cannot be represented at this text position.');
|
|
332
|
+
return marks.length ? { type: 'text', text, marks } : { type: 'text', text };
|
|
333
|
+
}
|
|
334
|
+
if (type === 'hardBreak') {
|
|
335
|
+
if (children.length)
|
|
336
|
+
throw new SourceError('invalidInput', [...path, 'content'], 'A hard break cannot contain children.');
|
|
337
|
+
return { type: 'hardBreak' };
|
|
338
|
+
}
|
|
339
|
+
if (type === 'paragraph' || type === 'heading') {
|
|
340
|
+
const inline = children.flatMap((child, index) => {
|
|
341
|
+
const converted = this.node(child, [...path, 'content', index], 'inline');
|
|
342
|
+
return converted?.type === 'text' || converted?.type === 'hardBreak' ? [converted] : [];
|
|
343
|
+
});
|
|
344
|
+
if (type === 'paragraph')
|
|
345
|
+
return { type, children: inline };
|
|
346
|
+
const level = attrs.has('level') ? attrs.get('level') : 1;
|
|
347
|
+
if (level !== 1 && level !== 2 && level !== 3 && level !== 4 && level !== 5 && level !== 6)
|
|
348
|
+
throw new SourceError('invalidInput', [...path, 'attrs', 'level'], 'A heading level must be between one and six.');
|
|
349
|
+
const heading = { type, level, children: inline };
|
|
350
|
+
return heading;
|
|
351
|
+
}
|
|
352
|
+
if (type === 'codeBlock') {
|
|
353
|
+
const runs = [];
|
|
354
|
+
children.forEach((child, index) => {
|
|
355
|
+
const converted = this.node(child, [...path, 'content', index], 'code');
|
|
356
|
+
if (converted?.type === 'text')
|
|
357
|
+
runs.push(converted);
|
|
358
|
+
});
|
|
359
|
+
const text = runs.map((run) => run.text).join('');
|
|
360
|
+
const language = attrs.get('language');
|
|
361
|
+
if (language !== undefined &&
|
|
362
|
+
language !== null &&
|
|
363
|
+
(typeof language !== 'string' || hasControlCharacters(language)))
|
|
364
|
+
throw new SourceError('invalidInput', [...path, 'attrs', 'language'], 'Code language must be plain string metadata or null.');
|
|
365
|
+
const content = text ? [{ type: 'text', text }] : [];
|
|
366
|
+
return typeof language === 'string' ? { type, language, children: content } : { type, children: content };
|
|
367
|
+
}
|
|
368
|
+
if (!children.length)
|
|
369
|
+
throw new SourceError('invalidInput', [...path, 'content'], 'This source structural node requires nonempty content.');
|
|
370
|
+
if (type === 'bulletList' || type === 'orderedList') {
|
|
371
|
+
const items = [];
|
|
372
|
+
children.forEach((child, index) => {
|
|
373
|
+
const result = this.node(child, [...path, 'content', index], 'list');
|
|
374
|
+
if (result?.type === 'listItem')
|
|
375
|
+
items.push(result);
|
|
376
|
+
});
|
|
377
|
+
if (!items.length) {
|
|
378
|
+
items.push({ type: 'listItem', children: [{ type: 'paragraph', children: [] }] });
|
|
379
|
+
this.loss('structuralRewrite', path, 'An empty sanitized list received an empty item.');
|
|
380
|
+
}
|
|
381
|
+
if (type === 'bulletList')
|
|
382
|
+
return { type, children: items };
|
|
383
|
+
const start = attrs.has('start') ? attrs.get('start') : 1;
|
|
384
|
+
if (typeof start !== 'number' || !Number.isSafeInteger(start))
|
|
385
|
+
throw new SourceError('invalidInput', [...path, 'attrs', 'start'], 'A list start must be a safe integer.');
|
|
386
|
+
const numbering = attrs.get('type');
|
|
387
|
+
if (numbering !== undefined && numbering !== null && typeof numbering !== 'string')
|
|
388
|
+
throw new SourceError('invalidInput', [...path, 'attrs', 'type'], 'List numbering metadata must be a string or null.');
|
|
389
|
+
if (numbering && numbering !== '1')
|
|
390
|
+
this.loss('unsupportedAttribute', [...path, 'attrs', 'type'], 'Nondecimal list numbering was omitted.');
|
|
391
|
+
return { type, start, children: items };
|
|
392
|
+
}
|
|
393
|
+
if (type === 'listItem' || type === 'blockquote') {
|
|
394
|
+
const blocks = [];
|
|
395
|
+
children.forEach((child, index) => {
|
|
396
|
+
const result = this.node(child, [...path, 'content', index], type);
|
|
397
|
+
if (result && result.type !== 'text' && result.type !== 'hardBreak' && result.type !== 'listItem')
|
|
398
|
+
blocks.push(result);
|
|
399
|
+
});
|
|
400
|
+
if (type === 'listItem' && blocks[0]?.type !== 'paragraph')
|
|
401
|
+
throw new SourceError('invalidInput', [...path, 'content', 0], 'A source list item must start with a paragraph.');
|
|
402
|
+
if (!blocks.length) {
|
|
403
|
+
blocks.push({ type: 'paragraph', children: [] });
|
|
404
|
+
this.loss('structuralRewrite', path, 'An empty sanitized structural block received a paragraph.');
|
|
405
|
+
}
|
|
406
|
+
return { type, children: blocks };
|
|
407
|
+
}
|
|
408
|
+
throw new SourceError('invalidInput', path, 'The source node cannot be converted.');
|
|
409
|
+
}
|
|
410
|
+
document(input) {
|
|
411
|
+
this.validateFrames(input);
|
|
412
|
+
const root = this.snapshot.record(input, []);
|
|
413
|
+
if (root.get('type') !== 'doc')
|
|
414
|
+
throw new SourceError('invalidInput', ['type'], 'The source root must be a document node from the selected profile.');
|
|
415
|
+
this.keys(root, ['type', 'attrs', 'content', 'marks'], []);
|
|
416
|
+
this.attrs(root, [], []);
|
|
417
|
+
this.marks(root, [], false);
|
|
418
|
+
const source = this.children(root, []);
|
|
419
|
+
if (!source.length)
|
|
420
|
+
throw new SourceError('invalidInput', ['content'], 'A source document requires at least one block.');
|
|
421
|
+
const children = [];
|
|
422
|
+
source.forEach((child, index) => {
|
|
423
|
+
const result = this.node(child, ['content', index], 'doc');
|
|
424
|
+
if (result && result.type !== 'text' && result.type !== 'hardBreak' && result.type !== 'listItem')
|
|
425
|
+
children.push(result);
|
|
426
|
+
});
|
|
427
|
+
return { type: 'document', children: children.length ? children : [{ type: 'paragraph', children: [] }] };
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
/** Pure migration of one explicit JSON profile. Never reads HTML, editor instances or runtime storage. */
|
|
431
|
+
export function importTiptapJson(input, options) {
|
|
432
|
+
return formatOperation({}, (outer) => {
|
|
433
|
+
if (options?.profile !== 'tiptap-3-starterkit') {
|
|
434
|
+
outer.add('unsupportedProfile', 'error', [], 'An explicit supported Tiptap data profile is required.');
|
|
435
|
+
return outer.failure();
|
|
436
|
+
}
|
|
437
|
+
if (options.onLoss !== undefined && options.onLoss !== 'reject' && options.onLoss !== 'sanitize') {
|
|
438
|
+
outer.add('invalidInput', 'error', [], 'The migration loss policy is not supported.');
|
|
439
|
+
return outer.failure();
|
|
440
|
+
}
|
|
441
|
+
const limits = migrationLimits(options);
|
|
442
|
+
const target = {
|
|
443
|
+
onLoss: options.onLoss ?? 'reject',
|
|
444
|
+
htmlLimits: { maxDiagnostics: limits.maxDiagnostics },
|
|
445
|
+
...(options.limits ? { limits: options.limits } : {}),
|
|
446
|
+
...(options.linkPolicy ? { linkPolicy: options.linkPolicy } : {}),
|
|
447
|
+
};
|
|
448
|
+
return formatOperation(target, (context) => {
|
|
449
|
+
try {
|
|
450
|
+
const snapshot = new SourceSnapshot(input, limits);
|
|
451
|
+
const converted = new TiptapReader(snapshot, context).document(input);
|
|
452
|
+
const native = validateDocument(converted, options);
|
|
453
|
+
for (const diagnostic of native.diagnostics)
|
|
454
|
+
context.add(diagnostic.severity === 'info'
|
|
455
|
+
? 'normalized'
|
|
456
|
+
: diagnostic.code === 'resourceLimit'
|
|
457
|
+
? 'resourceLimit'
|
|
458
|
+
: 'invalidDocument', diagnostic.severity, diagnostic.path.map((part) => (part === 'children' ? 'content' : part)), diagnostic.message);
|
|
459
|
+
return native.ok ? context.finish(native.value) : context.failure();
|
|
460
|
+
}
|
|
461
|
+
catch (error) {
|
|
462
|
+
if (!(error instanceof SourceError))
|
|
463
|
+
throw error;
|
|
464
|
+
context.add(error.code, 'error', error.path, error.message);
|
|
465
|
+
return context.failure();
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
//# sourceMappingURL=tiptap-import.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tiptap-import.js","sourceRoot":"","sources":["../../src/lib/tiptap-import.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,oBAAoB,EACpB,0BAA0B,EAC1B,gBAAgB,EAChB,gBAAgB,GACnB,MAAM,sBAAsB,CAAC;AAU9B,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAGjE,OAAO,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAC;AAQjE,MAAM,WAAY,SAAQ,KAAK;IAEd;IACA;IAFb,YACa,IAA0B,EAC1B,IAAgB,EACzB,OAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAC;QAJN,SAAI,GAAJ,IAAI,CAAsB;QAC1B,SAAI,GAAJ,IAAI,CAAY;IAI7B,CAAC;CACJ;AAED,SAAS,MAAM,CAAC,KAAc;IAC1B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC;AACvD,CAAC;AACD,SAAS,KAAK,CAAC,KAAc;IACzB,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC;AAED,SAAS,eAAe,CAAC,OAA4B;IACjD,MAAM,MAAM,GAAG,EAAE,GAAG,4BAA4B,EAAE,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAC/E,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC;QACnF,MAAM,IAAI,gBAAgB,CAAC,kDAAkD,CAAC,CAAC;IACnF,CAAC;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AC,eAAe;4BACjB,CAAC,CAAC,iBAAiB,EACzB,UAAU,CAAC,QAAQ,EACnB,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EACvE,UAAU,CAAC,OAAO,CACrB,CAAC;gBACN,OAAO,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YACxE,CAAC;YAAC,OAAO,KAAc,EAAE,CAAC;gBACtB,IAAI,CAAC,CAAC,KAAK,YAAY,WAAW,CAAC;oBAAE,MAAM,KAAK,CAAC;gBACjD,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;gBAC5D,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;YAC7B,CAAC;QACL,CAAC,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC","sourcesContent":["import {\n hasControlCharacters,\n isSupportedLinkDestination,\n isWellFormedText,\n validateDocument,\n} from '@3kles/richtext-core';\nimport type {\n BlockNode,\n DocumentNode,\n HeadingNode,\n InlineNode,\n ListItemNode,\n Mark,\n TextNode,\n} from '@3kles/richtext-core';\nimport { FormatLimitError, formatOperation } from './context.js';\nimport type { FormatContext } from './context.js';\nimport type { FormatDiagnosticCode, FormatOptions, FormatResult } from './types.js';\nimport { DEFAULT_TIPTAP_IMPORT_LIMITS } from './tiptap-types.js';\nimport type { TiptapImportLimits, TiptapImportOptions } from './tiptap-types.js';\n\ntype SourcePath = readonly (string | number)[];\ntype SourceRecord = ReadonlyMap<string, unknown>;\ntype ParentKind = 'doc' | 'blockquote' | 'listItem' | 'list' | 'inline' | 'code';\ntype ConvertedNode = BlockNode | ListItemNode | InlineNode;\n\nclass SourceError extends Error {\n constructor(\n readonly code: FormatDiagnosticCode,\n readonly path: SourcePath,\n message: string,\n ) {\n super(message);\n }\n}\n\nfunction object(value: unknown): value is object {\n return typeof value === 'object' && value !== null;\n}\nfunction array(value: unknown): value is readonly unknown[] {\n return Array.isArray(value);\n}\n\nfunction migrationLimits(options: TiptapImportOptions): TiptapImportLimits {\n const limits = { ...DEFAULT_TIPTAP_IMPORT_LIMITS, ...options.migrationLimits };\n if (Object.values(limits).some((value) => !Number.isSafeInteger(value) || value < 1)) {\n throw new FormatLimitError('Migration limits must be positive safe integers.');\n }\n if (limits.maxDepth > 512) throw new FormatLimitError('Migration JSON depth cannot exceed 512.');\n return limits;\n}\n\n/** Snapshots own data descriptors iteratively before structural conversion. */\nclass SourceSnapshot {\n private readonly records = new WeakMap<object, SourceRecord>();\n private readonly arrays = new WeakMap<object, readonly unknown[]>();\n\n constructor(input: unknown, limits: TiptapImportLimits) {\n interface Visit {\n readonly value: unknown;\n readonly path: SourcePath;\n readonly depth: number;\n readonly attribute: boolean;\n readonly leave?: boolean;\n }\n const stack: Visit[] = [{ value: input, path: [], depth: 0, attribute: false }];\n const ancestors = new WeakSet<object>();\n let nodes = 0;\n let text = 0;\n while (stack.length) {\n const item = stack.pop();\n if (!item) break;\n const { value, path, depth, attribute } = item;\n if (item.leave) {\n if (object(value)) ancestors.delete(value);\n continue;\n }\n if (++nodes > limits.maxNodes)\n throw new SourceError('resourceLimit', path, 'Source JSON value count exceeds its limit.');\n if (depth > limits.maxDepth)\n throw new SourceError('resourceLimit', path, 'Source JSON depth exceeds its limit.');\n if (typeof value === 'string') {\n if (!isWellFormedText(value))\n throw new SourceError('invalidUnicode', path, 'Source data contains an isolated UTF-16 surrogate.');\n text += value.length;\n if (text > limits.maxTextLength)\n throw new SourceError('resourceLimit', path, 'Source string data exceeds its total limit.');\n if (attribute && value.length > limits.maxAttributeLength)\n throw new SourceError('resourceLimit', path, 'A source attribute exceeds its length limit.');\n continue;\n }\n if (value === null || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value)))\n continue;\n if (!object(value)) throw new SourceError('invalidInput', path, 'Only JSON data values are accepted.');\n if (ancestors.has(value))\n throw new SourceError('invalidInput', path, 'Cyclic source data is not accepted.');\n ancestors.add(value);\n stack.push({ ...item, leave: true });\n if (array(value)) {\n let values = this.arrays.get(value);\n if (!values) {\n const prototype: unknown = Object.getPrototypeOf(value);\n if (prototype !== Array.prototype && prototype !== null)\n throw new SourceError('invalidInput', path, 'Only plain source arrays are accepted.');\n const size: unknown = Object.getOwnPropertyDescriptor(value, 'length')?.value;\n if (typeof size !== 'number' || size > limits.maxNodes)\n throw new SourceError('resourceLimit', path, 'A source array exceeds its value limit.');\n const keys = Reflect.ownKeys(value);\n if (keys.length !== size + 1)\n throw new SourceError(\n 'invalidInput',\n path,\n 'Sparse arrays and extra array properties are not accepted.',\n );\n const copied: unknown[] = [];\n for (let index = 0; index < size; index++) {\n const descriptor = Object.getOwnPropertyDescriptor(value, String(index));\n if (!descriptor || !('value' in descriptor) || !descriptor.enumerable)\n throw new SourceError(\n 'invalidInput',\n [...path, index],\n 'Array entries must be own enumerable data values.',\n );\n const entry: unknown = descriptor.value;\n copied.push(entry);\n }\n values = Object.freeze(copied);\n this.arrays.set(value, values);\n }\n if (nodes + values.length > limits.maxNodes)\n throw new SourceError('resourceLimit', path, 'A source array exceeds its remaining value budget.');\n for (let index = values.length - 1; index >= 0; index--)\n stack.push({ value: values[index], path: [...path, index], depth: depth + 1, attribute });\n } else {\n let record = this.records.get(value);\n if (!record) {\n const prototype: unknown = Object.getPrototypeOf(value);\n if (prototype !== Object.prototype && prototype !== null)\n throw new SourceError('invalidInput', path, 'Only plain source objects are accepted.');\n const keys = Reflect.ownKeys(value);\n if (keys.length > limits.maxAttributesPerObject)\n throw new SourceError(\n 'resourceLimit',\n path,\n 'A source object exceeds its property count limit.',\n );\n const copied = new Map<string, unknown>();\n for (const key of keys) {\n if (typeof key !== 'string')\n throw new SourceError('invalidInput', path, 'Symbol properties are not JSON data.');\n const descriptor = Object.getOwnPropertyDescriptor(value, key);\n if (!descriptor || !('value' in descriptor) || !descriptor.enumerable)\n throw new SourceError(\n 'invalidInput',\n path,\n 'Accessors and hidden properties are not accepted.',\n );\n if (!isWellFormedText(key))\n throw new SourceError(\n 'invalidUnicode',\n path,\n 'A source property name is not valid Unicode.',\n );\n if (key.length > limits.maxAttributeLength)\n throw new SourceError(\n 'resourceLimit',\n path,\n 'Source property names exceed their string limit.',\n );\n const field: unknown = descriptor.value;\n copied.set(key, field);\n }\n record = copied;\n this.records.set(value, record);\n }\n for (const [key, field] of record) {\n text += key.length;\n if (text > limits.maxTextLength)\n throw new SourceError(\n 'resourceLimit',\n path,\n 'Source property names exceed their total string limit.',\n );\n stack.push({\n value: field,\n path: [...path, key],\n depth: depth + 1,\n attribute: attribute || key === 'attrs',\n });\n }\n }\n }\n }\n\n record(value: unknown, path: SourcePath): SourceRecord {\n const record = object(value) ? this.records.get(value) : undefined;\n if (!record) throw new SourceError('invalidInput', path, 'Expected a source node, mark or attributes object.');\n return record;\n }\n\n content(value: unknown, path: SourcePath): readonly unknown[] {\n const values = object(value) ? this.arrays.get(value) : undefined;\n if (!values) throw new SourceError('invalidInput', path, 'Expected a source content or marks array.');\n return values;\n }\n}\n\nconst KNOWN_NODES = new Set([\n 'doc',\n 'paragraph',\n 'text',\n 'heading',\n 'hardBreak',\n 'bulletList',\n 'orderedList',\n 'listItem',\n 'blockquote',\n 'codeBlock',\n]);\nconst BLOCK_NODES = new Set(['paragraph', 'heading', 'bulletList', 'orderedList', 'blockquote', 'codeBlock']);\n\nclass TiptapReader {\n constructor(\n private readonly snapshot: SourceSnapshot,\n private readonly context: FormatContext,\n ) {}\n\n private validateFrames(input: unknown): void {\n interface Frame {\n readonly input: unknown;\n readonly path: SourcePath;\n readonly mark?: boolean;\n readonly parent?: ParentKind;\n }\n const pending: Frame[] = [{ input, path: [] }];\n while (pending.length) {\n const frame = pending.pop();\n if (!frame) break;\n const record = this.snapshot.record(frame.input, frame.path);\n const type = record.get('type');\n if (typeof type !== 'string' || !type)\n throw new SourceError(\n 'invalidInput',\n [...frame.path, 'type'],\n 'A source node or mark needs a nonempty type.',\n );\n if (record.has('attrs')) this.snapshot.record(record.get('attrs'), [...frame.path, 'attrs']);\n if (frame.mark) continue;\n if (frame.parent && KNOWN_NODES.has(type)) {\n const fits =\n frame.parent === 'inline'\n ? type === 'text' || type === 'hardBreak'\n : frame.parent === 'code'\n ? type === 'text'\n : frame.parent === 'list'\n ? type === 'listItem'\n : BLOCK_NODES.has(type);\n if (!fits)\n throw new SourceError(\n 'invalidInput',\n frame.path,\n 'A known source node appears in an invalid parent position.',\n );\n }\n if (type === 'text') {\n if (typeof record.get('text') !== 'string' || !record.get('text'))\n throw new SourceError(\n 'invalidInput',\n [...frame.path, 'text'],\n 'A source text run must contain nonempty text.',\n );\n } else if (record.has('text'))\n throw new SourceError('invalidInput', [...frame.path, 'text'], 'Only a source text node can own text.');\n const content = this.children(record, frame.path);\n if ((type === 'text' || type === 'hardBreak') && content.length)\n throw new SourceError(\n 'invalidInput',\n [...frame.path, 'content'],\n 'This source inline leaf cannot contain children.',\n );\n if (['doc', 'blockquote', 'bulletList', 'orderedList', 'listItem'].includes(type) && !content.length)\n throw new SourceError(\n 'invalidInput',\n [...frame.path, 'content'],\n 'This source structural node requires nonempty content.',\n );\n if (\n type === 'listItem' &&\n this.snapshot.record(content[0], [...frame.path, 'content', 0]).get('type') !== 'paragraph'\n )\n throw new SourceError(\n 'invalidInput',\n [...frame.path, 'content', 0],\n 'A source list item must start with a paragraph.',\n );\n const parent: ParentKind | undefined =\n type === 'paragraph' || type === 'heading'\n ? 'inline'\n : type === 'codeBlock'\n ? 'code'\n : type === 'bulletList' || type === 'orderedList'\n ? 'list'\n : type === 'doc' || type === 'blockquote' || type === 'listItem'\n ? type\n : undefined;\n content.forEach((child, index) =>\n pending.push({ input: child, path: [...frame.path, 'content', index], ...(parent ? { parent } : {}) }),\n );\n if (record.has('marks')) {\n const marks = this.snapshot.content(record.get('marks'), [...frame.path, 'marks']);\n marks.forEach((mark, index) =>\n pending.push({ input: mark, path: [...frame.path, 'marks', index], mark: true }),\n );\n }\n }\n }\n\n private loss(code: FormatDiagnosticCode, path: SourcePath, message: string): void {\n this.context.add(code, 'warning', path, message);\n }\n\n private keys(record: SourceRecord, allowed: readonly string[], path: SourcePath): void {\n for (const key of record.keys())\n if (!allowed.includes(key))\n this.loss(\n 'unsupportedAttribute',\n [...path, key],\n 'A source property outside the fixed profile was omitted.',\n );\n }\n\n private attrs(record: SourceRecord, allowed: readonly string[], path: SourcePath): SourceRecord {\n if (!record.has('attrs')) return new Map();\n const attrs = this.snapshot.record(record.get('attrs'), [...path, 'attrs']);\n this.keys(attrs, allowed, [...path, 'attrs']);\n return attrs;\n }\n\n private children(record: SourceRecord, path: SourcePath): readonly unknown[] {\n return record.has('content') ? this.snapshot.content(record.get('content'), [...path, 'content']) : [];\n }\n\n private marks(record: SourceRecord, path: SourcePath, allowed: boolean): readonly Mark[] {\n if (!record.has('marks')) return [];\n const source = this.snapshot.content(record.get('marks'), [...path, 'marks']);\n if (!allowed && source.length)\n this.loss(\n 'unsupportedMark',\n [...path, 'marks'],\n 'Marks at this source position cannot be represented and were omitted.',\n );\n const marks: Mark[] = [];\n source.forEach((item, index) => {\n const markPath = [...path, 'marks', index];\n const mark = this.snapshot.record(item, markPath);\n this.keys(mark, ['type', 'attrs'], markPath);\n const type = mark.get('type');\n if (typeof type !== 'string' || !type)\n throw new SourceError('invalidInput', [...markPath, 'type'], 'A source mark needs a nonempty type.');\n const attrs = this.attrs(\n mark,\n type === 'link' ? ['href', 'title', 'target', 'rel', 'class'] : [],\n markPath,\n );\n let converted: Mark | undefined;\n if (type === 'bold' || type === 'italic' || type === 'underline' || type === 'strike' || type === 'code')\n converted = { type };\n else if (type === 'link') {\n const href = attrs.get('href');\n if (typeof href !== 'string')\n throw new SourceError(\n 'invalidInput',\n [...markPath, 'attrs', 'href'],\n 'A source link needs a string URL.',\n );\n for (const name of ['target', 'rel', 'class']) {\n const value = attrs.get(name);\n if (value !== undefined && value !== null && typeof value !== 'string')\n throw new SourceError(\n 'invalidInput',\n [...markPath, 'attrs', name],\n 'Link metadata must be a string or null.',\n );\n if (value && !(name === 'target' && value === '_self'))\n this.loss(\n 'unsupportedAttribute',\n [...markPath, 'attrs', name],\n 'Nonrepresentable link metadata was omitted.',\n );\n }\n const title = attrs.get('title');\n if (title !== undefined && title !== null && typeof title !== 'string')\n throw new SourceError(\n 'invalidInput',\n [...markPath, 'attrs', 'title'],\n 'A source link title must be a string or null.',\n );\n if (!isSupportedLinkDestination(href, this.context.options.linkPolicy))\n this.loss(\n 'unsafeUrl',\n [...markPath, 'attrs', 'href'],\n 'A source link outside the configured destination policy was omitted.',\n );\n else converted = typeof title === 'string' ? { type: 'link', href, title } : { type: 'link', href };\n } else\n this.loss(\n 'unsupportedMark',\n [...markPath, 'type'],\n 'A mark outside the fixed source profile was omitted.',\n );\n if (allowed && converted) {\n const previous = marks.find((existing) => existing.type === converted.type);\n if (\n previous &&\n previous.type === 'link' &&\n converted.type === 'link' &&\n (previous.href !== converted.href || previous.title !== converted.title)\n )\n throw new SourceError('invalidInput', markPath, 'Conflicting source marks are not accepted.');\n marks.push(converted);\n }\n });\n return marks;\n }\n\n private node(input: unknown, path: SourcePath, parent: ParentKind): ConvertedNode | undefined {\n const record = this.snapshot.record(input, path);\n this.keys(record, ['type', 'attrs', 'content', 'marks', 'text'], path);\n const type = record.get('type');\n if (typeof type !== 'string' || !type)\n throw new SourceError('invalidInput', [...path, 'type'], 'A source node needs a nonempty type.');\n if (!KNOWN_NODES.has(type)) {\n this.loss(\n 'unsupportedNode',\n [...path, 'type'],\n 'An unsupported source node and its entire subtree were omitted.',\n );\n return undefined;\n }\n const fits =\n parent === 'inline'\n ? type === 'text' || type === 'hardBreak'\n : parent === 'code'\n ? type === 'text'\n : parent === 'list'\n ? type === 'listItem'\n : BLOCK_NODES.has(type);\n if (!fits)\n throw new SourceError('invalidInput', path, 'A known source node appears in an invalid parent position.');\n const attrs = this.attrs(\n record,\n type === 'heading'\n ? ['level']\n : type === 'orderedList'\n ? ['start', 'type']\n : type === 'codeBlock'\n ? ['language']\n : [],\n path,\n );\n const children = this.children(record, path);\n const marks = this.marks(record, path, type === 'text' && parent !== 'code');\n if (type !== 'text' && record.has('text'))\n throw new SourceError('invalidInput', [...path, 'text'], 'Only a source text node can own text.');\n if (type === 'text') {\n if (children.length)\n throw new SourceError('invalidInput', [...path, 'content'], 'Source text cannot contain children.');\n const text = record.get('text');\n if (typeof text !== 'string' || !text.length)\n throw new SourceError(\n 'invalidInput',\n [...path, 'text'],\n 'A source text run must contain nonempty text.',\n );\n if (parent === 'code' ? /\\r/u.test(text) : /[\\r\\n]/u.test(text))\n throw new SourceError(\n 'invalidInput',\n [...path, 'text'],\n 'Source line endings cannot be represented at this text position.',\n );\n return marks.length ? { type: 'text', text, marks } : { type: 'text', text };\n }\n if (type === 'hardBreak') {\n if (children.length)\n throw new SourceError('invalidInput', [...path, 'content'], 'A hard break cannot contain children.');\n return { type: 'hardBreak' };\n }\n if (type === 'paragraph' || type === 'heading') {\n const inline = children.flatMap((child, index) => {\n const converted = this.node(child, [...path, 'content', index], 'inline');\n return converted?.type === 'text' || converted?.type === 'hardBreak' ? [converted] : [];\n });\n if (type === 'paragraph') return { type, children: inline };\n const level = attrs.has('level') ? attrs.get('level') : 1;\n if (level !== 1 && level !== 2 && level !== 3 && level !== 4 && level !== 5 && level !== 6)\n throw new SourceError(\n 'invalidInput',\n [...path, 'attrs', 'level'],\n 'A heading level must be between one and six.',\n );\n const heading: HeadingNode = { type, level, children: inline };\n return heading;\n }\n if (type === 'codeBlock') {\n const runs: TextNode[] = [];\n children.forEach((child, index) => {\n const converted = this.node(child, [...path, 'content', index], 'code');\n if (converted?.type === 'text') runs.push(converted);\n });\n const text = runs.map((run) => run.text).join('');\n const language = attrs.get('language');\n if (\n language !== undefined &&\n language !== null &&\n (typeof language !== 'string' || hasControlCharacters(language))\n )\n throw new SourceError(\n 'invalidInput',\n [...path, 'attrs', 'language'],\n 'Code language must be plain string metadata or null.',\n );\n const content: TextNode[] = text ? [{ type: 'text', text }] : [];\n return typeof language === 'string' ? { type, language, children: content } : { type, children: content };\n }\n if (!children.length)\n throw new SourceError(\n 'invalidInput',\n [...path, 'content'],\n 'This source structural node requires nonempty content.',\n );\n if (type === 'bulletList' || type === 'orderedList') {\n const items: ListItemNode[] = [];\n children.forEach((child, index) => {\n const result = this.node(child, [...path, 'content', index], 'list');\n if (result?.type === 'listItem') items.push(result);\n });\n if (!items.length) {\n items.push({ type: 'listItem', children: [{ type: 'paragraph', children: [] }] });\n this.loss('structuralRewrite', path, 'An empty sanitized list received an empty item.');\n }\n if (type === 'bulletList') return { type, children: items };\n const start = attrs.has('start') ? attrs.get('start') : 1;\n if (typeof start !== 'number' || !Number.isSafeInteger(start))\n throw new SourceError(\n 'invalidInput',\n [...path, 'attrs', 'start'],\n 'A list start must be a safe integer.',\n );\n const numbering = attrs.get('type');\n if (numbering !== undefined && numbering !== null && typeof numbering !== 'string')\n throw new SourceError(\n 'invalidInput',\n [...path, 'attrs', 'type'],\n 'List numbering metadata must be a string or null.',\n );\n if (numbering && numbering !== '1')\n this.loss('unsupportedAttribute', [...path, 'attrs', 'type'], 'Nondecimal list numbering was omitted.');\n return { type, start, children: items };\n }\n if (type === 'listItem' || type === 'blockquote') {\n const blocks: BlockNode[] = [];\n children.forEach((child, index) => {\n const result = this.node(child, [...path, 'content', index], type);\n if (result && result.type !== 'text' && result.type !== 'hardBreak' && result.type !== 'listItem')\n blocks.push(result);\n });\n if (type === 'listItem' && blocks[0]?.type !== 'paragraph')\n throw new SourceError(\n 'invalidInput',\n [...path, 'content', 0],\n 'A source list item must start with a paragraph.',\n );\n if (!blocks.length) {\n blocks.push({ type: 'paragraph', children: [] });\n this.loss('structuralRewrite', path, 'An empty sanitized structural block received a paragraph.');\n }\n return { type, children: blocks };\n }\n throw new SourceError('invalidInput', path, 'The source node cannot be converted.');\n }\n\n document(input: unknown): DocumentNode {\n this.validateFrames(input);\n const root = this.snapshot.record(input, []);\n if (root.get('type') !== 'doc')\n throw new SourceError(\n 'invalidInput',\n ['type'],\n 'The source root must be a document node from the selected profile.',\n );\n this.keys(root, ['type', 'attrs', 'content', 'marks'], []);\n this.attrs(root, [], []);\n this.marks(root, [], false);\n const source = this.children(root, []);\n if (!source.length)\n throw new SourceError('invalidInput', ['content'], 'A source document requires at least one block.');\n const children: BlockNode[] = [];\n source.forEach((child, index) => {\n const result = this.node(child, ['content', index], 'doc');\n if (result && result.type !== 'text' && result.type !== 'hardBreak' && result.type !== 'listItem')\n children.push(result);\n });\n return { type: 'document', children: children.length ? children : [{ type: 'paragraph', children: [] }] };\n }\n}\n\n/** Pure migration of one explicit JSON profile. Never reads HTML, editor instances or runtime storage. */\nexport function importTiptapJson(input: unknown, options: TiptapImportOptions): FormatResult<DocumentNode> {\n return formatOperation({}, (outer) => {\n if (options?.profile !== 'tiptap-3-starterkit') {\n outer.add('unsupportedProfile', 'error', [], 'An explicit supported Tiptap data profile is required.');\n return outer.failure();\n }\n if (options.onLoss !== undefined && options.onLoss !== 'reject' && options.onLoss !== 'sanitize') {\n outer.add('invalidInput', 'error', [], 'The migration loss policy is not supported.');\n return outer.failure();\n }\n const limits = migrationLimits(options);\n const target: FormatOptions = {\n onLoss: options.onLoss ?? 'reject',\n htmlLimits: { maxDiagnostics: limits.maxDiagnostics },\n ...(options.limits ? { limits: options.limits } : {}),\n ...(options.linkPolicy ? { linkPolicy: options.linkPolicy } : {}),\n };\n return formatOperation(target, (context) => {\n try {\n const snapshot = new SourceSnapshot(input, limits);\n const converted = new TiptapReader(snapshot, context).document(input);\n const native = validateDocument(converted, options);\n for (const diagnostic of native.diagnostics)\n context.add(\n diagnostic.severity === 'info'\n ? 'normalized'\n : diagnostic.code === 'resourceLimit'\n ? 'resourceLimit'\n : 'invalidDocument',\n diagnostic.severity,\n diagnostic.path.map((part) => (part === 'children' ? 'content' : part)),\n diagnostic.message,\n );\n return native.ok ? context.finish(native.value) : context.failure();\n } catch (error: unknown) {\n if (!(error instanceof SourceError)) throw error;\n context.add(error.code, 'error', error.path, error.message);\n return context.failure();\n }\n });\n });\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { DocumentOptions } from '@3kles/richtext-core';
|
|
2
|
+
/** Fixed data profile, not runtime API or extension compatibility. */
|
|
3
|
+
export type TiptapImportProfile = 'tiptap-3-starterkit';
|
|
4
|
+
export interface TiptapImportLimits {
|
|
5
|
+
/** JSON container nesting, including content/marks arrays and attrs objects. */
|
|
6
|
+
readonly maxDepth: number;
|
|
7
|
+
/** Source JSON values, including containers, fields and array entries. */
|
|
8
|
+
readonly maxNodes: number;
|
|
9
|
+
/** Sum of source string values and property names, in UTF-16 units. */
|
|
10
|
+
readonly maxTextLength: number;
|
|
11
|
+
readonly maxAttributesPerObject: number;
|
|
12
|
+
/** Attribute name/value length; paragraph/code text uses the total text budget. */
|
|
13
|
+
readonly maxAttributeLength: number;
|
|
14
|
+
readonly maxDiagnostics: number;
|
|
15
|
+
}
|
|
16
|
+
export declare const DEFAULT_TIPTAP_IMPORT_LIMITS: TiptapImportLimits;
|
|
17
|
+
export interface TiptapImportOptions extends DocumentOptions {
|
|
18
|
+
readonly profile: TiptapImportProfile;
|
|
19
|
+
/** Unknown data and nonrepresentable metadata are rejected unless sanitization is explicit. */
|
|
20
|
+
readonly onLoss?: 'reject' | 'sanitize';
|
|
21
|
+
readonly migrationLimits?: Partial<TiptapImportLimits>;
|
|
22
|
+
}
|