Defer assignment of marker (#762)
This commit is contained in:
parent
3efd4a1a94
commit
21fc0a80f2
@ -81,9 +81,8 @@ class AnkiNoteBuilder {
|
|||||||
async formatField(field, data, templates, errors=null) {
|
async formatField(field, data, templates, errors=null) {
|
||||||
const pattern = /\{([\w-]+)\}/g;
|
const pattern = /\{([\w-]+)\}/g;
|
||||||
return await AnkiNoteBuilder.stringReplaceAsync(field, pattern, async (g0, marker) => {
|
return await AnkiNoteBuilder.stringReplaceAsync(field, pattern, async (g0, marker) => {
|
||||||
data.marker = marker;
|
|
||||||
try {
|
try {
|
||||||
return await this._renderTemplate(templates, data);
|
return await this._renderTemplate(templates, data, marker);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (errors) { errors.push(e); }
|
if (errors) { errors.push(e); }
|
||||||
return `{${marker}-render-error}`;
|
return `{${marker}-render-error}`;
|
||||||
|
@ -518,8 +518,8 @@ class Backend {
|
|||||||
return await this._anki.guiBrowse(`nid:${noteId}`);
|
return await this._anki.guiBrowse(`nid:${noteId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiTemplateRender({template, data}) {
|
async _onApiTemplateRender({template, data, marker}) {
|
||||||
return this._renderTemplate(template, data);
|
return this._renderTemplate(template, data, marker);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onApiCommandExec({command, params}) {
|
_onApiCommandExec({command, params}) {
|
||||||
@ -1339,8 +1339,8 @@ class Backend {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async _renderTemplate(template, data) {
|
async _renderTemplate(template, data, marker) {
|
||||||
return await this._templateRenderer.render(template, data);
|
return await this._templateRenderer.render(template, data, marker);
|
||||||
}
|
}
|
||||||
|
|
||||||
_getTemplates(options) {
|
_getTemplates(options) {
|
||||||
|
@ -28,7 +28,7 @@ class TemplateRenderer {
|
|||||||
this._stateStack = null;
|
this._stateStack = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async render(template, data) {
|
async render(template, data, marker) {
|
||||||
if (!this._helpersRegistered) {
|
if (!this._helpersRegistered) {
|
||||||
this._registerHelpers();
|
this._registerHelpers();
|
||||||
this._helpersRegistered = true;
|
this._helpersRegistered = true;
|
||||||
@ -42,11 +42,19 @@ class TemplateRenderer {
|
|||||||
cache.set(template, instance);
|
cache.set(template, instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const markerPre = data.marker;
|
||||||
|
const markerPreHas = hasOwn(data, 'marker');
|
||||||
try {
|
try {
|
||||||
this._stateStack = [new Map()];
|
this._stateStack = [new Map()];
|
||||||
|
data.marker = marker;
|
||||||
return instance(data).trim();
|
return instance(data).trim();
|
||||||
} finally {
|
} finally {
|
||||||
this._stateStack = null;
|
this._stateStack = null;
|
||||||
|
if (markerPreHas) {
|
||||||
|
data.marker = markerPre;
|
||||||
|
} else {
|
||||||
|
delete data.marker;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,8 +89,8 @@ const api = (() => {
|
|||||||
return this._invoke('noteView', {noteId});
|
return this._invoke('noteView', {noteId});
|
||||||
}
|
}
|
||||||
|
|
||||||
templateRender(template, data) {
|
templateRender(template, data, marker) {
|
||||||
return this._invoke('templateRender', {data, template});
|
return this._invoke('templateRender', {data, template, marker});
|
||||||
}
|
}
|
||||||
|
|
||||||
audioGetUri(definition, source, details) {
|
audioGetUri(definition, source, details) {
|
||||||
|
Loading…
Reference in New Issue
Block a user