diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-04-02 12:42:06 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-02 12:42:06 -0400 |
commit | 8179846e381eb0a87bf3bc266abec8f4400565bc (patch) | |
tree | ddb57d7870b461c9758f6de8a25dd76bbfaf81f7 /ext/js/data/anki-note-builder.js | |
parent | 36b7e34cce776cb09a76c28ce8e78e864dabcdda (diff) |
Refactor template rendering (#1583)
* Update _errorToJson to _serializeError
* Remove async
* Refactor render
* Simplify _getModifiedData
* Rename data => commonData
* Rename templates => template for consistency
* Improve errors check
* Update tests
Diffstat (limited to 'ext/js/data/anki-note-builder.js')
-rw-r--r-- | ext/js/data/anki-note-builder.js | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/ext/js/data/anki-note-builder.js b/ext/js/data/anki-note-builder.js index dfd92493..38f1eb16 100644 --- a/ext/js/data/anki-note-builder.js +++ b/ext/js/data/anki-note-builder.js @@ -30,7 +30,7 @@ class AnkiNoteBuilder { definition, mode, context, - templates, + template, deckName, modelName, fields, @@ -51,10 +51,10 @@ class AnkiNoteBuilder { duplicateScopeCheckChildren = true; } - const data = this._createData(definition, mode, context, resultOutputMode, glossaryLayoutMode, compactTags, injectedMedia); + const commonData = this._createData(definition, mode, context, resultOutputMode, glossaryLayoutMode, compactTags, injectedMedia); const formattedFieldValuePromises = []; for (const [, fieldValue] of fields) { - const formattedFieldValuePromise = this._formatField(fieldValue, data, templates, errors); + const formattedFieldValuePromise = this._formatField(fieldValue, commonData, template, errors); formattedFieldValuePromises.push(formattedFieldValuePromise); } @@ -110,12 +110,12 @@ class AnkiNoteBuilder { }; } - async _formatField(field, data, templates, errors=null) { + async _formatField(field, commonData, template, errors=null) { return await this._stringReplaceAsync(field, this._markerPattern, async (g0, marker) => { try { - return await this._renderTemplate(templates, data, marker); + return await this._renderTemplate(template, marker, commonData); } catch (e) { - if (errors) { + if (Array.isArray(errors)) { const error = new Error(`Template render error for {${marker}}`); error.data = {error: e}; errors.push(error); @@ -140,7 +140,7 @@ class AnkiNoteBuilder { return (await Promise.all(parts)).join(''); } - async _renderTemplate(template, data, marker) { - return await this._templateRenderer.render(template, {data, marker}, 'ankiNote'); + async _renderTemplate(template, marker, commonData) { + return await this._templateRenderer.render(template, {marker, commonData}, 'ankiNote'); } } |