From 1d7784cafaf3f9357e8f040e3a052b806b286db0 Mon Sep 17 00:00:00 2001 From: Pacharapol Withayasakpunt Date: Thu, 28 Apr 2022 11:34:36 +0700 Subject: [PATCH] fix sound tags --- scripts/query-anki.ts | 94 +++++++++++++++++++++++++++---- template/jp.takoboto/EJ/Back.html | 2 +- template/jp.takoboto/JE/Back.html | 2 +- 3 files changed, 86 insertions(+), 12 deletions(-) diff --git a/scripts/query-anki.ts b/scripts/query-anki.ts index 0858c0c..bd3b95e 100644 --- a/scripts/query-anki.ts +++ b/scripts/query-anki.ts @@ -1,12 +1,86 @@ import { AnkiConnect } from '@/ankiconnect'; -const anki = new AnkiConnect(); -anki - .api('findNotes', { - query: - 'deck:Takoboto -Takoboto: tag:common -tag:reading-challenge -tag:favorites -tag:death-note -tag:ハピネス -tag:https://www.bunka.go.jp/seisaku/bunkashingikai/kokugo/hokoku/pdf/ijidokun_140221.pdf', - }) - .then((notes) => anki.api('notesInfo', { notes })) - .then((notes) => { - console.log(new Set(notes.flatMap((n) => n.tags))); - }); +export async function listTags( + query = 'deck:Takoboto -Takoboto: tag:common -tag:reading-challenge -tag:favorites -tag:death-note -tag:ハピネス -tag:https://www.bunka.go.jp/seisaku/bunkashingikai/kokugo/hokoku/pdf/ijidokun_140221.pdf', +) { + const anki = new AnkiConnect(); + return anki + .api('findNotes', { + query, + }) + .then((notes) => anki.api('notesInfo', { notes })) + .then((notes) => { + return new Set(notes.flatMap((n) => n.tags)); + }); +} + +const soundTag = { + pre: '[sound:', + post: ']', + is(src: string) { + return src.startsWith(this.pre) && src.endsWith(this.post); + }, + add(src: string) { + if (soundTag.is(src)) return src; + return this.pre + src + this.post; + }, + remove(src: string) { + if (!soundTag.is(src)) return src; + if (this.isEmpty(src)) return src; + return src.substring(this.pre.length, src.length - this.post.length); + }, + isEmpty(src: string) { + return src === this.pre + this.post; + }, +}; + +export async function addSoundTag(query: string, fieldNames: string[]) { + query += ' (' + fieldNames.map((f) => `-${f}:`).join(' OR ') + ')'; + + const anki = new AnkiConnect(); + anki + .api('findNotes', { + query, + }) + .then((notes) => anki.api('notesInfo', { notes })) + .then((notes) => { + const notesToUpdate: { + id: number; + fields: Record; + }[] = []; + + for (const n of notes) { + let toUpdate: typeof notesToUpdate[0] | undefined; + + fieldNames.map((fieldName: string) => { + const field = n.fields[fieldName]; + if (field) { + const { value } = field; + if (!soundTag.is(value)) { + toUpdate = toUpdate || { id: n.noteId, fields: {} }; + toUpdate.fields[fieldName] = soundTag.add(value); + } + } + }); + + if (toUpdate) { + notesToUpdate.push(toUpdate); + } + } + + if (!notesToUpdate.length) return; + + return anki.multi<'updateNoteFields'[]>({ + actions: notesToUpdate.map((note) => ({ + action: 'updateNoteFields', + params: { + note, + }, + })), + }); + }); +} + +if (require.main === module) { + addSoundTag('jp.takoboto', ['SentenceAudio', 'JapaneseAudio']); +} diff --git a/template/jp.takoboto/EJ/Back.html b/template/jp.takoboto/EJ/Back.html index 4c06113..3e47a55 100644 --- a/template/jp.takoboto/EJ/Back.html +++ b/template/jp.takoboto/EJ/Back.html @@ -140,7 +140,7 @@ post: ']' } if (src.startsWith(soundTag.pre) && src.endsWith(soundTag.post)) { - src = src.substring(soundTag.pre, src.length - soundTag.post) + src = src.substring(soundTag.pre.length, src.length - soundTag.post.length) } try { diff --git a/template/jp.takoboto/JE/Back.html b/template/jp.takoboto/JE/Back.html index 2895b1b..65dd357 100644 --- a/template/jp.takoboto/JE/Back.html +++ b/template/jp.takoboto/JE/Back.html @@ -128,7 +128,7 @@ post: ']' } if (src.startsWith(soundTag.pre) && src.endsWith(soundTag.post)) { - src = src.substring(soundTag.pre, src.length - soundTag.post) + src = src.substring(soundTag.pre.length, src.length - soundTag.post.length) } try {