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
|
/*
* Copyright (C) 2023-2024 Yomitan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {AnkiNoteBuilder} from '../../ext/js/data/anki-note-builder.js';
import {createAnkiNoteData} from '../../ext/js/data/anki-note-data-creator.js';
import {getStandardFieldMarkers} from '../../ext/js/data/anki-template-util.js';
import {AnkiTemplateRenderer} from '../../ext/js/templates/anki-template-renderer.js';
/**
* @param {import('dictionary').DictionaryEntryType} type
* @returns {import('anki-note-builder').Field[]}
*/
function createTestFields(type) {
/** @type {import('anki-note-builder').Field[]} */
const fields = [];
for (const marker of getStandardFieldMarkers(type)) {
fields.push([marker, `{${marker}}`]);
}
return fields;
}
/**
* @param {import('dictionary').DictionaryEntry} dictionaryEntry
* @param {import('settings').ResultOutputMode} mode
* @returns {import('anki-templates').NoteData}
* @throws {Error}
*/
export function createTestAnkiNoteData(dictionaryEntry, mode) {
const marker = '{marker}';
/** @type {import('anki-templates-internal').CreateDetails} */
const data = {
dictionaryEntry,
resultOutputMode: mode,
mode: 'test',
glossaryLayoutMode: 'default',
compactTags: false,
context: {
url: 'url:',
sentence: {text: '', offset: 0},
documentTitle: 'title',
query: 'query',
fullQuery: 'fullQuery'
},
media: {}
};
return createAnkiNoteData(marker, data);
}
/**
* @param {import('dictionary').DictionaryEntry[]} dictionaryEntries
* @param {import('settings').ResultOutputMode} mode
* @param {string} template
* @param {?import('vitest').ExpectStatic} expect
* @returns {Promise<import('anki').NoteFields[]>}
*/
export async function getTemplateRenderResults(dictionaryEntries, mode, template, expect) {
const ankiTemplateRenderer = new AnkiTemplateRenderer();
await ankiTemplateRenderer.prepare();
const clozePrefix = 'cloze-prefix';
const clozeSuffix = 'cloze-suffix';
const results = [];
for (const dictionaryEntry of dictionaryEntries) {
let source = '';
switch (dictionaryEntry.type) {
case 'kanji':
source = dictionaryEntry.character;
break;
case 'term':
if (dictionaryEntry.headwords.length > 0 && dictionaryEntry.headwords[0].sources.length > 0) {
source = dictionaryEntry.headwords[0].sources[0].originalText;
}
break;
}
const api = new MinimalApi();
const ankiNoteBuilder = new AnkiNoteBuilder(api, ankiTemplateRenderer.templateRenderer);
const context = {
url: 'url:',
sentence: {
text: `${clozePrefix}${source}${clozeSuffix}`,
offset: clozePrefix.length
},
documentTitle: 'title',
query: 'query',
fullQuery: 'fullQuery'
};
/** @type {import('anki-note-builder').CreateNoteDetails} */
const details = {
dictionaryEntry,
mode: 'test',
context,
template,
deckName: 'deckName',
modelName: 'modelName',
fields: createTestFields(dictionaryEntry.type),
tags: ['yomitan'],
checkForDuplicates: true,
duplicateScope: 'collection',
duplicateScopeCheckAllModels: false,
resultOutputMode: mode,
glossaryLayoutMode: 'default',
compactTags: false,
requirements: [],
mediaOptions: null
};
const {note: {fields: noteFields}, errors} = await ankiNoteBuilder.createNote(details);
for (const error of errors) {
console.error(error);
}
if (expect !== null) {
expect(errors.length).toStrictEqual(0);
}
results.push(noteFields);
}
return results;
}
class MinimalApi {
/**
* @type {import('anki-note-builder.js').MinimalApi['injectAnkiNoteMedia']}
*/
async injectAnkiNoteMedia() {
throw new Error('Not supported');
}
/**
* @type {import('anki-note-builder.js').MinimalApi['parseText']}
*/
async parseText() {
throw new Error('Not supported');
}
}
|