summaryrefslogtreecommitdiff
path: root/ext/bg/js/template-renderer.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-01-10 19:28:50 -0500
committerGitHub <noreply@github.com>2021-01-10 19:28:50 -0500
commit4ed949364564f00a8b871095d030af516fc2ed6d (patch)
tree8a7ac38887874f67f2e99187f7ab3ce29496fe51 /ext/bg/js/template-renderer.js
parent25080ac82eef83fdaa921e2a8b12261130b8ac85 (diff)
Refactor anki note building (#1223)
* Move TemplateRendererProxy creation into AnkiNoteBuilder * Simplify _stringReplaceAsync * Organize note generation * Rename API * Make the template rendering function more generic
Diffstat (limited to 'ext/bg/js/template-renderer.js')
-rw-r--r--ext/bg/js/template-renderer.js30
1 files changed, 22 insertions, 8 deletions
diff --git a/ext/bg/js/template-renderer.js b/ext/bg/js/template-renderer.js
index c3e5470e..4bf9448e 100644
--- a/ext/bg/js/template-renderer.js
+++ b/ext/bg/js/template-renderer.js
@@ -26,9 +26,14 @@ class TemplateRenderer {
this._cacheMaxSize = 5;
this._helpersRegistered = false;
this._stateStack = null;
+ this._dataTypes = new Map();
}
- async render(template, data, marker) {
+ registerDataType(name, {modifier=null, modifierPost=null}) {
+ this._dataTypes.set(name, {modifier, modifierPost});
+ }
+
+ async render(template, data, type) {
if (!this._helpersRegistered) {
this._registerHelpers();
this._helpersRegistered = true;
@@ -42,18 +47,27 @@ class TemplateRenderer {
cache.set(template, instance);
}
- const markerPre = data.marker;
- const markerPreHas = Object.prototype.hasOwnProperty.call(data, 'marker');
+ let modifier = null;
+ let modifierPost = null;
+ if (typeof type === 'string') {
+ const typeInfo = this._dataTypes.get(type);
+ if (typeof typeInfo !== 'undefined') {
+ ({modifier, modifierPost} = typeInfo);
+ }
+ }
+
try {
+ if (typeof modifier === 'function') {
+ data = modifier(data);
+ }
+
this._stateStack = [new Map()];
- data.marker = marker;
return instance(data).trim();
} finally {
this._stateStack = null;
- if (markerPreHas) {
- data.marker = markerPre;
- } else {
- delete data.marker;
+
+ if (typeof modifierPost === 'function') {
+ modifierPost(data);
}
}
}