From 7b35650a4aa980a8e6f1f946ea6773c8568d8173 Mon Sep 17 00:00:00 2001 From: Zach Panter Date: Sat, 18 Jan 2025 17:18:20 -0700 Subject: [PATCH 1/2] Added atbash coder as option --- Atbash.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.ts | 15 +++++++++++++++ manifest.json | 2 +- package.json | 2 +- 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 Atbash.ts diff --git a/Atbash.ts b/Atbash.ts new file mode 100644 index 0000000..5dac655 --- /dev/null +++ b/Atbash.ts @@ -0,0 +1,51 @@ +import {Coder} from "./Coder"; + +export class AtbashEncoder implements Coder { + from: string; + to: string; + + constructor() { + this.from = "text"; + this.to = "atbash"; + } + + atbash(txt: string) { + return txt.replace(/[a-z]/gi, c => + "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba" + ["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".indexOf(c)]); + } + + transform(text: string): string { + return this.atbash(text); + } + + checkInput(text: string): boolean { + // For now, we assume that all text is valid. We will only encode A-Z and a-z. The rest will be left as is. + return true; + } +} + +export class AtbashDecoder implements Coder { + from: string; + to: string; + + constructor() { + this.from = "atbash"; + this.to = "text"; + } + + deatbash(txt: string) { + return txt.replace(/[a-z]/gi, c => + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + ["ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba".indexOf(c)]); + } + + transform(text: string): string { + return this.deatbash(text); + } + + checkInput(text: string): boolean { + // For now, we assume that all text is valid. We will only encode A-Z and a-z. The rest will be left as is. + return true; + } +} \ No newline at end of file diff --git a/main.ts b/main.ts index 1579c04..08c7648 100644 --- a/main.ts +++ b/main.ts @@ -3,6 +3,7 @@ import { App, MarkdownView, Plugin, MarkdownPostProcessorContext, PluginSettingT import { Coder } from "./Coder"; import { Base64Encoder, Base64Decoder } from "./Base64"; import { Rot13Encoder, Rot13Decoder } from "./Rot13"; +import { AtbashEncoder, AtbashDecoder } from "./Atbash"; export default class CoderPlugin extends Plugin { @@ -14,6 +15,10 @@ export default class CoderPlugin extends Plugin { this.registerMarkdownCodeBlockProcessor('transform-base64-text', this.processBase64ToText); this.registerMarkdownCodeBlockProcessor('transform-text-rot13', this.processTextToRot13); this.registerMarkdownCodeBlockProcessor('transform-rot13-text', this.processRot13ToText); + this.registerMarkdownCodeBlockProcessor('transform-text-text',this.processTextToAtbash); + this.registerMarkdownCodeBlockProcessor('transform-atbash-text',this.processAtbashToText); + + } // function to get a coder by from and to types @@ -50,6 +55,16 @@ export default class CoderPlugin extends Plugin { this.processText(content, el, coder); } + processTextToAtbash = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => { + let coder = this.getCoder("text", "atbash"); + this.processText(content, el, coder); + } + + processAtbashToText = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => { + let coder = this.getCoder("atbash", "text"); + this.processText(content, el, coder); + } + processText(content: string, el: HTMLElement, coder: Coder|null) { var destination; diff --git a/manifest.json b/manifest.json index 34131aa..233148b 100644 --- a/manifest.json +++ b/manifest.json @@ -3,7 +3,7 @@ "name": "Encoder/Decoder", "version": "1.2.0", "minAppVersion": "0.15.0", - "description": "Converts texts into other formats (base64, ROT13) and vice versa", + "description": "Converts texts into other formats (base64, ROT13, atbash) and vice versa", "author": "Rudi Häusler", "isDesktopOnly": false } diff --git a/package.json b/package.json index c0f5ee4..559f12f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "coder", "version": "1.1.0", - "description": "Converts text into other formats (base64, ROT13)", + "description": "Converts text into other formats (base64, ROT13, atbash)", "main": "main.js", "scripts": { "dev": "node esbuild.config.mjs", From f81abe518b9a8a2f69fae184f52af104dab5651c Mon Sep 17 00:00:00 2001 From: Zach Panter Date: Sun, 19 Jan 2025 06:34:22 -0700 Subject: [PATCH 2/2] Actually added Atbash to Coders array, fixed typo in transform-text-atbash --- main.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index 08c7648..15f4f9f 100644 --- a/main.ts +++ b/main.ts @@ -8,14 +8,14 @@ import { AtbashEncoder, AtbashDecoder } from "./Atbash"; export default class CoderPlugin extends Plugin { // List of coders - coders: Coder[] = [new Base64Encoder(), new Base64Decoder(), new Rot13Encoder(), new Rot13Decoder()]; + coders: Coder[] = [new Base64Encoder(), new Base64Decoder(), new Rot13Encoder(), new Rot13Decoder(), new AtbashEncoder(), new AtbashDecoder()]; async onload() { this.registerMarkdownCodeBlockProcessor('transform-text-base64', this.processTextToBase64); this.registerMarkdownCodeBlockProcessor('transform-base64-text', this.processBase64ToText); this.registerMarkdownCodeBlockProcessor('transform-text-rot13', this.processTextToRot13); this.registerMarkdownCodeBlockProcessor('transform-rot13-text', this.processRot13ToText); - this.registerMarkdownCodeBlockProcessor('transform-text-text',this.processTextToAtbash); + this.registerMarkdownCodeBlockProcessor('transform-text-atbash',this.processTextToAtbash); this.registerMarkdownCodeBlockProcessor('transform-atbash-text',this.processAtbashToText);