Skip to content

Commit 3a1ee01

Browse files
committed
Added ROT 13 encoder and decoder
1 parent b2b3c3b commit 3a1ee01

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@ From Obsidian v0.9.8, you can activate this plugin within Obsidian by doing the
2323

2424
## Usage
2525

26-
Type the `transform-text-base64` keyword to use the Plugin.
26+
Following conversions are available
27+
28+
| Source | Destination | Markdown keyword |
29+
|-----------|---------------|------------------------|
30+
| text | base64 | transform-text-base64 |
31+
| text | ROT13 | transform-text-rot13 |
32+
| ROT13 | text | transform-rot13-text |
33+
34+
35+
Type the Markdown keyword to use the specific encoding.
36+
37+
For example if you like to print out a given text as base64 you have to write:
2738

2839
````markdown
2940
```transform-text-base64
@@ -36,6 +47,10 @@ The result will be this:
3647
dGhpcyBpcyBhIHRleHQgdG8gZW5jb2Rl
3748

3849
## Version History
50+
51+
### 1.1.0
52+
- Added ROT13 conversion
53+
3954
### 1.0.0
4055
- First version to convert text to base64
4156

@@ -47,7 +62,6 @@ Upcoming changes for this plugin:
4762
- Base64 to text
4863
- Text to Vigenere
4964
- Vigenere to text
50-
- Text to ROT13
5165
- ROT13 to text
5266
- Text to hex
5367
- Hex to text

main.ts

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,66 @@ class Base64Encoder implements Coder {
3434
}
3535
}
3636

37+
class Rot13Encoder implements Coder {
38+
from:string;
39+
to: string;
40+
41+
constructor() {
42+
this.from = "text";
43+
this.to = "rot13";
44+
}
45+
46+
rot13(txt:string) {
47+
return txt.replace(/[a-z]/gi, c =>
48+
"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
49+
[ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".indexOf(c) ] );
50+
}
51+
52+
transform (text:string) : string {
53+
return this.rot13(text);
54+
}
55+
56+
checkInput(text: string): boolean {
57+
// 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.
58+
return true;
59+
}
60+
}
61+
62+
class Rot13Decoder implements Coder {
63+
from:string;
64+
to: string;
65+
66+
constructor() {
67+
this.from = "rot13";
68+
this.to = "text";
69+
}
70+
71+
derot13(txt:string) {
72+
return txt.replace(/[a-z]/gi, c =>
73+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
74+
[ "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm".indexOf(c) ] );
75+
}
76+
77+
transform (text:string) : string {
78+
return this.derot13(text);
79+
}
80+
81+
checkInput(text: string): boolean {
82+
// 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.
83+
return true;
84+
}
85+
}
86+
3787

3888
export default class CoderPlugin extends Plugin {
3989

4090
// List of coders
41-
coders: Coder[] = [new Base64Encoder()];
91+
coders: Coder[] = [new Base64Encoder(), new Rot13Encoder(), new Rot13Decoder()];
4292

4393
async onload() {
4494
this.registerMarkdownCodeBlockProcessor('transform-text-base64', this.processTextToBase64);
95+
this.registerMarkdownCodeBlockProcessor('transform-text-rot13', this.processTextToRot13);
96+
this.registerMarkdownCodeBlockProcessor('transform-rot13-text', this.processRot13ToText);
4597
}
4698

4799
// function to get a coder by from and to types
@@ -58,15 +110,28 @@ export default class CoderPlugin extends Plugin {
58110

59111
}
60112

61-
62113
processTextToBase64 = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
114+
let coder = this.getCoder("text", "base64");
115+
this.processText(content, el, coder);
116+
}
117+
118+
processTextToRot13 = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
119+
let coder = this.getCoder("text", "rot13");
120+
this.processText(content, el, coder);
121+
}
122+
123+
processRot13ToText = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
124+
let coder = this.getCoder("rot13", "text");
125+
this.processText(content, el, coder);
126+
}
127+
128+
processText(content: string, el: HTMLElement, coder: Coder|null) {
63129
var destination;
64130

65131
if(content.endsWith("\n")) {
66132
// Obsidian gives an unpretty linebreak at the end. Don't encode it in our content!
67133
content = content.substring(0, content.length - 1);
68134
}
69-
let coder = this.getCoder("text", "base64");
70135

71136
// convert the content variable to a byte array
72137
if(coder != null) {
@@ -83,7 +148,6 @@ export default class CoderPlugin extends Plugin {
83148
return;
84149
}
85150

86-
87151
/**
88152
processBase64ToText = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
89153
var destination;

manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"id": "coder",
33
"name": "Encoder/Decoder",
4-
"version": "1.0.3",
4+
"version": "1.1.0",
55
"minAppVersion": "0.15.0",
6-
"description": "Converts a text into base64 format.",
6+
"description": "Converts text into other formats (base64, ROT13)",
77
"author": "Rudi Häusler",
88
"isDesktopOnly": false
99
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "coder",
3-
"version": "1.0.0",
4-
"description": "Converts a text into base64 format",
3+
"version": "1.1.0",
4+
"description": "Converts text into other formats (base64, ROT13)",
55
"main": "main.js",
66
"scripts": {
77
"dev": "node esbuild.config.mjs",

versions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"1.0.0": "0.15.0",
33
"1.0.1": "0.15.0",
44
"1.0.2": "0.15.0",
5-
"1.0.3": "0.15.0"
5+
"1.0.3": "0.15.0",
6+
"1.1.0": "0.15.0"
67
}

0 commit comments

Comments
 (0)