aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-09-04 17:57:05 -0400
committerGitHub <noreply@github.com>2020-09-04 17:57:05 -0400
commit21fc0a80f2382f383ae5df221b17313e72b809db (patch)
tree6ac00942df30bc12a1cf132fa203934cba5e7967 /ext
parent3efd4a1a94ddac6b0c9e0d3cf6fa7eb5621bf8a2 (diff)
Defer assignment of marker (#762)
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/anki-note-builder.js3
-rw-r--r--ext/bg/js/backend.js8
-rw-r--r--ext/bg/js/template-renderer.js10
-rw-r--r--ext/mixed/js/api.js4
4 files changed, 16 insertions, 9 deletions
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js
index 2405543e..041e6dcd 100644
--- a/ext/bg/js/anki-note-builder.js
+++ b/ext/bg/js/anki-note-builder.js
@@ -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}`;
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index e9f4f924..0c901c68 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -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) {
diff --git a/ext/bg/js/template-renderer.js b/ext/bg/js/template-renderer.js
index 59af74c8..1f68e3e0 100644
--- a/ext/bg/js/template-renderer.js
+++ b/ext/bg/js/template-renderer.js
@@ -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;
+ }
}
}
diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js
index e6e27cb6..e58000e8 100644
--- a/ext/mixed/js/api.js
+++ b/ext/mixed/js/api.js
@@ -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) {