Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Apply rot13 to discord spoilers in unformatted matrix message body #14

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/discordmessageparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<span data-mx-spoiler>${node.content}</span>`;
}
Expand Down
16 changes: 16 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/test_discordmessageparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("<span data-mx-spoiler>foxies</span>");
});
it("processes unknown emoji correctly", async () => {
Expand Down