Browse Source

Use mdurl for correct permalink encoding

pull/104/head
Vitaly Puzrin 10 years ago
parent
commit
6ff6be7b70
  1. 58
      support/demo_template/index.js

58
support/demo_template/index.js

@ -3,6 +3,8 @@
/*eslint-env browser*/ /*eslint-env browser*/
/*global $, _*/ /*global $, _*/
var mdurl = require('mdurl');
var mdHtml, mdSrc, permalink, scrollMap; var mdHtml, mdSrc, permalink, scrollMap;
var defaults = { var defaults = {
@ -139,10 +141,10 @@ function updateResult() {
try { try {
if (source) { if (source) {
// serialize state - source and options // serialize state - source and options
permalink.href = '#md64=' + window.btoa(JSON.stringify({ permalink.href = '#md3=' + mdurl.encode(JSON.stringify({
source: source, source: source,
defaults: _.omit(defaults, 'highlight') defaults: _.omit(defaults, 'highlight')
})); }), mdurl.encode.componentChars, false);
} else { } else {
permalink.href = ''; permalink.href = '';
} }
@ -274,35 +276,36 @@ var syncSrcScroll = _.debounce(function () {
}, 100, 'linear'); }, 100, 'linear');
}, 50, { maxWait: 50 }); }, 50, { maxWait: 50 });
//////////////////////////////////////////////////////////////////////////////
// Init on page load
//
$(function() {
// highlight snippet
if (window.hljs) {
$('pre.code-sample code').each(function(i, block) {
window.hljs.highlightBlock(block);
});
}
// Restore content if opened by permalink function loadPermalink() {
if (location.hash && /^(#md=|#md64=)/.test(location.hash)) {
if (!location.hash) { return; }
var cfg, opts;
try { try {
var cfg;
if (/^#md64=/.test(location.hash)) { if (/^#md3=/.test(location.hash)) {
cfg = JSON.parse(mdurl.decode(location.hash.slice(5), mdurl.decode.componentChars));
} else if (/^#md64=/.test(location.hash)) {
cfg = JSON.parse(window.atob(location.hash.slice(6))); cfg = JSON.parse(window.atob(location.hash.slice(6)));
} else {
// Legacy mode for old links. Those become broken in github posts, } else if (/^#md=/.test(location.hash)) {
// so we switched to base64 encoding.
cfg = JSON.parse(decodeURIComponent(location.hash.slice(4))); cfg = JSON.parse(decodeURIComponent(location.hash.slice(4)));
} else {
return;
} }
if (_.isString(cfg.source)) { if (_.isString(cfg.source)) {
$('.source').val(cfg.source); $('.source').val(cfg.source);
} }
} catch (__) {
return;
}
var opts = _.isObject(cfg.defaults) ? cfg.defaults : {}; opts = _.isObject(cfg.defaults) ? cfg.defaults : {};
// copy config to defaults, but only if key exists // copy config to defaults, but only if key exists
// and value has the same type // and value has the same type
@ -325,9 +328,22 @@ $(function() {
if ([ 'html', 'src', 'debug' ].indexOf(defaults._view) === -1) { if ([ 'html', 'src', 'debug' ].indexOf(defaults._view) === -1) {
defaults._view = 'html'; defaults._view = 'html';
} }
} catch (__) {} }
//////////////////////////////////////////////////////////////////////////////
// Init on page load
//
$(function() {
// highlight snippet
if (window.hljs) {
$('pre.code-sample code').each(function(i, block) {
window.hljs.highlightBlock(block);
});
} }
loadPermalink();
// Activate tooltips // Activate tooltips
$('._tip').tooltip({ container: 'body' }); $('._tip').tooltip({ container: 'body' });

Loading…
Cancel
Save