DisplayAnki note requirements (#1799)

* Update how errors are collected from _formatField

* Expose requirements
This commit is contained in:
toasted-nutbread 2021-07-05 15:08:11 -04:00 committed by GitHub
parent 0491de12d4
commit da13a2ebff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 12 deletions

View File

@ -52,20 +52,27 @@ class AnkiNoteBuilder {
duplicateScopeCheckChildren = true; duplicateScopeCheckChildren = true;
} }
const errors = [];
const commonData = this._createData(dictionaryEntry, mode, context, resultOutputMode, glossaryLayoutMode, compactTags, injectedMedia); const commonData = this._createData(dictionaryEntry, mode, context, resultOutputMode, glossaryLayoutMode, compactTags, injectedMedia);
const formattedFieldValuePromises = []; const formattedFieldValuePromises = [];
for (const [, fieldValue] of fields) { for (const [, fieldValue] of fields) {
const formattedFieldValuePromise = this._formatField(fieldValue, commonData, template, errors); const formattedFieldValuePromise = this._formatField(fieldValue, commonData, template);
formattedFieldValuePromises.push(formattedFieldValuePromise); formattedFieldValuePromises.push(formattedFieldValuePromise);
} }
const formattedFieldValues = await Promise.all(formattedFieldValuePromises); const formattedFieldValues = await Promise.all(formattedFieldValuePromises);
const errors = [];
const uniqueRequirements = new Map();
const noteFields = {}; const noteFields = {};
for (let i = 0, ii = fields.length; i < ii; ++i) { for (let i = 0, ii = fields.length; i < ii; ++i) {
const fieldName = fields[i][0]; const fieldName = fields[i][0];
const formattedFieldValue = formattedFieldValues[i]; const {value, errors: fieldErrors, requirements} = formattedFieldValues[i];
noteFields[fieldName] = formattedFieldValue; noteFields[fieldName] = value;
errors.push(...fieldErrors);
for (const requirement of requirements) {
const key = JSON.stringify(requirement);
if (uniqueRequirements.has(key)) { continue; }
uniqueRequirements.set(key, requirement);
}
} }
const note = { const note = {
@ -82,7 +89,7 @@ class AnkiNoteBuilder {
} }
} }
}; };
return {note, errors}; return {note, errors, requirements: [...uniqueRequirements.values()]};
} }
async getRenderingData({ async getRenderingData({
@ -113,10 +120,13 @@ class AnkiNoteBuilder {
}; };
} }
async _formatField(field, commonData, template, errors) { async _formatField(field, commonData, template) {
return await this._stringReplaceAsync(field, this._markerPattern, async (g0, marker) => { const errors = [];
const requirements = [];
const value = await this._stringReplaceAsync(field, this._markerPattern, async (g0, marker) => {
try { try {
const {result} = await this._renderTemplateBatched(template, commonData, marker); const {result, requirements: fieldRequirements} = await this._renderTemplateBatched(template, commonData, marker);
requirements.push(...fieldRequirements);
return result; return result;
} catch (e) { } catch (e) {
const error = new Error(`Template render error for {${marker}}`); const error = new Error(`Template render error for {${marker}}`);
@ -125,6 +135,7 @@ class AnkiNoteBuilder {
return `{${marker}-render-error}`; return `{${marker}-render-error}`;
} }
}); });
return {value, errors, requirements};
} }
async _stringReplaceAsync(str, regex, replacer) { async _stringReplaceAsync(str, regex, replacer) {

View File

@ -498,13 +498,13 @@ class DisplayAnki {
const results = []; const results = [];
for (let i = 0, ii = noteInfoList.length; i < ii; ++i) { for (let i = 0, ii = noteInfoList.length; i < ii; ++i) {
const {note, errors} = noteInfoList[i]; const {note, errors, requirements} = noteInfoList[i];
const {canAdd, valid, noteIds, noteInfos} = infos[i]; const {canAdd, valid, noteIds, noteInfos} = infos[i];
const {mode, index} = noteTargets[i]; const {mode, index} = noteTargets[i];
while (index >= results.length) { while (index >= results.length) {
results.push([]); results.push([]);
} }
results[index].push({mode, note, errors, canAdd, valid, noteIds, noteInfos, ankiError}); results[index].push({mode, note, errors, requirements, canAdd, valid, noteIds, noteInfos, ankiError});
} }
return results; return results;
} }
@ -536,7 +536,7 @@ class DisplayAnki {
} }
} }
const {note, errors: createNoteErrors} = await this._ankiNoteBuilder.createNote({ const {note, errors: createNoteErrors, requirements} = await this._ankiNoteBuilder.createNote({
dictionaryEntry, dictionaryEntry,
mode, mode,
context, context,
@ -554,7 +554,7 @@ class DisplayAnki {
errors errors
}); });
errors.push(...createNoteErrors); errors.push(...createNoteErrors);
return {note, errors}; return {note, errors, requirements};
} }
async _injectAnkiNoteMedia(dictionaryEntry, fields) { async _injectAnkiNoteMedia(dictionaryEntry, fields) {