Skip to content

Commit 7b35650

Browse files
author
Zach Panter
committed
Added atbash coder as option
1 parent 18cadb2 commit 7b35650

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

Atbash.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {Coder} from "./Coder";
2+
3+
export class AtbashEncoder implements Coder {
4+
from: string;
5+
to: string;
6+
7+
constructor() {
8+
this.from = "text";
9+
this.to = "atbash";
10+
}
11+
12+
atbash(txt: string) {
13+
return txt.replace(/[a-z]/gi, c =>
14+
"ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba"
15+
["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".indexOf(c)]);
16+
}
17+
18+
transform(text: string): string {
19+
return this.atbash(text);
20+
}
21+
22+
checkInput(text: string): boolean {
23+
// 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.
24+
return true;
25+
}
26+
}
27+
28+
export class AtbashDecoder implements Coder {
29+
from: string;
30+
to: string;
31+
32+
constructor() {
33+
this.from = "atbash";
34+
this.to = "text";
35+
}
36+
37+
deatbash(txt: string) {
38+
return txt.replace(/[a-z]/gi, c =>
39+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
40+
["ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba".indexOf(c)]);
41+
}
42+
43+
transform(text: string): string {
44+
return this.deatbash(text);
45+
}
46+
47+
checkInput(text: string): boolean {
48+
// 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.
49+
return true;
50+
}
51+
}

main.ts

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { App, MarkdownView, Plugin, MarkdownPostProcessorContext, PluginSettingT
33
import { Coder } from "./Coder";
44
import { Base64Encoder, Base64Decoder } from "./Base64";
55
import { Rot13Encoder, Rot13Decoder } from "./Rot13";
6+
import { AtbashEncoder, AtbashDecoder } from "./Atbash";
67

78
export default class CoderPlugin extends Plugin {
89

@@ -14,6 +15,10 @@ export default class CoderPlugin extends Plugin {
1415
this.registerMarkdownCodeBlockProcessor('transform-base64-text', this.processBase64ToText);
1516
this.registerMarkdownCodeBlockProcessor('transform-text-rot13', this.processTextToRot13);
1617
this.registerMarkdownCodeBlockProcessor('transform-rot13-text', this.processRot13ToText);
18+
this.registerMarkdownCodeBlockProcessor('transform-text-text',this.processTextToAtbash);
19+
this.registerMarkdownCodeBlockProcessor('transform-atbash-text',this.processAtbashToText);
20+
21+
1722
}
1823

1924
// function to get a coder by from and to types
@@ -50,6 +55,16 @@ export default class CoderPlugin extends Plugin {
5055
this.processText(content, el, coder);
5156
}
5257

58+
processTextToAtbash = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
59+
let coder = this.getCoder("text", "atbash");
60+
this.processText(content, el, coder);
61+
}
62+
63+
processAtbashToText = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
64+
let coder = this.getCoder("atbash", "text");
65+
this.processText(content, el, coder);
66+
}
67+
5368
processText(content: string, el: HTMLElement, coder: Coder|null) {
5469
var destination;
5570

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "Encoder/Decoder",
44
"version": "1.2.0",
55
"minAppVersion": "0.15.0",
6-
"description": "Converts texts into other formats (base64, ROT13) and vice versa",
6+
"description": "Converts texts into other formats (base64, ROT13, atbash) and vice versa",
77
"author": "Rudi Häusler",
88
"isDesktopOnly": false
99
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "coder",
33
"version": "1.1.0",
4-
"description": "Converts text into other formats (base64, ROT13)",
4+
"description": "Converts text into other formats (base64, ROT13, atbash)",
55
"main": "main.js",
66
"scripts": {
77
"dev": "node esbuild.config.mjs",

0 commit comments

Comments
 (0)