generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
66 lines (54 loc) · 1.77 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { App, MarkdownView, Plugin, MarkdownPostProcessorContext, PluginSettingTab, Setting } from 'obsidian';
import { Coder } from "./Coder";
import { Base64Encoder, Base64Decoder } from "./Base64";
import { Base85Encoder, Base85Decoder } from "./Base85";
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 Base85Encoder(),
new Base85Decoder(),
new Rot13Encoder(),
new Rot13Decoder(),
new AtbashEncoder(),
new AtbashDecoder()
];
async onload() {
this.coders.forEach(coder => {
this.registerMarkdownCodeBlockProcessor(
`transform-${coder.from}-${coder.to}`,
async (content, el, ctx) => {
this.processText(content, el, coder);
}
);
});
}
// function to get a coder by from and to types
getCoder(from: string, to: string): Coder | null {
return this.coders.find(coder => coder.from === from && coder.to === to) || null;
}
onunload() {
}
processText(content: string, el: HTMLElement, coder: Coder|null) {
var destination;
if(content.endsWith("\n")) {
// Obsidian gives an unpretty linebreak at the end. Don't encode it in our content!
content = content.substring(0, content.length - 1);
}
// convert the content variable to a byte array
if(coder != null) {
if(coder.checkInput(content)) {
destination = document.createTextNode(coder.transform(content));
} else {
destination = document.createTextNode("Invalid input for coder " + coder.from + " to " + coder.to);
}
} else {
destination = document.createTextNode( "No coder found!");
}
el.appendChild(destination);
return;
}
}