Skip to content

Commit ecba5c2

Browse files
Attempt writing indices back to the markdown file
1 parent f9de732 commit ecba5c2

File tree

3 files changed

+27
-31
lines changed

3 files changed

+27
-31
lines changed

src/ankiConnectUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface ConnRequest {
2525
version: number
2626
}
2727

28-
interface ConnNote {
28+
export interface ConnNote {
2929
deckName: string,
3030
modelName: string,
3131
fields: ConnNoteFields

src/main.ts

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
AnkiLinkSettings,
55
AnkiLinkSettingsTab,
66
} from "./settings";
7-
import { TARGET_DECK, sendAddNoteRequest, buildNote, sendCreateDeckRequest, sendDeckNamesRequest, ConnNoteFields } from "./ankiConnectUtil";
7+
import { TARGET_DECK, sendAddNoteRequest, buildNote, sendCreateDeckRequest, sendDeckNamesRequest, ConnNoteFields, ConnNote } from "./ankiConnectUtil";
88
import { FLASHCARD_PATTERN, splitCalloutBody } from "./regexUtil";
99

1010
export default class AnkiLink extends Plugin {
@@ -56,48 +56,43 @@ export default class AnkiLink extends Plugin {
5656
async parse(): Promise<number> {
5757
const { vault } = this.app;
5858

59-
const fileContents = await Promise.all(
60-
vault.getMarkdownFiles().map((file) => vault.read(file)),
61-
);
62-
fileContents.forEach((c) => {
63-
const values = FLASHCARD_PATTERN.exec(c);
64-
values?.shift();
65-
});
59+
const markdownFiles = vault.getMarkdownFiles();
60+
6661
const deckNamesRes = await sendDeckNamesRequest();
6762
if (deckNamesRes.error) throw new Error(`AnkiConnect: ${deckNamesRes.error}`)
6863
const decks = deckNamesRes.result;
6964
if (!decks.contains(TARGET_DECK)) {
7065
const createDeckRes = await sendCreateDeckRequest(TARGET_DECK);
7166
if (createDeckRes.error) throw new Error(`AnkiConnect: ${createDeckRes.error}`)
7267
}
73-
const cards = fileContents.reduce<ConnNoteFields[]>((acc, s) => {
74-
const matches = FLASHCARD_PATTERN.exec(s);
75-
if (!matches || matches.length < 3) {
76-
return acc;
68+
for (const file of markdownFiles) {
69+
const s = await vault.read(file);
70+
const match = FLASHCARD_PATTERN.exec(s);
71+
if (!match || match.length < 3) {
72+
continue;
7773
}
78-
const title = matches[1]!;
79-
const body = matches[2]!;
80-
const back = splitCalloutBody(body);
81-
acc.push({ Front: title, Back: back });
82-
return acc;
83-
}, []);
84-
console.log(cards)
85-
for (const card of cards) {
86-
await this.sendNote(card);
74+
const title = match[1]!;
75+
const rawBody = match[2]!;
76+
const splitBody = splitCalloutBody(rawBody);
77+
const card = buildNote(title, splitBody)
78+
const index = await this.sendNote(card);
79+
console.log(title.length);
80+
const endIndex = match.index + title.length + 13;
81+
const indexedFileContent = this.spliceString(s, endIndex, index.toString());
82+
await vault.modify(file, indexedFileContent);
8783
}
88-
const note = buildNote(
89-
"Cool new front note content",
90-
"Boring back content"
91-
)
92-
const addNoteRes = await sendAddNoteRequest(note);
93-
if (addNoteRes.error) throw new Error(`AnkiConnect: ${addNoteRes.error}`);
84+
return 0;
85+
}
9486

95-
return fileContents.length;
87+
private spliceString(base: string, index: number, item: string): string {
88+
const s1 = base.slice(0, index);
89+
const s2 = base.slice(index, - 1)
90+
return s1 + item + s2;
9691
}
9792

98-
private async sendNote(noteFields: ConnNoteFields) {
99-
const note = buildNote(noteFields.Front, noteFields.Back);
93+
private async sendNote(note: ConnNote) {
10094
const res = await sendAddNoteRequest(note);
10195
if (res.error) throw new Error(`AnkiConnect ${res.error}`);
96+
return res.result;
10297
}
10398
}

src/regexUtil.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export const FLASHCARD_PATTERN: RegExp = /\[!flashcard\]\s*([^\n]*)\n((?:>(?:[^\
33

44
export function splitCalloutBody(body: string) {
55
const lines = body.split(">");
6+
lines.shift(); // All bodies will start with a > and a space
67
return lines.join("<br>");
78
}

0 commit comments

Comments
 (0)