From 6a9848ebe70358b373288f07796052654b76a48d Mon Sep 17 00:00:00 2001 From: pedr Date: Tue, 21 Feb 2023 14:57:04 -0300 Subject: [PATCH] Desktop: Fixes #7782: Markdown + Front Matter export fails when tag(s) lost (#7820) --- .../InteropService_Exporter_Md_frontmatter.test.ts | 11 +++++++++++ .../interop/InteropService_Exporter_Md_frontmatter.ts | 8 ++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/lib/services/interop/InteropService_Exporter_Md_frontmatter.test.ts b/packages/lib/services/interop/InteropService_Exporter_Md_frontmatter.test.ts index c98b4ef16..1087118d4 100644 --- a/packages/lib/services/interop/InteropService_Exporter_Md_frontmatter.test.ts +++ b/packages/lib/services/interop/InteropService_Exporter_Md_frontmatter.test.ts @@ -131,4 +131,15 @@ describe('interop/InteropService_Exporter_Md_frontmatter', () => { expect(content).not.toContain('longitude'); expect(content).not.toContain('altitude'); })); + + test('should export note without tag keyword if the tag has been deleted', (async () => { + const folder1 = await Folder.save({ title: 'folder1' }); + const note = await Note.save({ title: 'NoTag', body: '**ma note**', parent_id: folder1.id }); + const tag = await Tag.save({ title: 'tag' }); + await Tag.setNoteTagsByIds(note.id, [tag.id]); + + await Tag.delete(tag.id); + const content = await exportAndLoad(`${exportDir()}/folder1/NoTag.md`); + expect(content).not.toContain('tag'); + })); }); diff --git a/packages/lib/services/interop/InteropService_Exporter_Md_frontmatter.ts b/packages/lib/services/interop/InteropService_Exporter_Md_frontmatter.ts index e2c7713a4..c713e1e85 100644 --- a/packages/lib/services/interop/InteropService_Exporter_Md_frontmatter.ts +++ b/packages/lib/services/interop/InteropService_Exporter_Md_frontmatter.ts @@ -127,8 +127,12 @@ export default class InteropService_Exporter_Md_frontmatter extends InteropServi const context: FrontMatterContext = this.context(); if (context.noteTags[note.id]) { const tagIds = context.noteTags[note.id]; - const tags = tagIds.map((id: string) => context.tagTitles[id]).sort(); - md['tags'] = tags; + // In some cases a NoteTag can still exist, while the Tag does not. In this case, tagTitles + // for that tagId will return undefined, which can't be handled by the yaml library (issue #7782) + const tags = tagIds.map((id: string) => context.tagTitles[id]).filter(e => !!e).sort(); + if (tags.length > 0) { + md['tags'] = tags; + } } // This guarentees that fields will always be ordered the same way