Skip to content

Commit a77f562

Browse files
chaecrambbecknik
authored andcommitted
fix: bug introduced by frontmatter update
reuseman#196
1 parent dae1854 commit a77f562

File tree

2 files changed

+39
-59
lines changed

2 files changed

+39
-59
lines changed

src/entities/card.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CODE_DECK_EXTENSION } from "src/conf/constants";
22
import { arraysEqual } from "src/utils";
33

44
export abstract class Card {
5-
id: number;
5+
id: number | null;
66
deckName: string;
77
initialContent: string;
88
fields: Record<string, string>;

src/services/cards.ts

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ export class CardsService {
6060
let deckName = "";
6161
if (parseFrontMatterEntry(frontmatter, "cards-deck")) {
6262
deckName = parseFrontMatterEntry(frontmatter, "cards-deck");
63-
} else if (this.settings.folderBasedDeck && activeFile.parent?.path !== "/") {
63+
} else if (
64+
this.settings.folderBasedDeck &&
65+
activeFile.parent?.path !== "/"
66+
) {
6467
// If the current file is in the path "programming/java/strings.md" then the deck name is "programming::java"
6568
// TODO parent might be undefined
6669
deckName = activeFile.parent!.path.split("/").join("::");
@@ -191,68 +194,45 @@ export class CardsService {
191194
frontmatter: FrontMatterCache,
192195
deckName: string
193196
): Promise<number | undefined> {
194-
if (cardsToCreate.length) {
195-
let insertedCards = 0;
196-
try {
197-
const ids = await this.anki.addCards(cardsToCreate);
198-
// Add IDs from response to Flashcard[]
199-
ids.map((id: number, index: number) => {
200-
cardsToCreate[index].id = id;
201-
});
202-
203-
let total = 0;
204-
cardsToCreate.forEach((card) => {
205-
if (card.id === null) {
206-
new Notice(
207-
`Error, could not add: '${card.initialContent}'`,
208-
NOTICE_TIMEOUT
209-
);
210-
} else {
211-
card.reversed ? (insertedCards += 2) : insertedCards++;
212-
}
213-
card.reversed ? (total += 2) : total++;
214-
});
197+
if (cardsToCreate.length === 0) return;
215198

216-
this.updateFrontmatter(frontmatter, deckName);
217-
this.writeAnkiBlocks(cardsToCreate);
199+
let ids: number[] | undefined = undefined;
200+
try {
201+
ids = await this.anki.addCards(cardsToCreate);
202+
} catch (err) {
203+
console.error(err);
204+
throw Error("Error: Could not write cards on Anki");
205+
}
218206

219-
this.notifications.push(
220-
`Inserted successfully ${insertedCards}/${total} cards.`
207+
ids.forEach((id: number, index: number) => (cardsToCreate[index].id = id));
208+
209+
let cardsInserted = 0;
210+
let cardsTotal = 0;
211+
cardsToCreate.forEach((card) => {
212+
cardsTotal += card.reversed ? 2 : 1;
213+
if (card.id !== null) {
214+
cardsInserted += card.reversed ? 2 : 1;
215+
} else {
216+
new Notice(
217+
`Error, could not add: '${card.initialContent}'`,
218+
NOTICE_TIMEOUT
221219
);
222-
return insertedCards;
223-
} catch (err) {
224-
console.error(err);
225-
Error("Error: Could not write cards on Anki");
226220
}
227-
}
228-
}
221+
});
229222

230-
private updateFrontmatter(frontmatter: FrontMatterCache, deckName: string) {
231-
let newFrontmatter = "";
232-
const cardsDeckLine = `cards-deck: ${deckName}\n`;
233-
if (frontmatter) {
234-
const oldFrontmatter: string = this.file.substring(
235-
frontmatter.position.start.offset,
236-
frontmatter.position.end.offset
237-
);
238-
if (!oldFrontmatter.match(this.regex.cardsDeckLine)) {
239-
newFrontmatter =
240-
oldFrontmatter.substring(0, oldFrontmatter.length - 3) +
241-
cardsDeckLine +
242-
"---";
243-
this.totalOffset += cardsDeckLine.length;
244-
this.file =
245-
newFrontmatter +
246-
this.file.substring(
247-
frontmatter.position.end.offset,
248-
this.file.length + 1
249-
);
250-
}
251-
} else {
252-
newFrontmatter = `---\n${cardsDeckLine}---\n\n`;
253-
this.totalOffset += newFrontmatter.length;
254-
this.file = newFrontmatter + this.file;
255-
}
223+
// update frontmatter
224+
// TODO this might not be needed?
225+
const activeFile = this.app.workspace.getActiveFile()!;
226+
this.app.fileManager.processFrontMatter(activeFile, (frontmatter) => {
227+
frontmatter["cards-deck"] = deckName;
228+
});
229+
230+
this.writeAnkiBlocks(cardsToCreate);
231+
232+
this.notifications.push(
233+
`Inserted successfully ${cardsInserted}/${cardsTotal} cards.`
234+
);
235+
return cardsInserted;
256236
}
257237

258238
private writeAnkiBlocks(cardsToCreate: Card[]) {

0 commit comments

Comments
 (0)