Template rendering updates (#1762)

* Remove unused function

* Update template rendering return value
This commit is contained in:
toasted-nutbread 2021-06-26 17:05:55 -04:00 committed by GitHub
parent f497cb2a07
commit f9167c8fdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 6 deletions

View File

@ -115,7 +115,8 @@ class AnkiNoteBuilder {
async _formatField(field, commonData, template, errors=null) { async _formatField(field, commonData, template, errors=null) {
return await this._stringReplaceAsync(field, this._markerPattern, async (g0, marker) => { return await this._stringReplaceAsync(field, this._markerPattern, async (g0, marker) => {
try { try {
return await this._renderTemplateBatched(template, commonData, marker); const {result} = await this._renderTemplateBatched(template, commonData, marker);
return result;
} catch (e) { } catch (e) {
if (Array.isArray(errors)) { if (Array.isArray(errors)) {
const error = new Error(`Template render error for {${marker}}`); const error = new Error(`Template render error for {${marker}}`);
@ -142,10 +143,6 @@ class AnkiNoteBuilder {
return (await Promise.all(parts)).join(''); return (await Promise.all(parts)).join('');
} }
async _renderTemplate(template, marker, commonData) {
return await this._templateRenderer.render(template, {marker, commonData}, 'ankiNote');
}
_getBatchedTemplateGroup(template) { _getBatchedTemplateGroup(template) {
for (const item of this._batchedRequests) { for (const item of this._batchedRequests) {
if (item.template === template) { if (item.template === template) {

View File

@ -28,6 +28,7 @@ class TemplateRenderer {
this._helpersRegistered = false; this._helpersRegistered = false;
this._stateStack = null; this._stateStack = null;
this._dataTypes = new Map(); this._dataTypes = new Map();
this._requirements = null;
} }
registerDataType(name, {modifier=null, composeData=null}) { registerDataType(name, {modifier=null, composeData=null}) {
@ -85,10 +86,14 @@ class TemplateRenderer {
_renderTemplate(instance, data) { _renderTemplate(instance, data) {
try { try {
const requirements = [];
this._stateStack = [new Map()]; this._stateStack = [new Map()];
return instance(data).trim(); this._requirements = requirements;
const result = instance(data).trim();
return {result, requirements};
} finally { } finally {
this._stateStack = null; this._stateStack = null;
this._requirements = null;
} }
} }