Defer assignment of marker (#762)

This commit is contained in:
toasted-nutbread 2020-09-04 17:57:05 -04:00 committed by GitHub
parent 3efd4a1a94
commit 21fc0a80f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 9 deletions

View File

@ -81,9 +81,8 @@ class AnkiNoteBuilder {
async formatField(field, data, templates, errors=null) {
const pattern = /\{([\w-]+)\}/g;
return await AnkiNoteBuilder.stringReplaceAsync(field, pattern, async (g0, marker) => {
data.marker = marker;
try {
return await this._renderTemplate(templates, data);
return await this._renderTemplate(templates, data, marker);
} catch (e) {
if (errors) { errors.push(e); }
return `{${marker}-render-error}`;

View File

@ -518,8 +518,8 @@ class Backend {
return await this._anki.guiBrowse(`nid:${noteId}`);
}
async _onApiTemplateRender({template, data}) {
return this._renderTemplate(template, data);
async _onApiTemplateRender({template, data, marker}) {
return this._renderTemplate(template, data, marker);
}
_onApiCommandExec({command, params}) {
@ -1339,8 +1339,8 @@ class Backend {
return false;
}
async _renderTemplate(template, data) {
return await this._templateRenderer.render(template, data);
async _renderTemplate(template, data, marker) {
return await this._templateRenderer.render(template, data, marker);
}
_getTemplates(options) {

View File

@ -28,7 +28,7 @@ class TemplateRenderer {
this._stateStack = null;
}
async render(template, data) {
async render(template, data, marker) {
if (!this._helpersRegistered) {
this._registerHelpers();
this._helpersRegistered = true;
@ -42,11 +42,19 @@ class TemplateRenderer {
cache.set(template, instance);
}
const markerPre = data.marker;
const markerPreHas = hasOwn(data, 'marker');
try {
this._stateStack = [new Map()];
data.marker = marker;
return instance(data).trim();
} finally {
this._stateStack = null;
if (markerPreHas) {
data.marker = markerPre;
} else {
delete data.marker;
}
}
}

View File

@ -89,8 +89,8 @@ const api = (() => {
return this._invoke('noteView', {noteId});
}
templateRender(template, data) {
return this._invoke('templateRender', {data, template});
templateRender(template, data, marker) {
return this._invoke('templateRender', {data, template, marker});
}
audioGetUri(definition, source, details) {