generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
cards.ts
427 lines (382 loc) · 12.9 KB
/
cards.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import { Anki } from "src/services/anki";
import {
App,
FileSystemAdapter,
FrontMatterCache,
Notice,
parseFrontMatterEntry,
TFile,
} from "obsidian";
import { Parser } from "src/services/parser";
import { ISettings } from "src/conf/settings";
import { Card } from "src/entities/card";
import { arrayBufferToBase64 } from "src/utils";
import { Regex } from "src/conf/regex";
import { noticeTimeout } from "src/conf/constants";
import { Inlinecard } from "src/entities/inlinecard";
export class CardsService {
private app: App;
private settings: ISettings;
private regex: Regex;
private parser: Parser;
private anki: Anki;
private updateFile: boolean;
private totalOffset: number;
private file: string;
private notifications: string[];
constructor(app: App, settings: ISettings) {
this.app = app;
this.settings = settings;
this.regex = new Regex(this.settings);
this.parser = new Parser(this.regex, this.settings);
this.anki = new Anki();
}
public async execute(activeFile: TFile): Promise<string[]> {
this.regex.update(this.settings);
try {
await this.anki.ping();
} catch (err) {
console.error(err);
return ["Error: Anki must be open with AnkiConnect installed."];
}
// Init for the execute phase
this.updateFile = false;
this.totalOffset = 0;
this.notifications = [];
const filePath = activeFile.basename;
const sourcePath = activeFile.path;
const fileCachedMetadata = this.app.metadataCache.getFileCache(activeFile);
const vaultName = this.app.vault.getName();
let globalTags: string[] = undefined;
// Parse frontmatter
const frontmatter = fileCachedMetadata.frontmatter;
let deckName = "";
if (parseFrontMatterEntry(frontmatter, "cards-deck")) {
deckName = parseFrontMatterEntry(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("::");
} else {
deckName = this.settings.deck;
}
try {
this.anki.storeCodeHighlightMedias();
await this.anki.createModels(
this.settings.sourceSupport,
this.settings.codeHighlightSupport
);
await this.anki.createDeck(deckName);
this.file = await this.app.vault.read(activeFile);
if (!this.file.endsWith("\n")) {
this.file += "\n";
}
globalTags = this.parseGlobalTags(this.file);
// TODO with empty check that does not call ankiCards line
const ankiBlocks = this.parser.getAnkiIDsBlocks(this.file);
const ankiCards = ankiBlocks
? await this.anki.getCards(this.getAnkiIDs(ankiBlocks))
: undefined;
const cards: Card[] = this.parser.generateFlashcards(
this.file,
deckName,
vaultName,
filePath,
globalTags
);
const [cardsToCreate, cardsToUpdate, cardsNotInAnki] =
this.filterByUpdate(ankiCards, cards);
const cardIds: number[] = this.getCardsIds(ankiCards, cards);
const cardsToDelete: number[] = this.parser.getCardsToDelete(this.file);
console.info("Flashcards: Cards to create");
console.info(cardsToCreate);
console.info("Flashcards: Cards to update");
console.info(cardsToUpdate);
console.info("Flashcards: Cards to delete");
console.info(cardsToDelete);
if (cardsNotInAnki) {
console.info("Flashcards: Cards not in Anki (maybe deleted)");
for (const card of cardsNotInAnki) {
this.notifications.push(
`Error: Card with ID ${card.id} is not in Anki!`
);
}
}
console.info(cardsNotInAnki);
this.insertMedias(cards, sourcePath);
await this.deleteCardsOnAnki(cardsToDelete, ankiBlocks);
await this.updateCardsOnAnki(cardsToUpdate);
await this.insertCardsOnAnki(cardsToCreate, frontmatter, deckName);
// Update decks if needed
const deckNeedToBeChanged = await this.deckNeedToBeChanged(
cardIds,
deckName
);
if (deckNeedToBeChanged) {
try {
this.anki.changeDeck(cardIds, deckName);
this.notifications.push("Cards moved in new deck");
} catch {
return ["Error: Could not update deck the file."];
}
}
// Update file
if (this.updateFile) {
try {
this.app.vault.modify(activeFile, this.file);
} catch (err) {
Error("Could not update the file.");
return ["Error: Could not update the file."];
}
}
if (!this.notifications.length) {
this.notifications.push("Nothing to do. Everything is up to date");
}
return this.notifications;
} catch (err) {
console.error(err);
Error("Something went wrong");
}
}
private async insertMedias(cards: Card[], sourcePath: string) {
try {
// Currently the media are created for every run, this is not a problem since Anki APIs overwrite the file
// A more efficient way would be to keep track of the medias saved
await this.generateMediaLinks(cards, sourcePath);
await this.anki.storeMediaFiles(cards);
} catch (err) {
console.error(err);
Error("Error: Could not upload medias");
}
}
private async generateMediaLinks(cards: Card[], sourcePath: string) {
if (this.app.vault.adapter instanceof FileSystemAdapter) {
// @ts-ignore: Unreachable code error
for (const card of cards) {
for (const media of card.mediaNames) {
const image = this.app.metadataCache.getFirstLinkpathDest(
decodeURIComponent(media),
sourcePath
);
try {
const binaryMedia = await this.app.vault.readBinary(image);
card.mediaBase64Encoded.push(arrayBufferToBase64(binaryMedia));
} catch (err) {
Error("Error: Could not read media");
}
}
}
}
}
private async insertCardsOnAnki(
cardsToCreate: Card[],
frontmatter: FrontMatterCache,
deckName: string
): Promise<number> {
if (cardsToCreate.length) {
let insertedCards = 0;
try {
const ids = await this.anki.addCards(cardsToCreate);
// Add IDs from response to Flashcard[]
ids.map((id: number, index: number) => {
cardsToCreate[index].id = id;
});
let total = 0;
cardsToCreate.forEach((card) => {
if (card.id === null) {
new Notice(
`Error, could not add: '${card.initialContent}'`,
noticeTimeout
);
} else {
card.reversed ? (insertedCards += 2) : insertedCards++;
}
card.reversed ? (total += 2) : total++;
});
this.updateFrontmatter(frontmatter, deckName);
this.writeAnkiBlocks(cardsToCreate);
this.notifications.push(
`Inserted successfully ${insertedCards}/${total} cards.`
);
return insertedCards;
} catch (err) {
console.error(err);
Error("Error: Could not write cards on Anki");
}
}
}
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;
}
}
private writeAnkiBlocks(cardsToCreate: Card[]) {
for (const card of cardsToCreate) {
// Card.id cannot be null, because if written already previously it has an ID,
// if it has been inserted it has an ID too
if (card.id !== null && !card.inserted) {
let id = card.getIdFormat();
if (card instanceof Inlinecard) {
if (this.settings.inlineID) {
id = " " + id;
} else {
id = "\n" + id;
}
}
card.endOffset += this.totalOffset;
const offset = card.endOffset;
this.updateFile = true;
this.file =
this.file.substring(0, offset) +
id +
this.file.substring(offset, this.file.length + 1);
this.totalOffset += id.length;
}
}
}
private async updateCardsOnAnki(cards: Card[]): Promise<number> {
if (cards.length) {
try {
this.anki.updateCards(cards);
this.notifications.push(
`Updated successfully ${cards.length}/${cards.length} cards.`
);
} catch (err) {
console.error(err);
Error("Error: Could not update cards on Anki");
}
return cards.length;
}
}
public async deleteCardsOnAnki(
cards: number[],
ankiBlocks: RegExpMatchArray[]
): Promise<number> {
if (cards.length) {
let deletedCards = 0;
for (const block of ankiBlocks) {
const id = Number(block[1]);
// Deletion of cards that need to be deleted (i.e. blocks ID that don't have content)
if (cards.includes(id)) {
try {
this.anki.deleteCards(cards);
deletedCards++;
this.updateFile = true;
this.file =
this.file.substring(0, block["index"]) +
this.file.substring(
block["index"] + block[0].length,
this.file.length
);
this.totalOffset -= block[0].length;
this.notifications.push(
`Deleted successfully ${deletedCards}/${cards.length} cards.`
);
} catch (err) {
console.error(err);
Error("Error, could not delete the card from Anki");
}
}
}
return deletedCards;
}
}
private getAnkiIDs(blocks: RegExpMatchArray[]): number[] {
const IDs: number[] = [];
for (const b of blocks) {
IDs.push(Number(b[1]));
}
return IDs;
}
public filterByUpdate(ankiCards: any, generatedCards: Card[]) {
let cardsToCreate: Card[] = [];
const cardsToUpdate: Card[] = [];
const cardsNotInAnki: Card[] = [];
if (ankiCards) {
for (const flashcard of generatedCards) {
// Inserted means that anki blocks are available, that means that the card should
// (the user can always delete it) be in Anki
let ankiCard = undefined;
if (flashcard.inserted) {
ankiCard = ankiCards.filter(
(card: any) => Number(card.noteId) === flashcard.id
)[0];
if (!ankiCard) {
cardsNotInAnki.push(flashcard);
} else if (!flashcard.match(ankiCard)) {
flashcard.oldTags = ankiCard.tags;
cardsToUpdate.push(flashcard);
}
} else {
cardsToCreate.push(flashcard);
}
}
} else {
cardsToCreate = [...generatedCards];
}
return [cardsToCreate, cardsToUpdate, cardsNotInAnki];
}
public async deckNeedToBeChanged(cardsIds: number[], deckName: string) {
const cardsInfo = await this.anki.cardsInfo(cardsIds);
console.log("Flashcards: Cards info");
console.log(cardsInfo);
if (cardsInfo.length !== 0) {
return cardsInfo[0].deckName !== deckName;
}
return false;
}
public getCardsIds(ankiCards: any, generatedCards: Card[]): number[] {
let ids: number[] = [];
if (ankiCards) {
for (const flashcard of generatedCards) {
let ankiCard = undefined;
if (flashcard.inserted) {
ankiCard = ankiCards.filter(
(card: any) => Number(card.noteId) === flashcard.id
)[0];
if (ankiCard) {
ids = ids.concat(ankiCard.cards);
}
}
}
}
return ids;
}
public parseGlobalTags(file: string): string[] {
let globalTags: string[] = [];
const tags = file.match(/(?:cards-)?tags: ?(.*)/im);
globalTags = tags ? tags[1].match(this.regex.globalTagsSplitter) : [];
if (globalTags) {
for (let i = 0; i < globalTags.length; i++) {
globalTags[i] = globalTags[i].replace("#", "");
globalTags[i] = globalTags[i].replace(/\//g, "::");
globalTags[i] = globalTags[i].replace(/\[\[(.*)\]\]/, "$1");
globalTags[i] = globalTags[i].trim();
globalTags[i] = globalTags[i].replace(/ /g, "-");
}
return globalTags;
}
return [];
}
}