Skip to content
Open
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
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "flashcards-obsidian",
"name": "Flashcards",
"version": "1.6.5",
"minAppVersion": "0.9.17",
"minAppVersion": "1.4.0",
"description": "Anki integration",
"author": "Alex Colucci",
"authorUrl": "https://github.com/reuseman",
Expand Down
40 changes: 20 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
{
"name": "flashcards",
"version": "0.2.0",
"description": "An Anki integration.",
"main": "main.js",
"scripts": {
"dev": "rollup --config rollup.config.js -w --environment BUILD:dev",
"build": "rollup --config rollup.config.js --environment BUILD:production",
"test": "jest"
},
"keywords": [
"obsidian",
"anki",
"flashcards"
],
"author": "",
"license": "MIT",
"dependencies": {
"@types/showdown": "^1.9.3",
"showdown": "^1.9.1"
},
"description": "An Anki integration.",
"devDependencies": {
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
Expand All @@ -25,16 +15,26 @@
"@typescript-eslint/parser": "^5.7.0",
"eslint": "^8.4.1",
"jest": "^29.2.2",
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
"obsidian": "latest",
"rollup": "^2.52.2",
"rollup-plugin-copy": "^3.4.0",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"tslib": "^2.3.0",
"typescript": "^4.3.4"
},
"dependencies": {
"@types/showdown": "^1.9.3",
"showdown": "^1.9.1"
}
"keywords": [
"obsidian",
"anki",
"flashcards"
],
"license": "MIT",
"main": "main.js",
"name": "flashcards",
"scripts": {
"build": "rollup --config rollup.config.js --environment BUILD:production",
"dev": "rollup --config rollup.config.js -w --environment BUILD:dev",
"test": "jest"
},
"version": "0.2.0"
}
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ if (process.env.BUILD === "dev") {
configs.push(DEV_PLUGIN_CONFIG);
}

export default configs;
export default configs;
52 changes: 15 additions & 37 deletions src/services/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
FileSystemAdapter,
FrontMatterCache,
Notice,
parseFrontMatterEntry,
TFile,
} from "obsidian";
import { Parser } from "src/services/parser";
Expand Down Expand Up @@ -58,8 +57,8 @@ export class CardsService {
// Parse frontmatter
const frontmatter = fileCachedMetadata.frontmatter;
let deckName = "";
if (parseFrontMatterEntry(frontmatter, "cards-deck")) {
deckName = parseFrontMatterEntry(frontmatter, "cards-deck");
if (frontmatter["cards-deck"]) {
deckName = frontmatter["cards-deck"];
} else if (this.settings.folderBasedDeck && activeFile.parent.path !== "/") {
// If the current file is in the path "programming/java/strings.md" then the deck name is "programming::java"
deckName = activeFile.parent.path.split("/").join("::");
Expand Down Expand Up @@ -92,8 +91,10 @@ export class CardsService {
filePath,
globalTags
);
const [cardsToCreate, cardsToUpdate, cardsNotInAnki] =
this.filterByUpdate(ankiCards, cards);
const [cardsToCreate, cardsToUpdate, cardsNotInAnki] = this.filterByUpdate(
ankiCards,
cards
);
const cardIds: number[] = this.getCardsIds(ankiCards, cards);
const cardsToDelete: number[] = this.parser.getCardsToDelete(this.file);

Expand All @@ -116,7 +117,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 +143,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 +188,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 +211,6 @@ export class CardsService {
card.reversed ? (total += 2) : total++;
});

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

this.notifications.push(
Expand All @@ -227,31 +225,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