From cae6b657ab418a1cafedcb1cf72d0e793fa5178b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 2 May 2020 12:50:16 -0400 Subject: Anki audio download (#477) * Update how audio is added to Anki cards * Upgrade Anki templates * Update comments --- ext/bg/data/default-anki-field-templates.handlebars | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'ext/bg/data/default-anki-field-templates.handlebars') diff --git a/ext/bg/data/default-anki-field-templates.handlebars b/ext/bg/data/default-anki-field-templates.handlebars index 6061851f..77818a43 100644 --- a/ext/bg/data/default-anki-field-templates.handlebars +++ b/ext/bg/data/default-anki-field-templates.handlebars @@ -14,7 +14,9 @@ {{~/if~}} {{/inline}} -{{#*inline "audio"}}{{/inline}} +{{#*inline "audio"~}} + [sound:{{definition.audioFileName}}] +{{~/inline}} {{#*inline "character"}} {{~definition.character~}} -- cgit v1.2.3 From bdbe680f5cbe612df73cc0532f098f7973dfcc65 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 9 May 2020 12:29:41 -0400 Subject: Omit the sound tag if it's empty (#525) --- ext/bg/data/default-anki-field-templates.handlebars | 8 +++++--- ext/bg/js/options.js | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'ext/bg/data/default-anki-field-templates.handlebars') diff --git a/ext/bg/data/default-anki-field-templates.handlebars b/ext/bg/data/default-anki-field-templates.handlebars index 77818a43..4382f707 100644 --- a/ext/bg/data/default-anki-field-templates.handlebars +++ b/ext/bg/data/default-anki-field-templates.handlebars @@ -14,9 +14,11 @@ {{~/if~}} {{/inline}} -{{#*inline "audio"~}} - [sound:{{definition.audioFileName}}] -{{~/inline}} +{{#*inline "audio"}} + {{~#if definition.audioFileName~}} + [sound:{{definition.audioFileName}}] + {{~/if~}} +{{/inline}} {{#*inline "character"}} {{~definition.character~}} diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 47101b49..10df033c 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -115,7 +115,7 @@ const profileOptionsVersionUpdates = [ let fieldTemplates = options.anki.fieldTemplates; if (typeof fieldTemplates !== 'string') { return; } - const replacement = '{{#*inline "audio"~}}\n [sound:{{definition.audioFileName}}]\n{{~/inline}}'; + const replacement = '{{#*inline "audio"}}\n {{~#if definition.audioFileName~}}\n [sound:{{definition.audioFileName}}]\n {{~/if~}}\n{{/inline}}'; let replaced = false; fieldTemplates = fieldTemplates.replace(/\{\{#\*inline "audio"\}\}\{\{\/inline\}\}/g, () => { replaced = true; -- cgit v1.2.3 From 39df44eca40d00242d99e8121179ae8aeffce961 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 23 May 2020 03:03:34 +0300 Subject: Fix "tags" template (#539) * fix tag templates for merge and group modes * update version upgrade * adjust upgrade replacement order --- .eslintrc.json | 1 + .../data/default-anki-field-templates.handlebars | 4 +-- ext/bg/js/backend.js | 2 +- ext/bg/js/handlebars.js | 21 ++++++++++++++ ext/bg/js/options.js | 32 +++++++++++++++------- ext/mixed/js/core.js | 5 ++++ 6 files changed, 52 insertions(+), 13 deletions(-) (limited to 'ext/bg/data/default-anki-field-templates.handlebars') diff --git a/.eslintrc.json b/.eslintrc.json index 3186a491..3e384524 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -98,6 +98,7 @@ "areSetsEqual": "readonly", "getSetIntersection": "readonly", "getSetDifference": "readonly", + "escapeRegExp": "readonly", "EventDispatcher": "readonly", "EventListenerCollection": "readonly", "EXTENSION_IS_BROWSER_EDGE": "readonly" diff --git a/ext/bg/data/default-anki-field-templates.handlebars b/ext/bg/data/default-anki-field-templates.handlebars index 4382f707..42deae23 100644 --- a/ext/bg/data/default-anki-field-templates.handlebars +++ b/ext/bg/data/default-anki-field-templates.handlebars @@ -151,7 +151,7 @@ {{/inline}} {{#*inline "tags"}} - {{~#each definition.definitionTags}}{{name}}{{#unless @last}}, {{/unless}}{{/each~}} + {{~#mergeTags definition group merge}}{{this}}{{/mergeTags~}} {{/inline}} {{#*inline "url"}} @@ -166,4 +166,4 @@ {{~context.document.title~}} {{/inline}} -{{~> (lookup . "marker") ~}} \ No newline at end of file +{{~> (lookup . "marker") ~}} diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 557ceb29..20d31efc 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -150,7 +150,7 @@ class Backend { await profileConditionsDescriptorPromise; this.optionsSchema = await requestJson(chrome.runtime.getURL('/bg/data/options-schema.json'), 'GET'); - this.defaultAnkiFieldTemplates = await requestText(chrome.runtime.getURL('/bg/data/default-anki-field-templates.handlebars'), 'GET'); + this.defaultAnkiFieldTemplates = (await requestText(chrome.runtime.getURL('/bg/data/default-anki-field-templates.handlebars'), 'GET')).trim(); this.options = await optionsLoad(); this.options = JsonSchema.getValidValueOrDefault(this.optionsSchema, this.options); diff --git a/ext/bg/js/handlebars.js b/ext/bg/js/handlebars.js index 860acb14..822174e2 100644 --- a/ext/bg/js/handlebars.js +++ b/ext/bg/js/handlebars.js @@ -123,6 +123,26 @@ function handlebarsRegexMatch(...args) { return value; } +function handlebarsMergeTags(object, isGroupMode, isMergeMode) { + const tagSources = []; + if (isGroupMode || isMergeMode) { + for (const definition of object.definitions) { + tagSources.push(definition.definitionTags); + } + } else { + tagSources.push(object.definitionTags); + } + + const tags = new Set(); + for (const tagSource of tagSources) { + for (const tag of tagSource) { + tags.add(tag.name); + } + } + + return [...tags].join(', '); +} + function handlebarsRegisterHelpers() { if (Handlebars.partials !== Handlebars.templates) { Handlebars.partials = Handlebars.templates; @@ -134,6 +154,7 @@ function handlebarsRegisterHelpers() { Handlebars.registerHelper('sanitizeCssClass', handlebarsSanitizeCssClass); Handlebars.registerHelper('regexReplace', handlebarsRegexReplace); Handlebars.registerHelper('regexMatch', handlebarsRegexMatch); + Handlebars.registerHelper('mergeTags', handlebarsMergeTags); } } diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 10df033c..35fdde82 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -111,19 +111,31 @@ const profileOptionsVersionUpdates = [ }, (options) => { // Version 14 changes: - // Changed template for Anki audio. + // Changed template for Anki audio and tags. let fieldTemplates = options.anki.fieldTemplates; if (typeof fieldTemplates !== 'string') { return; } - const replacement = '{{#*inline "audio"}}\n {{~#if definition.audioFileName~}}\n [sound:{{definition.audioFileName}}]\n {{~/if~}}\n{{/inline}}'; - let replaced = false; - fieldTemplates = fieldTemplates.replace(/\{\{#\*inline "audio"\}\}\{\{\/inline\}\}/g, () => { - replaced = true; - return replacement; - }); - - if (!replaced) { - fieldTemplates += '\n\n' + replacement; + const replacements = [ + [ + '{{#*inline "audio"}}{{/inline}}', + '{{#*inline "audio"}}\n {{~#if definition.audioFileName~}}\n [sound:{{definition.audioFileName}}]\n {{~/if~}}\n{{/inline}}' + ], + [ + '{{#*inline "tags"}}\n {{~#each definition.definitionTags}}{{name}}{{#unless @last}}, {{/unless}}{{/each~}}\n{{/inline}}', + '{{#*inline "tags"}}\n {{~#mergeTags definition group merge}}{{this}}{{/mergeTags~}}\n{{/inline}}' + ] + ]; + + for (const [pattern, replacement] of replacements) { + let replaced = false; + fieldTemplates = fieldTemplates.replace(new RegExp(escapeRegExp(pattern), 'g'), () => { + replaced = true; + return replacement; + }); + + if (!replaced) { + fieldTemplates += '\n\n' + replacement; + } } options.anki.fieldTemplates = fieldTemplates; diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 835d9cea..589425f2 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -94,6 +94,11 @@ function hasOwn(object, property) { return Object.prototype.hasOwnProperty.call(object, property); } +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions +function escapeRegExp(string) { + return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); +} + // toIterable is required on Edge for cross-window origin objects. function toIterable(value) { if (typeof Symbol !== 'undefined' && typeof value[Symbol.iterator] !== 'undefined') { -- cgit v1.2.3