Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix duplicate deck names #613

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions server/lib/anki/getDeckname.test.ts
Original file line number Diff line number Diff line change
@@ -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');
})
});
16 changes: 16 additions & 0 deletions server/lib/anki/getDeckname.ts
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion server/lib/notion/BlockHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -399,7 +400,7 @@ class BlockHandler {
'utf8'
);
const deck = new Deck(
parentName ? `${parentName}::${title}` : title,
getDeckName(parentName, title),
cards,
undefined,
NOTION_STYLE,
Expand Down