diff options
Diffstat (limited to 'ext/bg/js/anki-note-builder.js')
-rw-r--r-- | ext/bg/js/anki-note-builder.js | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index ae608bed..632d9f8a 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -17,12 +17,13 @@ /* global * DictionaryDataUtil + * TemplateRendererProxy */ class AnkiNoteBuilder { - constructor({renderTemplate}) { - this._renderTemplate = renderTemplate; + constructor(enabled) { this._markerPattern = /\{([\w-]+)\}/g; + this._templateRenderer = enabled ? new TemplateRendererProxy() : null; } async createNote({ @@ -49,22 +50,6 @@ class AnkiNoteBuilder { duplicateScopeCheckChildren = true; } - const noteFields = {}; - const note = { - fields: noteFields, - tags, - deckName, - modelName, - options: { - allowDuplicate: !checkForDuplicates, - duplicateScope, - duplicateScopeOptions: { - deckName: duplicateScopeDeckName, - checkChildren: duplicateScopeCheckChildren - } - } - }; - const data = this._createNoteData(definition, mode, context, resultOutputMode, glossaryLayoutMode, compactTags); const formattedFieldValuePromises = []; for (const [, fieldValue] of fields) { @@ -73,13 +58,27 @@ class AnkiNoteBuilder { } const formattedFieldValues = await Promise.all(formattedFieldValuePromises); + const noteFields = {}; for (let i = 0, ii = fields.length; i < ii; ++i) { const fieldName = fields[i][0]; const formattedFieldValue = formattedFieldValues[i]; noteFields[fieldName] = formattedFieldValue; } - return note; + return { + fields: noteFields, + tags, + deckName, + modelName, + options: { + allowDuplicate: !checkForDuplicates, + duplicateScope, + duplicateScopeOptions: { + deckName: duplicateScopeDeckName, + checkChildren: duplicateScopeCheckChildren + } + } + }; } containsMarker(fields, marker) { @@ -146,7 +145,7 @@ class AnkiNoteBuilder { }); } - _stringReplaceAsync(str, regex, replacer) { + async _stringReplaceAsync(str, regex, replacer) { let match; let index = 0; const parts = []; @@ -155,9 +154,13 @@ class AnkiNoteBuilder { index = regex.lastIndex; } if (parts.length === 0) { - return Promise.resolve(str); + return str; } parts.push(str.substring(index)); - return Promise.all(parts).then((v) => v.join('')); + return (await Promise.all(parts)).join(''); + } + + async _renderTemplate(template, data, marker) { + return await this._templateRenderer.render(template, {data, marker}, 'ankiNote'); } } |