From 7a1570885e7310e5b1e15ab798c9eb66694a96fb Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 9 Jul 2021 20:14:05 -0400 Subject: Simplify get media structure (#1817) * Remove format argument of getMedia * Implement escaping --- .../anki-field-templates-upgrade-v13.handlebars | 12 ++++++------ .../templates/default-anki-field-templates.handlebars | 12 ++++++------ ext/js/data/anki-note-builder.js | 16 ++++++++-------- ext/js/templates/template-renderer-media-provider.js | 16 ++++++++++++---- 4 files changed, 32 insertions(+), 24 deletions(-) (limited to 'ext') diff --git a/ext/data/templates/anki-field-templates-upgrade-v13.handlebars b/ext/data/templates/anki-field-templates-upgrade-v13.handlebars index 25007030..b3e0f1b9 100644 --- a/ext/data/templates/anki-field-templates-upgrade-v13.handlebars +++ b/ext/data/templates/anki-field-templates-upgrade-v13.handlebars @@ -1,11 +1,11 @@ {{#*inline "selection-text"}} - {{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText" format="text"}}{{/getMedia}}{{/if~}} + {{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText"}}{{/getMedia}}{{/if~}} {{/inline}} {{#*inline "sentence-furigana"}} {{~#if definition.cloze~}} {{~#if (hasMedia "textFurigana" definition.cloze.sentence)~}} - {{#getMedia "textFurigana" definition.cloze.sentence format="html"}}{{/getMedia}} + {{#getMedia "textFurigana" definition.cloze.sentence escape=false}}{{/getMedia}} {{~else~}} {{definition.cloze.sentence}} {{~/if~}} @@ -36,7 +36,7 @@ {{~/if~}} {{=======}} {{~#if (hasMedia "audio")~}} - [sound:{{#getMedia "audio" format="fileName"}}{{/getMedia}}] + [sound:{{#getMedia "audio"}}{{/getMedia}}] {{~/if~}} {{>>>>>>>}} @@ -44,7 +44,7 @@ {{=======}} {{~#if (hasMedia "screenshot")~}} - + {{~/if~}} {{>>>>>>>}} @@ -54,12 +54,12 @@ {{~/if~}} {{=======}} {{~#if (hasMedia "clipboardImage")~}} - + {{~/if~}} {{>>>>>>>}} {{<<<<<<<}} {{~#if definition.clipboardText~}}{{definition.clipboardText}}{{~/if~}} {{=======}} -{{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText" format="text"}}{{/getMedia}}{{/if~}} +{{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText"}}{{/getMedia}}{{/if~}} {{>>>>>>>}} diff --git a/ext/data/templates/default-anki-field-templates.handlebars b/ext/data/templates/default-anki-field-templates.handlebars index cdbec87d..92f9c6b7 100644 --- a/ext/data/templates/default-anki-field-templates.handlebars +++ b/ext/data/templates/default-anki-field-templates.handlebars @@ -32,7 +32,7 @@ {{#*inline "audio"}} {{~#if (hasMedia "audio")~}} - [sound:{{#getMedia "audio" format="fileName"}}{{/getMedia}}] + [sound:{{#getMedia "audio"}}{{/getMedia}}] {{~/if~}} {{/inline}} @@ -174,7 +174,7 @@ {{#*inline "screenshot"}} {{~#if (hasMedia "screenshot")~}} - + {{~/if~}} {{/inline}} @@ -294,12 +294,12 @@ {{#*inline "clipboard-image"}} {{~#if (hasMedia "clipboardImage")~}} - + {{~/if~}} {{/inline}} {{#*inline "clipboard-text"}} - {{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText" format="text"}}{{/getMedia}}{{/if~}} + {{~#if (hasMedia "clipboardText")}}{{#getMedia "clipboardText"}}{{/getMedia}}{{/if~}} {{/inline}} {{#*inline "conjugation"}} @@ -379,13 +379,13 @@ {{/inline}} {{#*inline "selection-text"}} - {{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText" format="text"}}{{/getMedia}}{{/if~}} + {{~#if (hasMedia "selectionText")}}{{#getMedia "selectionText"}}{{/getMedia}}{{/if~}} {{/inline}} {{#*inline "sentence-furigana"}} {{~#if definition.cloze~}} {{~#if (hasMedia "textFurigana" definition.cloze.sentence)~}} - {{#getMedia "textFurigana" definition.cloze.sentence format="html"}}{{/getMedia}} + {{#getMedia "textFurigana" definition.cloze.sentence escape=false}}{{/getMedia}} {{~else~}} {{definition.cloze.sentence}} {{~/if~}} diff --git a/ext/js/data/anki-note-builder.js b/ext/js/data/anki-note-builder.js index 02aa7969..621816d4 100644 --- a/ext/js/data/anki-note-builder.js +++ b/ext/js/data/anki-note-builder.js @@ -362,14 +362,14 @@ class AnkiNoteBuilder { (dictionaryMedia[dictionary]) : (dictionaryMedia[dictionary] = {}) ); - dictionaryMedia2[path] = {fileName}; + dictionaryMedia2[path] = {value: fileName}; } const media = { - audio: (typeof audioFileName === 'string' ? {fileName: audioFileName} : null), - screenshot: (typeof screenshotFileName === 'string' ? {fileName: screenshotFileName} : null), - clipboardImage: (typeof clipboardImageFileName === 'string' ? {fileName: clipboardImageFileName} : null), - clipboardText: (typeof clipboardText === 'string' ? {text: clipboardText} : null), - selectionText: (typeof selectionText === 'string' ? {text: selectionText} : null), + audio: (typeof audioFileName === 'string' ? {value: audioFileName} : null), + screenshot: (typeof screenshotFileName === 'string' ? {value: screenshotFileName} : null), + clipboardImage: (typeof clipboardImageFileName === 'string' ? {value: clipboardImageFileName} : null), + clipboardText: (typeof clipboardText === 'string' ? {value: clipboardText} : null), + selectionText: (typeof selectionText === 'string' ? {value: selectionText} : null), textFurigana, dictionaryMedia }; @@ -391,8 +391,8 @@ class AnkiNoteBuilder { break; } if (data !== null) { - const html = this._createFuriganaHtml(data, readingMode); - results.push({text, readingMode, details: {html}}); + const value = this._createFuriganaHtml(data, readingMode); + results.push({text, readingMode, details: {value}}); } } return results; diff --git a/ext/js/templates/template-renderer-media-provider.js b/ext/js/templates/template-renderer-media-provider.js index 604b5331..114d3387 100644 --- a/ext/js/templates/template-renderer-media-provider.js +++ b/ext/js/templates/template-renderer-media-provider.js @@ -15,6 +15,10 @@ * along with this program. If not, see . */ +/* global + * Handlebars + */ + class TemplateRendererMediaProvider { constructor() { this._requirements = null; @@ -38,8 +42,7 @@ class TemplateRendererMediaProvider { const {media} = root; const data = this._getMediaData(media, args, namedArgs); if (data !== null) { - const {format} = namedArgs; - const result = this._getFormattedValue(data, format); + const result = this._getFormattedValue(data, namedArgs); if (typeof result === 'string') { return result; } } const defaultValue = namedArgs.default; @@ -53,8 +56,13 @@ class TemplateRendererMediaProvider { this._requirements.push(value); } - _getFormattedValue(data, format) { - return Object.prototype.hasOwnProperty.call(data, format) ? data[format] : null; + _getFormattedValue(data, namedArgs) { + let {value} = data; + const {escape=true} = namedArgs; + if (escape) { + value = Handlebars.Utils.escapeExpression(value); + } + return value; } _getMediaData(media, args, namedArgs) { -- cgit v1.2.3