aboutsummaryrefslogtreecommitdiff
path: root/ext/js/templates
diff options
context:
space:
mode:
Diffstat (limited to 'ext/js/templates')
-rw-r--r--ext/js/templates/sandbox/anki-template-renderer.js42
1 files changed, 34 insertions, 8 deletions
diff --git a/ext/js/templates/sandbox/anki-template-renderer.js b/ext/js/templates/sandbox/anki-template-renderer.js
index ad2b0042..d2bb25d5 100644
--- a/ext/js/templates/sandbox/anki-template-renderer.js
+++ b/ext/js/templates/sandbox/anki-template-renderer.js
@@ -674,6 +674,7 @@ export class AnkiTemplateRenderer {
*/
_formatGlossary(args, _context, options) {
const [dictionary, content] = /** @type {[dictionary: string, content: import('dictionary-data').TermGlossaryContent]} */ (args);
+ /** @type {import('anki-templates').NoteData} */
const data = options.data.root;
if (typeof content === 'string') { return this._stringToMultiLineHtml(this._escape(content)); }
if (!(typeof content === 'object' && content !== null)) { return ''; }
@@ -713,31 +714,42 @@ export class AnkiTemplateRenderer {
* @type {import('template-renderer').HelperFunction<boolean>}
*/
_hasMedia(args, _context, options) {
- return this._mediaProvider.hasMedia(options.data.root, args, options.hash);
+ /** @type {import('anki-templates').NoteData} */
+ const data = options.data.root;
+ return this._mediaProvider.hasMedia(data, args, options.hash);
}
/**
* @type {import('template-renderer').HelperFunction<?string>}
*/
_getMedia(args, _context, options) {
- return this._mediaProvider.getMedia(options.data.root, args, options.hash);
+ /** @type {import('anki-templates').NoteData} */
+ const data = options.data.root;
+ return this._mediaProvider.getMedia(data, args, options.hash);
}
/**
* @type {import('template-renderer').HelperFunction<string>}
*/
_pronunciation(_args, _context, options) {
- let {format, reading, downstepPosition, nasalPositions, devoicePositions} = options.hash;
-
- if (typeof reading !== 'string' || reading.length === 0) { return ''; }
- if (typeof downstepPosition !== 'number') { return ''; }
- if (!Array.isArray(nasalPositions)) { nasalPositions = []; }
- if (!Array.isArray(devoicePositions)) { devoicePositions = []; }
+ const {format, reading, downstepPosition} = options.hash;
+
+ if (
+ typeof reading !== 'string' ||
+ reading.length === 0 ||
+ typeof downstepPosition !== 'number'
+ ) {
+ return '';
+ }
const morae = getKanaMorae(reading);
switch (format) {
case 'text':
+ {
+ const nasalPositions = this._getValidNumberArray(options.hash.nasalPositions);
+ const devoicePositions = this._getValidNumberArray(options.hash.devoicePositions);
return this._getPronunciationHtml(createPronunciationText(morae, downstepPosition, nasalPositions, devoicePositions));
+ }
case 'graph':
return this._getPronunciationHtml(createPronunciationGraph(morae, downstepPosition));
case 'position':
@@ -748,6 +760,20 @@ export class AnkiTemplateRenderer {
}
/**
+ * @param {unknown} value
+ * @returns {number[]}
+ */
+ _getValidNumberArray(value) {
+ const result = [];
+ if (Array.isArray(value)) {
+ for (const item of value) {
+ if (typeof item === 'number') { result.push(item); }
+ }
+ }
+ return result;
+ }
+
+ /**
* @type {import('template-renderer').HelperFunction<string>}
*/
_hiragana(args, context, options) {