Update doReturn() API with objectToHtmlFunc - Allow future commands to provide a different "object to HTML" converter func than "object to text", if need be
| @@ -41,10 +41,13 @@ export const slashCommandReturnHelper = { | ||
| 41 | 41 | * @param {object|number|string} value The value to return |
| 42 | 42 | * @param {object} [options={}] Options |
| 43 | 43 | * @param {(o: object) => string} [options.objectToStringFunc=null] Function to convert the object to a string, if object was provided and 'object' was not the chosen return type |
| 44 | + * @param {(o: object) => string} [options.objectToHtmlFunc=null] Analog to 'objectToStringFunc', which will be used here if not provided - but can do a different string layout if HTML is requested | |
| 44 | 45 | * @returns {Promise<*>} The processed return value |
| 45 | 46 | */ |
| 46 | 47 | async doReturn(type, value, { objectToStringFunc = o => o?.toString(), objectToHtmlFunc = null } = {}) { |
| 47 | - const stringValue = typeof value !== 'string' ? objectToStringFunc(value) : value; | |
| 48 | + const shouldHtml = type.endsWith('html'); | |
| 49 | + const actualConverterFunc = shouldHtml && objectToHtmlFunc ? objectToHtmlFunc : objectToStringFunc; | |
| 50 | + const stringValue = typeof value !== 'string' ? actualConverterFunc(value) : value; | |
| 48 | 51 | |
| 49 | 52 | switch (type) { |
| 50 | 53 | case 'popup-html': |
| @@ -53,12 +56,11 @@ export const slashCommandReturnHelper = { | ||
| 53 | 56 | case 'chat-html': |
| 54 | 57 | case 'toast-text': |
| 55 | 58 | case 'toast-html': { |
| 56 | - const shouldHtml = type.endsWith('html'); | |
| 59 | + const htmlOrNotHtml = shouldHtml ? (new showdown.Converter()).makeHtml(stringValue) : escapeHtml(stringValue); | |
| 57 | - const makeHtml = (str) => (new showdown.Converter()).makeHtml(str); | |
| 58 | 60 | |
| 59 | 61 | if (type.startsWith('popup')) await callGenericPopup(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue)htmlOrNotHtml, POPUP_TYPE.TEXT); |
| 60 | 62 | if (type.startsWith('chat')) sendSystemMessage(system_message_types.GENERIC, shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue)htmlOrNotHtml); |
| 61 | 63 | if (type.startsWith('toast')) toastr.info(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue)htmlOrNotHtml, null, { escapeHtml: !shouldHtml }); |
| 62 | 64 | |
| 63 | 65 | return ''; |
| 64 | 66 | } |