diff --git a/manifest.json b/manifest.json index d09597c..fee9267 100644 --- a/manifest.json +++ b/manifest.json @@ -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", diff --git a/package.json b/package.json index b58eeae..ad0d670 100644 --- a/package.json +++ b/package.json @@ -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", @@ -25,7 +15,7 @@ "@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", @@ -33,8 +23,18 @@ "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" } diff --git a/rollup.config.js b/rollup.config.js index b7f12ac..0d4c0b5 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -45,4 +45,4 @@ if (process.env.BUILD === "dev") { configs.push(DEV_PLUGIN_CONFIG); } -export default configs; \ No newline at end of file +export default configs; diff --git a/src/services/cards.ts b/src/services/cards.ts index 238ea86..0da8b3e 100644 --- a/src/services/cards.ts +++ b/src/services/cards.ts @@ -4,7 +4,6 @@ import { FileSystemAdapter, FrontMatterCache, Notice, - parseFrontMatterEntry, TFile, } from "obsidian"; import { Parser } from "src/services/parser"; @@ -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("::"); @@ -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); @@ -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( @@ -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"); } @@ -185,11 +188,7 @@ export class CardsService { } } - private async insertCardsOnAnki( - cardsToCreate: Card[], - frontmatter: FrontMatterCache, - deckName: string - ): Promise { + private async insertCardsOnAnki(cardsToCreate: Card[]): Promise { if (cardsToCreate.length) { let insertedCards = 0; try { @@ -212,7 +211,6 @@ export class CardsService { card.reversed ? (total += 2) : total++; }); - this.updateFrontmatter(frontmatter, deckName); this.writeAnkiBlocks(cardsToCreate); this.notifications.push( @@ -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[]) {