|
4 | 4 | AnkiLinkSettings, |
5 | 5 | AnkiLinkSettingsTab, |
6 | 6 | } 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"; |
8 | 8 | import { FLASHCARD_PATTERN, splitCalloutBody } from "./regexUtil"; |
9 | 9 |
|
10 | 10 | export default class AnkiLink extends Plugin { |
@@ -56,48 +56,43 @@ export default class AnkiLink extends Plugin { |
56 | 56 | async parse(): Promise<number> { |
57 | 57 | const { vault } = this.app; |
58 | 58 |
|
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 | + |
66 | 61 | const deckNamesRes = await sendDeckNamesRequest(); |
67 | 62 | if (deckNamesRes.error) throw new Error(`AnkiConnect: ${deckNamesRes.error}`) |
68 | 63 | const decks = deckNamesRes.result; |
69 | 64 | if (!decks.contains(TARGET_DECK)) { |
70 | 65 | const createDeckRes = await sendCreateDeckRequest(TARGET_DECK); |
71 | 66 | if (createDeckRes.error) throw new Error(`AnkiConnect: ${createDeckRes.error}`) |
72 | 67 | } |
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; |
77 | 73 | } |
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); |
87 | 83 | } |
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 | + } |
94 | 86 |
|
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; |
96 | 91 | } |
97 | 92 |
|
98 | | - private async sendNote(noteFields: ConnNoteFields) { |
99 | | - const note = buildNote(noteFields.Front, noteFields.Back); |
| 93 | + private async sendNote(note: ConnNote) { |
100 | 94 | const res = await sendAddNoteRequest(note); |
101 | 95 | if (res.error) throw new Error(`AnkiConnect ${res.error}`); |
| 96 | + return res.result; |
102 | 97 | } |
103 | 98 | } |
0 commit comments