diff --git a/blocksuite/affine/all/src/__tests__/adapters/notion-text.unit.spec.ts b/blocksuite/affine/all/src/__tests__/adapters/notion-text.unit.spec.ts index f99813352..a4d98aa6e 100644 --- a/blocksuite/affine/all/src/__tests__/adapters/notion-text.unit.spec.ts +++ b/blocksuite/affine/all/src/__tests__/adapters/notion-text.unit.spec.ts @@ -106,4 +106,65 @@ describe('notion-text to snapshot', () => { }); expect(nanoidReplacement(target!)).toEqual(sliceSnapshot); }); + + test('notion text with empty styles array', () => { + const notionText = + '{"blockType":"text","editing":[["a "],["bold text",[["b"]]],[" hello world"]],"selection":{"startIndex":0,"endIndex":23},"action":"copy"}'; + + const sliceSnapshot: SliceSnapshot = { + type: 'slice', + content: [ + { + type: 'block', + id: 'matchesReplaceMap[0]', + flavour: 'affine:note', + props: { + xywh: '[0,0,800,95]', + background: DefaultTheme.noteBackgrounColor, + index: 'a0', + hidden: false, + displayMode: 'both', + }, + children: [ + { + type: 'block', + id: 'matchesReplaceMap[1]', + flavour: 'affine:paragraph', + props: { + type: 'text', + text: { + '$blocksuite:internal:text$': true, + delta: [ + { + insert: 'a ', + }, + { + insert: 'bold text', + attributes: { + bold: true, + }, + }, + { + insert: ' hello world', + }, + ], + }, + }, + children: [], + }, + ], + }, + ], + workspaceId: '', + pageId: '', + }; + + const ntAdapter = new NotionTextAdapter(createJob(), provider); + const target = ntAdapter.toSliceSnapshot({ + file: notionText, + workspaceId: '', + pageId: '', + }); + expect(nanoidReplacement(target!)).toEqual(sliceSnapshot); + }); }); diff --git a/blocksuite/affine/shared/src/adapters/notion-text.ts b/blocksuite/affine/shared/src/adapters/notion-text.ts index fd11bdc0e..b407ea55f 100644 --- a/blocksuite/affine/shared/src/adapters/notion-text.ts +++ b/blocksuite/affine/shared/src/adapters/notion-text.ts @@ -94,28 +94,49 @@ export class NotionTextAdapter extends BaseAdapter { const notionText = JSON.parse(payload.file) as NotionTextSerialized; const content: SliceSnapshot['content'] = []; const deltas: DeltaInsert[] = []; + + // Check if the notionText.editing is an array + if (!Array.isArray(notionText.editing)) { + return null; + } + for (const editing of notionText.editing) { const delta: DeltaInsert = { insert: editing[0], - attributes: Object.create(null), }; - for (const styleElement of editing[1]) { - switch (styleElement[0]) { - case 'b': - delta.attributes!.bold = true; - break; - case 'i': - delta.attributes!.italic = true; - break; - case '_': - delta.attributes!.underline = true; - break; - case 'c': - delta.attributes!.code = true; - break; - case 's': - delta.attributes!.strike = true; - break; + + // Check if the stylesArray of editing[1] is an array + const stylesArray = editing[1]; + if (Array.isArray(stylesArray)) { + for (const styleElement of stylesArray) { + // Skip invalid style entries + if (!styleElement || typeof styleElement[0] !== 'string') { + continue; + } + + // Check if the delta.attributes exists, if not, create a new object + if (!delta.attributes) { + delta.attributes = Object.create(null); + } + + // Add the style to the delta.attributes + switch (styleElement[0]) { + case 'b': + delta.attributes!.bold = true; + break; + case 'i': + delta.attributes!.italic = true; + break; + case '_': + delta.attributes!.underline = true; + break; + case 'c': + delta.attributes!.code = true; + break; + case 's': + delta.attributes!.strike = true; + break; + } } } deltas.push(delta);