add escapes to formatters

This commit is contained in:
Evert Prants 2023-08-03 16:44:40 +03:00
parent 767b431022
commit 0523a79b4a
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 43 additions and 1 deletions

View File

@ -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",

View File

@ -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: '<pre><code>', end: '</code></pre>'},
};
public escapeReplacements: [RegExp, string, string][] = [
[/&/g, '&amp;', 'ampersand'],
[/</g, '&lt;', 'angle brackets'],
[/>/g, '&gt;', 'angle brackets'],
[/"/g, '&quot;', 'quotes'],
[/'/g, '&#039;', '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, '&lt;', 'angle brackets'],
[/>/g, '&gt;', 'angle brackets'],
[/_/g, '\\_', 'underscores'],
[/`/g, '\\`', 'codeblocks']
];
constructor(flags?: ProtocolFeatureFlag[]) {
super(true, false, flags);
}