Skip to content

Commit e1a9bc8

Browse files
committed
Update JS libs + Add reload script feature
1 parent 70412d0 commit e1a9bc8

File tree

4 files changed

+92
-53
lines changed

4 files changed

+92
-53
lines changed

README.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ Child elements of `encrypted_something` are build with a key `<unique name>` in
237237
The list have to be contructed with the name of an HTML element `<html tag>` as first item and `id` or `class` as the second item.
238238

239239
```yaml
240-
encrypted_something:
241-
<uniq name>: [<html tag>, <'class' or 'id'>]
240+
plugins:
241+
- encryptcontent:
242+
encrypted_something:
243+
<uniq name>: [<html tag>, <'class' or 'id'>]
242244
```
243245

244246
The `<unique name>` key identifies the name of a specific element of the page that will be searched by beautifulSoup.
@@ -273,8 +275,10 @@ Then add these elements in the format of a yaml dictionary under the variable `e
273275
Set your configuration like this :
274276

275277
```yaml
276-
encrypted_something:
277-
mkdocs-encrypted-toc: [div, id]
278+
plugins:
279+
- encryptcontent:
280+
encrypted_something:
281+
mkdocs-encrypted-toc: [div, id]
278282
```
279283

280284
2. Other example, with multiples target. In you Material Theme, you want to encrypt ToC content and Footer.
@@ -298,16 +302,18 @@ After modification, your template looks like this :
298302

299303
Your configuration like this :
300304
```yaml
301-
encrypted_something:
302-
mkdocs-encrypted-toc: [nav, class]
303-
mkdocs-encrypted-footer: [div, id]
304-
mkdocs-encrypted-footer-meta: [div, id]
305+
plugins:
306+
- encryptcontent:
307+
encrypted_something:
308+
mkdocs-encrypted-toc: [nav, class]
309+
mkdocs-encrypted-footer: [div, id]
310+
mkdocs-encrypted-footer-meta: [div, id]
305311
```
306312

307313

308314
### Search index encryption
309315

310-
> **ALPHA VERSION**, use at your own risks. ONLY work with themes using default mkdocs search.
316+
> **ALPHA VERSION**, use at your own risks. **ONLY** work with themes using default mkdocs search.
311317

312318
Related to [issue #13](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/issues/13)
313319

@@ -338,6 +344,28 @@ This functionality overwrite the index creation function of the “search” plu
338344
When the configuration mode is set to "**dynamically**", the javascripts contrib files of the default search plugin are also overloaded to include a process for decrypting and keeping the search index.
339345

340346

347+
### Reload scripts
348+
349+
Related to [issue #14](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/issues/14)
350+
351+
You can set `reload_scripts:` in your `mkdocs.yml` with list of script source, to reload and execute some js lib after decryption process.
352+
353+
```yaml
354+
plugins:
355+
reload_scripts:
356+
- "./js/example.js"
357+
```
358+
359+
This feature use the following JQuery function to remove, add and reload javascripts.
360+
361+
```javascript
362+
var reload_js = function(src) {
363+
$('script[src="' + src + '"]').remove();
364+
$('<script>').attr('src', src).appendTo('head');
365+
};
366+
```
367+
368+
341369
# Contributing
342370

343371
From reporting a bug to submitting a pull request: every contribution is appreciated and welcome.

encryptcontent/decrypt-form.tpl.html

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ <h1>{{ summary }}</h1>
4444
return false;
4545
}
4646
};
47-
{% if remember_password %}
47+
{% if remember_password -%}
4848
var setCookie = function(name,value,days,path) {
4949
/* Set local cookie to store password */
5050
var expires = "";
@@ -57,13 +57,13 @@ <h1>{{ summary }}</h1>
5757
if (path) {
5858
current_path = "; path=" + path
5959
}
60-
{% if disable_cookie_protection %}
60+
{% if disable_cookie_protection -%}
6161
encryptcontent = name + "=" + encodeURIComponent(value || "") + expires + current_path;
62-
{% else %}
62+
{%- else -%}
6363
encryptcontent = name + "=" + encodeURIComponent(value || "") + expires + current_path;
6464
// add security flag on cookie
6565
encryptcontent = encryptcontent + "; SameSite=Strict; Secure";
66-
{% endif %}
66+
{%- endif %}
6767
return encryptcontent;
6868
};
6969
var getCookie = function(name) {
@@ -77,19 +77,24 @@ <h1>{{ summary }}</h1>
7777
}
7878
return null;
7979
};
80-
{% endif %}
80+
{%- endif %}
81+
// Reload scripts src after decryption process
82+
var reload_js = function(src) {
83+
$('script[src="' + src + '"]').remove();
84+
$('<script>').attr('src', src).appendTo('head');
85+
};
8186
var init_decryptor = function() {
8287
var password_input = document.getElementById('mkdocs-content-password'),
8388
encrypted_content = document.getElementById('mkdocs-encrypted-content'),
8489
decrypted_content = document.getElementById('mkdocs-decrypted-content'),
85-
{% if password_button %}
90+
{% if password_button -%}
8691
decrypt_button = document.getElementById("mkdocs-decrypt-button"),
87-
{% endif %}
92+
{%- endif %}
8893
decrypt_form = document.getElementById('mkdocs-decrypt-form');
8994
// Adjust password field width to placeholder length
9095
let input = document.getElementById("mkdocs-content-password");
9196
input.setAttribute('size', input.getAttribute('placeholder').length);
92-
{% if experimental %}
97+
{% if experimental -%}
9398
// Decrypt sessionStorage search index with password if exist
9499
var decrypt_search = function(path_location) {
95100
sessionIndex = sessionStorage.getItem('encryptcontent-index');
@@ -115,8 +120,8 @@ <h1>{{ summary }}</h1>
115120
searchWorker.postMessage({init: true, sessionIndex: sessionIndex});
116121
}
117122
}
118-
{% endif %}
119-
{% if encrypted_something %}
123+
{%- endif %}
124+
{% if encrypted_something -%}
120125
var encrypted_something = {{ encrypted_something }}
121126
// Decrypt all others elements
122127
var decrypt_somethings = function() {
@@ -149,7 +154,7 @@ <h1>{{ summary }}</h1>
149154
}
150155
}
151156
}
152-
{% endif %}
157+
{%- endif %}
153158
// Decrypt content
154159
var decrypt_action = function() {
155160
// grab the ciphertext bundle
@@ -163,14 +168,18 @@ <h1>{{ summary }}</h1>
163168
decrypted_content.innerHTML = content;
164169
encrypted_content.parentNode.removeChild(encrypted_content);
165170
// any post processing on the decrypted content should be done here
166-
{% if arithmatex %}
171+
{% if arithmatex -%}
167172
MathJax.typesetPromise()
168-
{% endif %}
169-
{% if hljs %}
173+
{%- endif %}
174+
{% if hljs -%}
170175
document.getElementById("mkdocs-decrypted-content").querySelectorAll('pre code').forEach((block) => {
171176
hljs.highlightBlock(block);
172177
});
173-
{% endif %}
178+
{%- endif %}
179+
{% if reload_scripts | length > 0 -%}
180+
let reload_scripts = {{ reload_scripts }}
181+
for (i = 0; i < reload_scripts.length; i++) { reload_js(reload_scripts[i]); };
182+
{%- endif %}
174183
} else {
175184
// create HTML element for the inform message
176185
var decrypt_msg = document.createElement('p');
@@ -187,64 +196,64 @@ <h1>{{ summary }}</h1>
187196
password_input.focus();
188197
}
189198
}
190-
{% if remember_password %}
199+
{% if remember_password -%}
191200
/* If remember_password is set, try to use cookie to decrypt content when page is loaded */
192201
var password_cookie = getCookie('encryptcontent')
193202
if (password_cookie) {
194203
password_input.value = password_cookie
195204
decrypt_action();
196-
{% if experimental %}
205+
{% if experimental -%}
197206
decrypt_search(window.location.pathname.substring(1));
198-
{% endif %}
199-
{% if encrypted_something %}
207+
{%- endif %}
208+
{% if encrypted_something -%}
200209
decrypt_somethings();
201-
{% endif %}
210+
{%- endif %}
202211
}
203-
{% endif %}
204-
{% if password_button %}
212+
{%- endif %}
213+
{% if password_button -%}
205214
/* If password_button is set, try decrypt content when button is press */
206215
if (decrypt_button) {
207216
decrypt_button.onclick = function(event) {
208217
event.preventDefault();
209218
decrypt_action();
210-
{% if experimental %}
219+
{% if experimental -%}
211220
decrypt_search(window.location.pathname.substring(1));
212-
{% endif %}
213-
{% if encrypted_something %}
221+
{%- endif %}
222+
{% if encrypted_something -%}
214223
decrypt_somethings();
215-
{% endif %}
224+
{%- endif %}
216225
};
217226
}
218-
{% endif %}
227+
{%- endif %}
219228
/* Default, try decrypt content when password is send */
220229
password_input.addEventListener('keydown', function(event) {
221230
if (event.key === "Enter") {
222231
if (event.ctrlKey) {
223-
{% if remember_password %}
232+
{% if remember_password -%}
224233
// set password on cookie with default path=/ (Overwrite specific cookie)
225234
// this cookie can by use on all page of your site
226235
document.cookie = setCookie("encryptcontent", password_input.value, 1)
227-
{% endif %}
228-
{% if experimental %}
236+
{%- endif %}
237+
{% if experimental -%}
229238
// Decrypt all possible values on search index
230239
decrypt_search("/");
231-
{% endif %}
240+
{%- endif %}
232241
} else {
233-
{% if remember_password %}
242+
{% if remember_password -%}
234243
// set password on cookie with specific path=document.location.pathname
235244
// this cookie can only be use on this specific page of your site
236245
document.cookie = setCookie("encryptcontent", password_input.value, 1, document.location.pathname)
237-
{% endif %}
238-
{% if experimental %}
246+
{%- endif %}
247+
{% if experimental -%}
239248
// Only decrypt current page on search index
240249
decrypt_search(window.location.pathname.substring(1));
241-
{% endif %}
250+
{%- endif %}
242251
}
243252
event.preventDefault();
244253
decrypt_action();
245-
{% if encrypted_something %}
254+
{% if encrypted_something -%}
246255
decrypt_somethings();
247-
{% endif %}
256+
{%- endif %}
248257
}
249258
});
250259
};

encryptcontent/plugin.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
string_types = str
1818

1919
JS_LIBRARIES = [
20-
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/core.js',
21-
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/enc-base64.js',
22-
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/cipher-core.js',
23-
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/pad-nopadding.js',
24-
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/md5.js',
25-
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/aes.js'
20+
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/core.js',
21+
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/enc-base64.js',
22+
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/cipher-core.js',
23+
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/pad-nopadding.js',
24+
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/md5.js',
25+
'//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/aes.js'
2626
]
2727

2828
PLUGIN_DIR = os.path.dirname(os.path.realpath(__file__))
@@ -64,6 +64,7 @@ class encryptContentPlugin(BasePlugin):
6464
('password_button', config_options.Type(bool, default=False)),
6565
('encrypted_something', config_options.Type(dict, default={})),
6666
('search_index', config_options.Choice(('clear', 'dynamically', 'encrypted'), default='encrypted')),
67+
('reload_scripts', config_options.Type(list, default=[])),
6768
('experimental', config_options.Type(bool, default=False)),
6869
)
6970

@@ -111,6 +112,7 @@ def __encrypt_content__(self, content):
111112
'remember_password': self.config['remember_password'],
112113
'disable_cookie_protection': self.config['disable_cookie_protection'],
113114
'encrypted_something': self.config['encrypted_something'],
115+
'reload_scripts': self.config['reload_scripts'],
114116
'experimental': self.config['experimental'],
115117
})
116118
return decrypt_form

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def read(fname):
1111

1212
setup(
1313
name='mkdocs-encryptcontent-plugin',
14-
version='1.1.0.1',
14+
version='1.2.0.1',
1515
author='CoinK0in',
1616
author_email='[email protected]',
1717
description='A MkDocs plugin that encrypt/decrypt markdown content with AES',

0 commit comments

Comments
 (0)