UNPKG

@raintech-oss/jovo-dew

Version:
81 lines 2.87 kB
import { sample } from 'lodash'; import path from 'path'; import { urlJoin } from 'url-join-ts'; export function missingInterpolationHandler(text, keys) { let result; let lastKey; // Sample keys: ['{{name}}', 'name'] for (const key of keys) { lastKey = key; if (!key.startsWith('{{')) { // check variables file for function named 'key' result = processAsVariable(this, key); if (result !== undefined) { break; } // check audios file for variables named 'key' result = processAsAudio(this, key); if (result !== undefined) { break; } } } if (result === undefined) { result = `[missing variable: ${lastKey}]`; } return result.toString(); } function processAsVariable(plugin, key) { if (plugin.viewVariables) { const func = plugin.viewVariables[key]; if (func) { let result = func.call(plugin.viewVariables); let myValue; if (result.then) { result.then((value) => { // console.log("processAsVariable test1"); myValue = value; }, () => { // console.log("processAsVariable test2"); }); result = myValue; } return result; } } } function processAsAudio(plugin, key) { var _a, _b, _c; if (plugin.audioItems) { const items = plugin.audioItems.filter((a) => a.variableName === key); if (items.length > 0) { const item = sample(items); if (item) { if (item.filename && item.filename !== '') { const baseUrl = (_a = plugin.config.audio) === null || _a === void 0 ? void 0 : _a.baseUrl; const defaultExt = (_c = (_b = plugin.config.audio) === null || _b === void 0 ? void 0 : _b.defaultExt) !== null && _c !== void 0 ? _c : '.mp3'; if (baseUrl) { const fullPath = getAudioFullPath(item, baseUrl, defaultExt); return `<audio src="${fullPath}" />`; } } if (item.text) { return item.text; } } } } return; } function getAudioFullPath(item, baseUrl, defaultExt) { if (!item.filename) { return ''; } const ext = path.extname(item.filename); const fileName = ext ? item.filename : item.filename + defaultExt; if (fileName.toLowerCase().startsWith('http')) { return fileName; } return urlJoin(baseUrl, fileName); } //# sourceMappingURL=missingInterpolationHandler.js.map