|
1 | 1 | import { App, MarkdownView, Plugin, MarkdownPostProcessorContext, PluginSettingTab, Setting } from 'obsidian';
|
2 | 2 |
|
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"; |
87 | 6 |
|
88 | 7 | export default class CoderPlugin extends Plugin {
|
89 | 8 |
|
@@ -147,27 +66,5 @@ export default class CoderPlugin extends Plugin {
|
147 | 66 | el.appendChild(destination);
|
148 | 67 | return;
|
149 | 68 | }
|
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 |
| - |
172 | 69 | }
|
173 | 70 |
|
0 commit comments