Browse Source

base64 encode demo permalinks to avoid broken content for github postings

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
d02fc8a32b
  1. 18
      demo/assets/index.js
  2. 1
      demo/index.html
  3. 3
      demo/index.jade

18
demo/assets/index.js

@ -60,9 +60,9 @@
try { try {
if (source) { if (source) {
// serialize state - source and options // serialize state - source and options
permalink.href = '#md=' + encodeURIComponent(JSON.stringify({ permalink.href = '#md64=' + window.btoa(JSON.stringify({
defaults: _.omit(defaults, 'highlight'), source: source,
source: source defaults: _.omit(defaults, 'highlight')
})); }));
} else { } else {
permalink.href = ''; permalink.href = '';
@ -80,9 +80,17 @@
}); });
// Restore content if opened by permalink // Restore content if opened by permalink
if (location.hash && location.hash.toString().slice(0,4) === '#md=') { if (location.hash && /^(#md=|#md64=)/.test(location.hash)) {
try { try {
var cfg = JSON.parse(decodeURIComponent(location.hash.slice(4))); var cfg;
if (/^#md64=/.test(location.hash)) {
cfg = JSON.parse(window.atob(location.hash.slice(6)));
} else {
// Legacy mode for old links. Those become broken in github posts,
// so we switched to base64 encoding.
cfg = JSON.parse(decodeURIComponent(location.hash.slice(4)));
}
if (_.isString(cfg.source)) { if (_.isString(cfg.source)) {
$('.source').val(cfg.source); $('.source').val(cfg.source);

1
demo/index.html

@ -5,6 +5,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Base64/0.3.0/base64.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.2/highlight.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.2/highlight.min.js"></script>

3
demo/index.jade

@ -5,8 +5,9 @@ html
meta(charset='UTF-8') meta(charset='UTF-8')
script(src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js') script(src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js')
script(src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js') script(src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js')
//- atob/btoa polyfill
script(src='https://cdnjs.cloudflare.com/ajax/libs/Base64/0.3.0/base64.js')
script(src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js') script(src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js')
link(rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css') link(rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css')

Loading…
Cancel
Save