From 8d6561fb39849027783f6a57da8cc25a4ecf4ff9 Mon Sep 17 00:00:00 2001 From: Alexander Alemayhu Date: Mon, 18 Apr 2022 09:56:16 +0200 Subject: [PATCH] fix: fix duplicate deck names Closes: #446 --- server/lib/anki/getDeckname.test.ts | 13 +++++++++++++ server/lib/anki/getDeckname.ts | 16 ++++++++++++++++ server/lib/notion/BlockHandler.ts | 3 ++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 server/lib/anki/getDeckname.test.ts create mode 100644 server/lib/anki/getDeckname.ts diff --git a/server/lib/anki/getDeckname.test.ts b/server/lib/anki/getDeckname.test.ts new file mode 100644 index 000000000..bb3222a7b --- /dev/null +++ b/server/lib/anki/getDeckname.test.ts @@ -0,0 +1,13 @@ +import getDeckName from './getDeckname'; + +describe('getDeckname', () => { + it('has no parent', () => { + expect(getDeckName(undefined, 'test')).toBe('test'); + }); + it('has parent', () => { + expect(getDeckName('parent', 'test')).toBe('parent::test'); + }) + it("ignores parent is same as child", () => { + expect(getDeckName('test', 'test')).toBe('test'); + }) +}); diff --git a/server/lib/anki/getDeckname.ts b/server/lib/anki/getDeckname.ts new file mode 100644 index 000000000..84a476a82 --- /dev/null +++ b/server/lib/anki/getDeckname.ts @@ -0,0 +1,16 @@ +/** + * This will collapse children with exact same title to the parent. + * This is safe to do because the parent will be the one that is used to create the deck. + * The user is potentially losing the ability to create a deck with the same name as a parent. + * In the real world this does not really matter but adding this note in case that assumption + * changes. + */ +export default function getDeckName( + parent: string | undefined, + name: string +): string { + if (parent && parent !== name) { + return `${parent}::${name}`; + } + return name; +} diff --git a/server/lib/notion/BlockHandler.ts b/server/lib/notion/BlockHandler.ts index 88a9614ec..1284ad33a 100644 --- a/server/lib/notion/BlockHandler.ts +++ b/server/lib/notion/BlockHandler.ts @@ -39,6 +39,7 @@ import isTesting from './helpers/isTesting'; import BlockEquation from './blocks/BlockEquation'; import renderFront from './helpers/renderFront'; import perserveNewlinesIfApplicable from './helpers/perserveNewlinesIfApplicable'; +import getDeckName from '../anki/getDeckname'; class BlockHandler { api: NotionAPIWrapper; @@ -399,7 +400,7 @@ class BlockHandler { 'utf8' ); const deck = new Deck( - parentName ? `${parentName}::${title}` : title, + getDeckName(parentName, title), cards, undefined, NOTION_STYLE,