From c858e516bb20096d15ef1bcd4fa9c5c60ae8301d Mon Sep 17 00:00:00 2001 From: "Mae B. Morella" <4561733+mmorella-dev@users.noreply.github.com> Date: Sat, 27 Jan 2024 05:38:35 -0500 Subject: [PATCH] fix: Fixed issue with Simplenote importer not titling multi-line documents (#2798) --- .../SimplenoteConverter.spec.ts | 2 +- .../SimplenoteConverter.ts | 21 +++++++++++++------ .../Import/SimplenoteConverter/testData.ts | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/ui-services/src/Import/SimplenoteConverter/SimplenoteConverter.spec.ts b/packages/ui-services/src/Import/SimplenoteConverter/SimplenoteConverter.spec.ts index 3ab5bb378..8683d65f0 100644 --- a/packages/ui-services/src/Import/SimplenoteConverter/SimplenoteConverter.spec.ts +++ b/packages/ui-services/src/Import/SimplenoteConverter/SimplenoteConverter.spec.ts @@ -40,7 +40,7 @@ describe('SimplenoteConverter', () => { expect(result?.[1].uuid).not.toBeNull() expect(result?.[1].content_type).toBe('Note') expect(result?.[1].content.title).toBe('Testing 2') - expect(result?.[1].content.text).toBe("This is the 2nd note's content.") + expect(result?.[1].content.text).toBe("This is...\r\nthe 2nd note's content.") expect(result?.[1].content.trashed).toBe(false) expect(result?.[2].created_at).toBeInstanceOf(Date) diff --git a/packages/ui-services/src/Import/SimplenoteConverter/SimplenoteConverter.ts b/packages/ui-services/src/Import/SimplenoteConverter/SimplenoteConverter.ts index 98e4d7a9d..bfff99631 100644 --- a/packages/ui-services/src/Import/SimplenoteConverter/SimplenoteConverter.ts +++ b/packages/ui-services/src/Import/SimplenoteConverter/SimplenoteConverter.ts @@ -15,6 +15,17 @@ type SimplenoteData = { const isSimplenoteEntry = (entry: any): boolean => entry.id && entry.content != undefined && entry.creationDate && entry.lastModified +const splitAtFirst = (str: string, delim: string): [string, string] | [] => { + const indexOfDelimiter = str.indexOf(delim) + const hasDelimiter = indexOfDelimiter > -1 + if (!hasDelimiter) { + return [] + } + const before = str.slice(0, indexOfDelimiter) + const after = str.slice(indexOfDelimiter + delim.length) + return [before, after] +} + export class SimplenoteConverter implements Converter { constructor() {} @@ -58,17 +69,15 @@ export class SimplenoteConverter implements Converter { const createdAtDate = new Date(item.creationDate) const updatedAtDate = new Date(item.lastModified) - const splitItemContent = item.content.split('\r\n') - const hasTitleAndContent = splitItemContent.length === 2 - const title = - hasTitleAndContent && splitItemContent[0].length ? splitItemContent[0] : createdAtDate.toLocaleString() - const content = hasTitleAndContent && splitItemContent[1].length ? splitItemContent[1] : item.content + const splitContent = splitAtFirst(item.content, '\r\n') + const title = splitContent[0] ?? createdAtDate.toLocaleString() + const text = splitContent[1] ?? item.content return createNote({ createdAt: createdAtDate, updatedAt: updatedAtDate, title, - text: content, + text, trashed, useSuperIfPossible: true, }) diff --git a/packages/ui-services/src/Import/SimplenoteConverter/testData.ts b/packages/ui-services/src/Import/SimplenoteConverter/testData.ts index cfb0b5120..abbef96ef 100644 --- a/packages/ui-services/src/Import/SimplenoteConverter/testData.ts +++ b/packages/ui-services/src/Import/SimplenoteConverter/testData.ts @@ -2,7 +2,7 @@ const data = { activeNotes: [ { id: '43349052-4efa-48c2-bdd6-8323124451b1', - content: "Testing 2\r\nThis is the 2nd note's content.", + content: "Testing 2\r\nThis is...\r\nthe 2nd note's content.", creationDate: '2020-06-08T21:28:43.856Z', lastModified: '2021-04-16T06:21:53.124Z', },