Skip to content

Commit 84d0a3e

Browse files
committed
Refactoring of code. Coder interface and Coder implementations moved into own files.
1 parent 3a1ee01 commit 84d0a3e

File tree

4 files changed

+80
-106
lines changed

4 files changed

+80
-106
lines changed

Base64.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Coder } from "./Coder";
2+
3+
export class Base64Encoder implements Coder {
4+
from = "text";
5+
to = "base64";
6+
7+
transform(text: string): string {
8+
return btoa(text);
9+
}
10+
11+
checkInput(text: string): boolean {
12+
// Check if the input is a string and not null or undefined
13+
return typeof text === "string" && text.length > 0;
14+
}
15+
}

Coder.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
// Abstract class for all coders
3+
interface Coder {
4+
5+
from: string;
6+
to: string;
7+
8+
transform (text:string) : string;
9+
checkInput (text:string) : boolean;
10+
11+
}

Rot13.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Coder } from "./Coder";
2+
3+
export class Rot13Encoder implements Coder {
4+
from:string;
5+
to: string;
6+
7+
constructor() {
8+
this.from = "text";
9+
this.to = "rot13";
10+
}
11+
12+
rot13(txt:string) {
13+
return txt.replace(/[a-z]/gi, c =>
14+
"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"
15+
[ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".indexOf(c) ] );
16+
}
17+
18+
transform (text:string) : string {
19+
return this.rot13(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 Rot13Decoder implements Coder {
29+
from:string;
30+
to: string;
31+
32+
constructor() {
33+
this.from = "rot13";
34+
this.to = "text";
35+
}
36+
37+
derot13(txt:string) {
38+
return txt.replace(/[a-z]/gi, c =>
39+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
40+
[ "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm".indexOf(c) ] );
41+
}
42+
43+
transform (text:string) : string {
44+
return this.derot13(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

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,8 @@
11
import { App, MarkdownView, Plugin, MarkdownPostProcessorContext, PluginSettingTab, Setting } from 'obsidian';
22

3-
import * as base64js from 'base64-js';
4-
5-
6-
// Abstract class for all coders
7-
interface Coder {
8-
9-
from: string;
10-
to: string;
11-
12-
transform (text:string) : string;
13-
checkInput (text:string) : boolean;
14-
15-
}
16-
17-
class Base64Encoder implements Coder {
18-
from:string;
19-
to: string;
20-
21-
constructor() {
22-
this.from = "text";
23-
this.to = "base64";
24-
}
25-
26-
transform (text:string) : string {
27-
let utf8Encode = new TextEncoder();
28-
return base64js.fromByteArray(utf8Encode.encode(text));
29-
}
30-
31-
checkInput(text: string): boolean {
32-
// For now, we assume that all text is valid
33-
return true;
34-
}
35-
}
36-
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-
3+
import { Coder } from "./Coder";
4+
import { Base64Encoder } from "./Base64";
5+
import { Rot13Encoder, Rot13Decoder } from "./Rot13";
876

887
export default class CoderPlugin extends Plugin {
898

@@ -147,27 +66,5 @@ export default class CoderPlugin extends Plugin {
14766
el.appendChild(destination);
14867
return;
14968
}
150-
151-
/**
152-
processBase64ToText = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
153-
var destination;
154-
155-
if(content.endsWith("\n")) {
156-
// Obsidian gives an unpretty linebreak at the end. Don't encode it in our content!
157-
content = content.substring(0, content.length - 1);
158-
}
159-
let coder = this.getCoder("base64", "text");
160-
161-
// convert the content variable to a byte array
162-
if(coder != null) {
163-
destination = document.createTextNode (coder.transform(content));
164-
} else {
165-
destination = document.createTextNode ( "No coder found!" );
166-
}
167-
168-
el.appendChild(destination);
169-
return;
170-
}**/
171-
17269
}
17370

0 commit comments

Comments
 (0)