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 bug introduced by yaml properties update #196

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
41 changes: 9 additions & 32 deletions src/services/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class CardsService {
this.insertMedias(cards, sourcePath);
await this.deleteCardsOnAnki(cardsToDelete, ankiBlocks);
await this.updateCardsOnAnki(cardsToUpdate);
await this.insertCardsOnAnki(cardsToCreate, frontmatter, deckName);
await this.insertCardsOnAnki(cardsToCreate);

// Update decks if needed
const deckNeedToBeChanged = await this.deckNeedToBeChanged(
Expand All @@ -142,6 +142,8 @@ export class CardsService {
}
}

this.updateFrontmatter(frontmatter, deckName);

if (!this.notifications.length) {
this.notifications.push("Nothing to do. Everything is up to date");
}
Expand Down Expand Up @@ -185,11 +187,7 @@ export class CardsService {
}
}

private async insertCardsOnAnki(
cardsToCreate: Card[],
frontmatter: FrontMatterCache,
deckName: string
): Promise<number> {
private async insertCardsOnAnki(cardsToCreate: Card[]): Promise<number> {
if (cardsToCreate.length) {
let insertedCards = 0;
try {
Expand All @@ -212,7 +210,6 @@ export class CardsService {
card.reversed ? (total += 2) : total++;
});

this.updateFrontmatter(frontmatter, deckName);
this.writeAnkiBlocks(cardsToCreate);

this.notifications.push(
Expand All @@ -227,31 +224,11 @@ export class CardsService {
}

private updateFrontmatter(frontmatter: FrontMatterCache, deckName: string) {
let newFrontmatter = "";
const cardsDeckLine = `cards-deck: ${deckName}\n`;
if (frontmatter) {
const oldFrontmatter: string = this.file.substring(
frontmatter.position.start.offset,
frontmatter.position.end.offset
);
if (!oldFrontmatter.match(this.regex.cardsDeckLine)) {
newFrontmatter =
oldFrontmatter.substring(0, oldFrontmatter.length - 3) +
cardsDeckLine +
"---";
this.totalOffset += cardsDeckLine.length;
this.file =
newFrontmatter +
this.file.substring(
frontmatter.position.end.offset,
this.file.length + 1
);
}
} else {
newFrontmatter = `---\n${cardsDeckLine}---\n\n`;
this.totalOffset += newFrontmatter.length;
this.file = newFrontmatter + this.file;
}
const activeFile = this.app.workspace.getActiveFile()

this.app.fileManager.processFrontMatter(activeFile, (frontmatter) => {
frontmatter["cards-deck"] = deckName;
});
}

private writeAnkiBlocks(cardsToCreate: Card[]) {
Expand Down