aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/anki-note-builder.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/anki-note-builder.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/anki-note-builder.js')
-rw-r--r--ext/bg/js/anki-note-builder.js47
1 files changed, 25 insertions, 22 deletions
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js
index ae608bed..632d9f8a 100644
--- a/ext/bg/js/anki-note-builder.js
+++ b/ext/bg/js/anki-note-builder.js
@@ -17,12 +17,13 @@
/* global
* DictionaryDataUtil
+ * TemplateRendererProxy
*/
class AnkiNoteBuilder {
- constructor({renderTemplate}) {
- this._renderTemplate = renderTemplate;
+ constructor(enabled) {
this._markerPattern = /\{([\w-]+)\}/g;
+ this._templateRenderer = enabled ? new TemplateRendererProxy() : null;
}
async createNote({
@@ -49,22 +50,6 @@ class AnkiNoteBuilder {
duplicateScopeCheckChildren = true;
}
- const noteFields = {};
- const note = {
- fields: noteFields,
- tags,
- deckName,
- modelName,
- options: {
- allowDuplicate: !checkForDuplicates,
- duplicateScope,
- duplicateScopeOptions: {
- deckName: duplicateScopeDeckName,
- checkChildren: duplicateScopeCheckChildren
- }
- }
- };
-
const data = this._createNoteData(definition, mode, context, resultOutputMode, glossaryLayoutMode, compactTags);
const formattedFieldValuePromises = [];
for (const [, fieldValue] of fields) {
@@ -73,13 +58,27 @@ class AnkiNoteBuilder {
}
const formattedFieldValues = await Promise.all(formattedFieldValuePromises);
+ const noteFields = {};
for (let i = 0, ii = fields.length; i < ii; ++i) {
const fieldName = fields[i][0];
const formattedFieldValue = formattedFieldValues[i];
noteFields[fieldName] = formattedFieldValue;
}
- return note;
+ return {
+ fields: noteFields,
+ tags,
+ deckName,
+ modelName,
+ options: {
+ allowDuplicate: !checkForDuplicates,
+ duplicateScope,
+ duplicateScopeOptions: {
+ deckName: duplicateScopeDeckName,
+ checkChildren: duplicateScopeCheckChildren
+ }
+ }
+ };
}
containsMarker(fields, marker) {
@@ -146,7 +145,7 @@ class AnkiNoteBuilder {
});
}
- _stringReplaceAsync(str, regex, replacer) {
+ async _stringReplaceAsync(str, regex, replacer) {
let match;
let index = 0;
const parts = [];
@@ -155,9 +154,13 @@ class AnkiNoteBuilder {
index = regex.lastIndex;
}
if (parts.length === 0) {
- return Promise.resolve(str);
+ return str;
}
parts.push(str.substring(index));
- return Promise.all(parts).then((v) => v.join(''));
+ return (await Promise.all(parts)).join('');
+ }
+
+ async _renderTemplate(template, data, marker) {
+ return await this._templateRenderer.render(template, {data, marker}, 'ankiNote');
}
}