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..15f4f9f 100644 --- a/main.ts +++ b/main.ts @@ -3,17 +3,22 @@ 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 { // 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-atbash',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",