@@ -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
3888export 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;
0 commit comments