forked from mhujer/ankiai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anki.ts
58 lines (48 loc) · 1.69 KB
/
anki.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
import {
fetchNotesInfo,
findNotes,
NoteId,
NotesInfoItemField,
NotesInfoItemFields,
updateNoteFields,
UpdateNoteFieldsFields,
UpdateNoteFieldsNote,
} from './anki/api';
import { filterNoteContent, isSentence, splitMultiTerms } from './anki/note-text-filter';
export interface NoteFields extends NotesInfoItemFields {
Simplified: NotesInfoItemField;
Audio: NotesInfoItemField;
Examples: NotesInfoItemField;
Meaning: NotesInfoItemField;
Image: NotesInfoItemField;
}
export interface NoteForProcessing {
noteId: NoteId;
text: string;
}
export async function fetchNotesFromAnki(deckName: string): Promise<NoteForProcessing[]> {
const notesIds = await findNotes(deckName);
const notesInfo = await fetchNotesInfo<NoteFields>(notesIds);
const notesWithoutExamples = notesInfo.filter((note) => note.fields.Examples.value.length === 0);
const notesForProcessing = notesWithoutExamples.map((note): NoteForProcessing => {
return {
noteId: note.noteId,
text: note.fields.Simplified.value,
};
});
const filteredNotes = notesForProcessing.map((note) => {
return {
noteId: note.noteId,
text: filterNoteContent(note.text),
};
});
const notesNonSentences = filteredNotes.filter((note) => !isSentence(note.text));
const notesSplittedMultipleTerms = splitMultiTerms(notesNonSentences);
return notesSplittedMultipleTerms;
}
export interface UpdateNoteFields extends UpdateNoteFieldsFields {
Examples: string;
}
export async function updateNote(noteData: UpdateNoteFieldsNote<UpdateNoteFields>): Promise<void> {
await updateNoteFields(noteData);
}