From 0523a79b4a01815374ad031a008d7d2b2828b0cc Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Thu, 3 Aug 2023 16:44:40 +0300 Subject: [PATCH] add escapes to formatters --- package.json | 2 +- src/types/message-format.ts | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8381d6d..b91bd92 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@squeebot/core", - "version": "3.6.0-1", + "version": "3.6.1", "description": "Squeebot v3 core for the execution environment", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/types/message-format.ts b/src/types/message-format.ts index 696355a..8ef41fc 100644 --- a/src/types/message-format.ts +++ b/src/types/message-format.ts @@ -123,6 +123,9 @@ export class Formatter { */ public colorEscape = ''; + /** Replacements for string escape function */ + public escapeReplacements: [RegExp, string, string][] = []; + constructor( /** * @deprecated Use feature flags @@ -167,10 +170,27 @@ export class Formatter { } } + /** + * Strip any extraneous tags/info. + * @param msg Message + * @returns Stripped message + */ public strip(msg: string): string { return msg; } + /** + * Escape any extraneous tags/info. + * @param msg Message + * @returns Escaped message + */ + public escape(msg: string): string { + return this.escapeReplacements.reduce( + (str, replacement) => str.replace(replacement[0], replacement[1]), + msg + ); + } + /** * Object compositor. * @@ -284,6 +304,14 @@ export class HTMLFormatter extends Formatter { multicode: {start: '
', end: '
'}, }; + public escapeReplacements: [RegExp, string, string][] = [ + [/&/g, '&', 'ampersand'], + [//g, '>', 'angle brackets'], + [/"/g, '"', 'quotes'], + [/'/g, ''', 'single quates'], + ]; + constructor(flags?: ProtocolFeatureFlag[]) { super(true, true, flags); } @@ -323,6 +351,20 @@ export class MarkdownFormatter extends Formatter { multicode: {start: '```', end: '```'}, }; + public escapeReplacements: [RegExp, string, string][] = [ + [/\*/g, '\\*', 'asterisks'], + [/#/g, '\\#', 'number signs'], + [/\//g, '\\/', 'slashes'], + [/\(/g, '\\(', 'parentheses'], + [/\)/g, '\\)', 'parentheses'], + [/\[/g, '\\[', 'square brackets'], + [/\]/g, '\\]', 'square brackets'], + [//g, '>', 'angle brackets'], + [/_/g, '\\_', 'underscores'], + [/`/g, '\\`', 'codeblocks'] + ]; + constructor(flags?: ProtocolFeatureFlag[]) { super(true, false, flags); }