Skip to content

Commit f055466

Browse files
committed
Added Base64 to text decoding.
1 parent 1304283 commit f055466

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Base64.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,23 @@ export class Base64Encoder implements Coder {
1313
return typeof text === "string" && text.length > 0;
1414
}
1515
}
16+
17+
export class Base64Decoder implements Coder {
18+
19+
from = "base64";
20+
to = "text";
21+
22+
transform(text: string): string {
23+
try {
24+
return atob(text);
25+
} catch (e) {
26+
throw new Error("Invalid Base64 input");
27+
}
28+
}
29+
30+
checkInput(text: string): boolean {
31+
// Check if the input is a valid Base64 string
32+
const base64Regex = /^[A-Za-z0-9+/=]+$/;
33+
return typeof text === "string" && base64Regex.test(text) && text.length % 4 === 0;
34+
}
35+
}

main.ts

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

33
import { Coder } from "./Coder";
4-
import { Base64Encoder } from "./Base64";
4+
import { Base64Encoder, Base64Decoder } from "./Base64";
55
import { Rot13Encoder, Rot13Decoder } from "./Rot13";
66

77
export default class CoderPlugin extends Plugin {
88

99
// List of coders
10-
coders: Coder[] = [new Base64Encoder(), new Rot13Encoder(), new Rot13Decoder()];
10+
coders: Coder[] = [new Base64Encoder(), new Base64Decoder(), new Rot13Encoder(), new Rot13Decoder()];
1111

1212
async onload() {
1313
this.registerMarkdownCodeBlockProcessor('transform-text-base64', this.processTextToBase64);
14+
this.registerMarkdownCodeBlockProcessor('transform-base64-text', this.processBase64ToText);
1415
this.registerMarkdownCodeBlockProcessor('transform-text-rot13', this.processTextToRot13);
1516
this.registerMarkdownCodeBlockProcessor('transform-rot13-text', this.processRot13ToText);
1617
}
@@ -34,6 +35,11 @@ export default class CoderPlugin extends Plugin {
3435
this.processText(content, el, coder);
3536
}
3637

38+
processBase64ToText = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
39+
let coder = this.getCoder("base64", "text");
40+
this.processText(content, el, coder);
41+
}
42+
3743
processTextToRot13 = async (content: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
3844
let coder = this.getCoder("text", "rot13");
3945
this.processText(content, el, coder);

0 commit comments

Comments
 (0)