diff --git a/src/discordmessageparser.ts b/src/discordmessageparser.ts index 9797add..3d6de29 100644 --- a/src/discordmessageparser.ts +++ b/src/discordmessageparser.ts @@ -274,7 +274,7 @@ export class DiscordMessageParser { // matrix spoilers are still in MSC stage // see https://github.com/matrix-org/matrix-doc/pull/2010 if (!html) { - return `(Spoiler: ${node.content})`; + return `(Spoiler (rot13): ${Util.Rot13(node.content)})`; } return `${node.content}`; } diff --git a/src/util.ts b/src/util.ts index d869b1c..b11c291 100644 --- a/src/util.ts +++ b/src/util.ts @@ -30,6 +30,22 @@ export class Util { return htmlColor; } + public static Rot13(plaintext: string): string { + const UPPER_Z = 90; + const LOWER_Z = 122; + const CAESAR_SHIFT = 13; + const ALPHABET_LENGTH = 26; + return plaintext.replace(/[a-zA-Z]/g, (c) => { + const code = c.charCodeAt(0); + let shiftedCode = code + CAESAR_SHIFT; + if ((code <= UPPER_Z && shiftedCode > UPPER_Z) + || (code <= LOWER_Z && shiftedCode > LOWER_Z)) { + shiftedCode = shiftedCode - ALPHABET_LENGTH; + } + return String.fromCharCode(shiftedCode); + }); + } + public static async AsyncForEach(arr, callback) { for (let i = 0; i < arr.length; i++) { await callback(arr[i], i, arr); diff --git a/test/test_discordmessageparser.ts b/test/test_discordmessageparser.ts index 3265d66..35927a8 100644 --- a/test/test_discordmessageparser.ts +++ b/test/test_discordmessageparser.ts @@ -350,7 +350,7 @@ describe("DiscordMessageParser", () => { const mp = new DiscordMessageParser(); const msg = getMessage("||foxies||"); const result = await mp.FormatMessage(defaultOpts, msg); - expect(result.body).is.equal("(Spoiler: foxies)"); + expect(result.body).is.equal("(Spoiler (rot13): sbkvrf)"); expect(result.formattedBody).is.equal("foxies"); }); it("processes unknown emoji correctly", async () => {