-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrich-text-tiptap.ts
More file actions
609 lines (577 loc) · 19.4 KB
/
Copy pathrich-text-tiptap.ts
File metadata and controls
609 lines (577 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import type {
AnyRichTextBlockElement,
AnyRichTextSectionElement,
RichTextBlock,
RichTextSectionElementStyleWithCode,
RichTextSectionLink
} from 'slack-web-api-client';
import { sanitizeHref } from './url-safety';
type RichStyle = RichTextSectionElementStyleWithCode;
/**
* Minimal ProseMirror node shape we read/write. Mirrors the JSON format
* TipTap emits via `editor.getJSON()` and accepts via `setContent`.
* We avoid pulling in `prosemirror-model` types so this file stays
* dependency-free for tests.
*/
export interface PMNode {
type: string;
attrs?: Record<string, unknown>;
content?: PMNode[];
marks?: { type: string; attrs?: Record<string, unknown> }[];
text?: string;
}
const SUPPORTED_BLOCK_KINDS = new Set([
'rich_text_section',
'rich_text_list',
'rich_text_quote',
'rich_text_preformatted'
]);
const SUPPORTED_INLINE_KINDS = new Set(['text', 'link', 'emoji']);
const SUPPORTED_TEXT_STYLE_KEYS = new Set(['bold', 'italic', 'strike', 'underline', 'code']);
/**
* Slack's documented maximum value for `indent` on a rich_text_list.
* Deeper nesting is flattened to this depth on export.
*/
const MAX_LIST_INDENT = 8;
/**
* Reasons a Slack rich_text payload can't round-trip cleanly through the
* TipTap WYSIWYG (paragraphs, lists, blockquote, code block, emoji, plus
* bold/italic/strike/underline/code marks and links). Anything else (mentions,
* broadcasts, list indent, etc.) ends up in this list so the UI
* can offer the structured editor instead.
*/
export interface LossyReason {
/** Short label suitable for a UI badge or list item. */
label: string;
/** Path within the rich_text payload, for diagnostics. */
where: string;
}
/**
* Inspects a rich_text block and reports anything the WYSIWYG converter
* would drop on a round trip.
* @param block - the rich_text block to inspect
* @returns array of reasons; an empty array means safe to round-trip
*/
export function detectLossy(block: RichTextBlock): LossyReason[] {
const out: LossyReason[] = [];
const blocks = block.elements ?? [];
blocks.forEach((el, blockIdx) => {
const where = `elements[${blockIdx}]`;
if (!SUPPORTED_BLOCK_KINDS.has(el.type)) {
out.push({ label: `Unsupported element: ${el.type}`, where });
return;
}
if (el.type === 'rich_text_list') {
if ((el.border ?? 0) > 0) {
out.push({ label: 'List border', where });
}
el.elements?.forEach((item, itemIdx) => {
scanInlines(item.elements ?? [], `${where}.elements[${itemIdx}]`, out);
});
return;
}
if ('border' in el && (el.border ?? 0) > 0) {
out.push({ label: 'Quote/preformatted border', where });
}
scanInlines(el.elements ?? [], where, out);
});
return out;
}
/**
* Walks inline elements and pushes a LossyReason for each unsupported feature.
* @param inlines - inline elements to scan
* @param where - dotted path describing the location, used in diagnostics
* @param out - accumulator the helper appends LossyReason entries to
*/
function scanInlines(inlines: AnyRichTextSectionElement[], where: string, out: LossyReason[]) {
inlines.forEach((inline, i) => {
const sub = `${where}.elements[${i}]`;
if (!SUPPORTED_INLINE_KINDS.has(inline.type)) {
out.push({ label: `Inline: ${inline.type}`, where: sub });
return;
}
if (inline.type === 'text') {
const style = inline.style;
if (style) {
for (const k of Object.keys(style)) {
if (!SUPPORTED_TEXT_STYLE_KEYS.has(k)) {
out.push({ label: `Text style: ${k}`, where: sub });
}
}
}
}
});
}
/** The Slack emoji element shape we read on import. */
interface SlackEmojiElement {
type: 'emoji';
name?: string;
unicode?: string;
skin_tone?: number;
}
/**
* Resolves the render-only attributes (`src`, `unicode`) of a Slack emoji
* element when importing into the WYSIWYG. Lets the editor inject a custom
* emoji image (from `customEmojis`) and a glyph codepoint (from
* `emoji-datasource`) without this module depending on either. Optional: when
* omitted, emoji carry only what the payload already specifies.
* @param el - the Slack emoji element
* @returns the PM emoji node attrs
*/
export type EmojiImportResolver = (el: SlackEmojiElement) => {
name: string;
src: string | null;
unicode: string | null;
skinTone: number | null;
};
const defaultEmojiResolver: EmojiImportResolver = (el) => ({
name: el.name ?? '',
src: null,
unicode: el.unicode ?? null,
skinTone: el.skin_tone ?? null
});
/**
* Converts a Slack rich_text block to a TipTap-compatible ProseMirror
* `doc` node. Anything not in {@link detectLossy}'s supported set is
* dropped (callers should pre-flight with `detectLossy`).
* @param block - the rich_text block to convert
* @param resolveEmoji - optional resolver for emoji `src` / `unicode`
* @returns a ProseMirror `doc` node
*/
export function richTextToProseMirror(
block: RichTextBlock,
resolveEmoji: EmojiImportResolver = defaultEmojiResolver
): PMNode {
const content: PMNode[] = [];
// Track the most-recent list at each indent level so we can append
// sibling rich_text_list elements with the same indent and nest deeper
// ones inside the parent's last listItem.
const listsByIndent = new Map<number, PMNode>();
for (const el of block.elements ?? []) {
if (el.type === 'rich_text_list') {
appendRichTextList(el, content, listsByIndent, resolveEmoji);
continue;
}
listsByIndent.clear();
const node = blockElementToPM(el, resolveEmoji);
if (node) {
content.push(node);
}
}
return {
type: 'doc',
content: content.length > 0 ? content : [{ type: 'paragraph' }]
};
}
/**
* Appends a Slack rich_text_list as a ProseMirror list, nesting deeper
* indents inside the previous level's last list item.
* @param el - the Slack rich_text_list element
* @param topLevel - the running array of top-level ProseMirror nodes
* @param listsByIndent - map tracking the open list at each indent level
*/
function appendRichTextList(
el: Extract<AnyRichTextBlockElement, { type: 'rich_text_list' }>,
topLevel: PMNode[],
listsByIndent: Map<number, PMNode>,
resolveEmoji: EmojiImportResolver
) {
const indent = el.indent ?? 0;
const listType = el.style === 'ordered' ? 'orderedList' : 'bulletList';
const items: PMNode[] = (el.elements ?? []).map((section) => ({
type: 'listItem',
content: [{ type: 'paragraph', content: inlinesToPM(section.elements ?? [], resolveEmoji) }]
}));
// Drop any deeper-indent lists from the running map; we've left them.
for (const k of Array.from(listsByIndent.keys())) {
if (k > indent) {
listsByIndent.delete(k);
}
}
const existing = listsByIndent.get(indent);
if (existing && existing.type === listType) {
existing.content = [...(existing.content ?? []), ...items];
return;
}
const list: PMNode = { type: listType, content: items };
listsByIndent.set(indent, list);
if (indent === 0) {
topLevel.push(list);
return;
}
const parent = listsByIndent.get(indent - 1);
if (!parent?.content || parent.content.length === 0) {
// Skipped indent levels (uncommon). Fall back to top-level.
topLevel.push(list);
return;
}
const parentLastItem = parent.content[parent.content.length - 1];
parentLastItem.content = [...(parentLastItem.content ?? []), list];
}
/**
* Converts a single Slack rich_text block element to a ProseMirror node.
* Lists are handled separately via {@link appendRichTextList}.
* @param el - the Slack rich_text block element
* @returns the corresponding ProseMirror node, or null for list elements
*/
function blockElementToPM(el: AnyRichTextBlockElement, resolveEmoji: EmojiImportResolver): PMNode | null {
if (el.type === 'rich_text_section') {
return {
type: 'paragraph',
content: inlinesToPM(el.elements ?? [], resolveEmoji)
};
}
if (el.type === 'rich_text_quote') {
return {
type: 'blockquote',
content: [
{
type: 'paragraph',
content: inlinesToPM(el.elements ?? [], resolveEmoji)
}
]
};
}
if (el.type === 'rich_text_preformatted') {
const text = (el.elements ?? []).map((i) => (i.type === 'text' ? i.text : '')).join('');
const node: PMNode = { type: 'codeBlock' };
if (text) {
node.content = [{ type: 'text', text }];
}
return node;
}
// rich_text_list is handled separately by appendRichTextList so we can
// re-nest indented lists into ProseMirror's nested-list shape.
return null;
}
/**
* Pushes a Slack text run onto a ProseMirror inline array, converting any
* embedded newlines into `hardBreak` nodes so they render as soft line
* breaks in the editor (and round-trip back to `\n` on export). A run of
* N consecutive newlines becomes N hardBreaks, so blank lines (`\n\n`)
* are preserved. Without this, a `\n` inside a Slack text element would
* become a literal newline character in a ProseMirror text node, which
* the browser collapses to a space.
* @param out - the inline array to append to
* @param text - the Slack text run, possibly containing `\n`
* @param marks - marks to apply to each text segment
*/
function pushTextWithHardBreaks(
out: PMNode[],
text: string,
marks: { type: string; attrs?: Record<string, unknown> }[]
) {
const segments = text.split('\n');
segments.forEach((segment, i) => {
if (i > 0) {
out.push({ type: 'hardBreak' });
}
if (segment) {
out.push({ type: 'text', text: segment, marks });
}
});
}
/**
* Converts Slack inline section elements (text, link) to ProseMirror text
* nodes with the appropriate marks. Unsupported types are silently dropped.
* @param inlines - inline elements from a section, quote, or list item
* @param resolveEmoji - resolver for emoji `src` / `unicode`
* @returns the corresponding ProseMirror text nodes
*/
function inlinesToPM(inlines: AnyRichTextSectionElement[], resolveEmoji: EmojiImportResolver): PMNode[] {
const out: PMNode[] = [];
for (const inline of inlines) {
if (inline.type === 'text') {
if (!inline.text) {
continue;
}
pushTextWithHardBreaks(out, inline.text, styleToMarks(inline.style));
} else if (inline.type === 'emoji') {
const attrs = resolveEmoji(inline as SlackEmojiElement);
if (!attrs.name) {
continue;
}
out.push({ type: 'emoji', attrs });
} else if (inline.type === 'link') {
const link = inline as RichTextSectionLink;
const text = link.text || link.url || '';
if (!text) {
continue;
}
out.push({
type: 'text',
text,
marks: [...styleToMarks(link.style as RichStyle | undefined), { type: 'link', attrs: { href: link.url } }]
});
}
// unsupported inlines are silently dropped (caller pre-flighted)
}
return out;
}
/**
* Converts Slack rich_text style flags to ProseMirror marks.
* @param style - the Slack style flags, if any
* @returns the corresponding ProseMirror marks (may be empty)
*/
function styleToMarks(style: RichStyle | undefined): { type: string; attrs?: Record<string, unknown> }[] {
if (!style) {
return [];
}
const marks: { type: string }[] = [];
if (style.bold) {
marks.push({ type: 'bold' });
}
if (style.italic) {
marks.push({ type: 'italic' });
}
if (style.strike) {
marks.push({ type: 'strike' });
}
if (style.underline) {
marks.push({ type: 'underline' });
}
if (style.code) {
marks.push({ type: 'code' });
}
return marks;
}
/**
* Converts a TipTap `doc` node back to a Slack rich_text block.
* Inverse of {@link richTextToProseMirror}.
* @param doc - the ProseMirror `doc` node from `editor.getJSON()`
* @returns a fresh rich_text block
*/
export function proseMirrorToRichText(doc: PMNode): RichTextBlock {
const elements: AnyRichTextBlockElement[] = [];
for (const node of doc.content ?? []) {
pushBlockElements(node, 0, elements);
}
return { type: 'rich_text', elements };
}
/**
* Translates one ProseMirror block-level node into Slack rich_text elements
* and pushes them onto the output array.
* @param node - the ProseMirror node to translate
* @param indent - current list indent depth, used by nested lists
* @param out - accumulator the helper appends Slack elements to
*/
function pushBlockElements(node: PMNode, indent: number, out: AnyRichTextBlockElement[]) {
if (node.type === 'paragraph') {
out.push({
type: 'rich_text_section',
elements: proseMirrorInlinesToRichTextElements(node.content ?? [])
});
return;
}
if (node.type === 'bulletList' || node.type === 'orderedList') {
flattenList(node, indent, out);
return;
}
if (node.type === 'blockquote') {
const para = node.content?.find((n) => n.type === 'paragraph') ?? node.content?.[0];
out.push({
type: 'rich_text_quote',
elements: proseMirrorInlinesToRichTextElements(para?.content ?? [])
});
return;
}
if (node.type === 'codeBlock') {
const text = (node.content ?? []).map((n) => n.text ?? '').join('');
out.push({
type: 'rich_text_preformatted',
elements: [{ type: 'text', text }]
});
}
}
/**
* Flattens a (possibly nested) ProseMirror list into sibling Slack
* rich_text_list elements with increasing `indent` values.
* @param listNode - the ProseMirror bullet or ordered list node
* @param indent - current indent depth (0 at the top level)
* @param out - accumulator the helper appends Slack elements to
*/
function flattenList(listNode: PMNode, indent: number, out: AnyRichTextBlockElement[]) {
const style = listNode.type === 'orderedList' ? 'ordered' : 'bullet';
// Slack rich_text caps `indent` at 8. Clamp here so we never emit a
// payload Slack will reject; deeper nesting will visually flatten in
// the preview but text content is preserved.
const clampedIndent = Math.min(indent, MAX_LIST_INDENT);
// Buffer items at this indent into one rich_text_list. When an item
// has nested lists, flush the buffer, then recurse into the nested
// list at indent + 1, so Slack sees flat sibling lists with deeper
// `indent` values.
let buffered: {
type: 'rich_text_section';
elements: AnyRichTextSectionElement[];
}[] = [];
const flush = () => {
if (buffered.length === 0) {
return;
}
const list: AnyRichTextBlockElement = {
type: 'rich_text_list',
style,
elements: buffered
};
if (clampedIndent > 0) {
(list as { indent?: number }).indent = clampedIndent;
}
out.push(list);
buffered = [];
};
for (const item of listNode.content ?? []) {
if (item.type !== 'listItem') {
continue;
}
let para: PMNode | undefined;
const nested: PMNode[] = [];
for (const child of item.content ?? []) {
if (child.type === 'paragraph' && !para) {
para = child;
} else if (child.type === 'bulletList' || child.type === 'orderedList') {
nested.push(child);
}
}
buffered.push({
type: 'rich_text_section',
elements: proseMirrorInlinesToRichTextElements(para?.content ?? [])
});
if (nested.length > 0) {
flush();
for (const sub of nested) {
flattenList(sub, indent + 1, out);
}
}
}
flush();
}
/**
* Converts ProseMirror inline text nodes (with marks) back to Slack
* inline elements, merging adjacent runs that share the same style.
* @param nodes - ProseMirror text nodes from a paragraph or list item
* @returns the corresponding Slack inline elements
*/
function proseMirrorInlinesToRichTextElements(nodes: PMNode[]): AnyRichTextSectionElement[] {
const out: AnyRichTextSectionElement[] = [];
for (const node of nodes) {
if (node.type === 'emoji') {
const name = String(node.attrs?.name ?? '');
if (!name) {
continue;
}
// Export keeps only Slack's fields: `name` (+ `skin_tone`). The
// render-only `src` / `unicode` attrs are intentionally dropped.
const emoji: AnyRichTextSectionElement = { type: 'emoji', name };
const skinTone = node.attrs?.skinTone;
if (typeof skinTone === 'number' && skinTone >= 2 && skinTone <= 6) {
(emoji as { skin_tone?: number }).skin_tone = skinTone;
}
out.push(emoji);
continue;
}
if (node.type === 'hardBreak') {
// A soft line break (Shift+Enter) becomes a newline in the Slack
// text stream. mergeAdjacentTextRuns folds this into neighbouring
// unstyled runs, so "a" + <br> + "b" collapses to one "a\nb" run.
out.push({ type: 'text', text: '\n' });
continue;
}
if (node.type !== 'text' || !node.text) {
continue;
}
const linkMark = node.marks?.find((m) => m.type === 'link');
const style = marksToStyle(node.marks ?? []);
if (linkMark) {
// TipTap's setLink/toggleLink already gate on isAllowedUri, but
// a link mark can also enter the editor via setContent() (used
// when seeding from a payload). Sanitize once more here so a
// crafted Slack rich_text payload that already contains an unsafe
// href cannot round-trip back out unchanged.
const url = sanitizeHref(String(linkMark.attrs?.href ?? ''));
const link: RichTextSectionLink = {
type: 'link',
url,
text: node.text
};
if (Object.keys(style).length > 0) {
link.style = style as RichTextSectionLink['style'];
}
out.push(link);
} else {
const text: AnyRichTextSectionElement = {
type: 'text',
text: node.text
};
if (Object.keys(style).length > 0) {
(text as { style?: RichStyle }).style = style;
}
out.push(text);
}
}
return mergeAdjacentTextRuns(out);
}
/**
* Converts ProseMirror marks back to Slack rich_text style flags.
* @param marks - the ProseMirror marks attached to a text node
* @returns the corresponding Slack style flag object
*/
function marksToStyle(marks: { type: string }[]): RichStyle {
const style: Record<string, boolean> = {};
for (const m of marks) {
if (m.type === 'bold') {
style.bold = true;
}
if (m.type === 'italic') {
style.italic = true;
}
if (m.type === 'strike') {
style.strike = true;
}
if (m.type === 'underline') {
style.underline = true;
}
if (m.type === 'code') {
style.code = true;
}
}
return style as RichStyle;
}
/**
* Concatenates adjacent text runs that share the same style flags.
* @param inlines - inline elements possibly containing splittable runs
* @returns a new array with adjacent matching text runs merged
*/
function mergeAdjacentTextRuns(inlines: AnyRichTextSectionElement[]): AnyRichTextSectionElement[] {
const out: AnyRichTextSectionElement[] = [];
for (const inline of inlines) {
const prev = out[out.length - 1];
if (
prev &&
prev.type === 'text' &&
inline.type === 'text' &&
sameStyle((prev as { style?: RichStyle }).style, (inline as { style?: RichStyle }).style)
) {
out[out.length - 1] = {
...prev,
text: (prev.text ?? '') + (inline.text ?? '')
};
continue;
}
out.push(inline);
}
return out;
}
/**
* Returns true when two style flag objects have the same set of keys.
* @param a - first style flag object
* @param b - second style flag object
* @returns true when both have the same active flags
*/
function sameStyle(a: RichStyle | undefined, b: RichStyle | undefined): boolean {
const ak = a ? Object.keys(a).sort() : [];
const bk = b ? Object.keys(b).sort() : [];
if (ak.length !== bk.length) {
return false;
}
return ak.every((k, i) => k === bk[i]);
}