showdown/js/app.js

153 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-12-14 20:48:41 +08:00
var converter = new showdown.Converter({
metadata: true,
tables: true,
strikethrough: true,
ellipsis: true,
headerLevelStart: 3
});
/** ROUTING **/
var router = new Navigo('http://showdownjs.com/', true, '#!');
/** BLOG POSTS **/
var blogPosts = {};
2017-12-12 04:01:58 +08:00
$(document).ready(function() {
2017-12-14 20:48:41 +08:00
blogPosts = new BlogPosts();
2017-12-12 04:01:58 +08:00
2017-12-14 20:48:41 +08:00
function BlogPosts () {
var getList = this.getList = function() {
var dfd = jQuery.Deferred();
2018-10-17 09:33:10 +08:00
$.getJSON('blog/posts.json', function(data) {
for (var i = 0; i < data.length; ++i) {
data[i].metadata.summary = converter.makeHtml(data[i].metadata.summary);
}
dfd.resolve(data);
});
2017-12-14 20:48:41 +08:00
return dfd.promise();
};
this.getPostByCanonical = function(canonical) {
var dfd = jQuery.Deferred();
$.when(getList()).then(function(list) {
2018-10-17 09:33:10 +08:00
for (var i = 0; i < list.length; ++i) {
if (list[i].canonical === canonical) {
var promise = $.ajax({
url: list[i].url,
dataType: "text"
});
promise.done((function (index, listItem) {
return function (md) {
listItem.post = converter.makeHtml(md);
dfd.resolve(listItem);
};
})(i, list[i]));
return;
2017-12-14 20:48:41 +08:00
}
}
});
return dfd.promise();
};
2017-12-12 04:01:58 +08:00
}
2017-12-14 20:48:41 +08:00
$(document).ajaxStart(function(){
$.LoadingOverlay('show', {
image: '',
//color: 'rgba(166, 200, 157, 0.7)',
color: '#fff',
fontawesome: 'fa fa-spinner fa-spin'
});
});
$(document).ajaxStop(function(){
$.LoadingOverlay("hide");
});
function getLatestVersion() {
var dfd = jQuery.Deferred();
if (typeof Cookies.get('version') === 'undefined' || typeof Cookies.get('zipball') === 'undefined') {
$.getJSON('https://api.github.com/repos/showdownjs/showdown/releases/latest', function (data) {
var version = data.tag_name;
var zipball = data.zipball_url;
Cookies.set('version', version, { expires: 0.1 });
Cookies.set('zipball', zipball, { expires: 0.1 });
dfd.resolve({version: version, zipball: zipball});
});
2017-12-12 04:01:58 +08:00
} else {
2017-12-14 20:48:41 +08:00
dfd.resolve({
version: Cookies.get('version'),
zipball: Cookies.get('zipball')
});
2017-12-12 04:01:58 +08:00
}
2017-12-14 20:48:41 +08:00
return dfd.promise();
2017-12-12 04:01:58 +08:00
}
2018-10-17 09:33:10 +08:00
function loadPage(url, params) {
2017-12-14 20:48:41 +08:00
var $page = $('#page');
return $page.load(url, function () {
2018-10-17 09:33:10 +08:00
if (params && params.page === 'blog-article') {
$.when(blogPosts.getPostByCanonical(params.canonical)).then(function(post) {
var article = Mustache.render($('#blog-article-tpl').html(), post);
$('#blog-post-container').html(article);
$('pre code').each(function(i, block) {
hljs.highlightBlock(block);
hljs.lineNumbersBlock(block);
});
router.updatePageLinks();
});
}
2017-12-14 20:48:41 +08:00
$page.find('pre>code').each(function(i, block) {
hljs.highlightBlock(block);
hljs.lineNumbersBlock(block);
router.updatePageLinks();
});
2017-12-12 04:01:58 +08:00
});
}
2017-12-14 20:48:41 +08:00
function changeActiveLink(name) {
2017-12-12 04:01:58 +08:00
$('.internal-navigation').removeClass('active');
2017-12-14 20:48:41 +08:00
$('#navlink-' + name).addClass('active');
}
$.when(getLatestVersion()).then(
function(data) {
$('#lt-version-num').html('v' + data.version);
$('#lt-version-lnk').attr('href', data.zipball);
}
);
2017-12-12 04:01:58 +08:00
2017-12-14 20:48:41 +08:00
router
2018-10-17 09:33:10 +08:00
.on('/releases', function (params) {
2017-12-14 20:48:41 +08:00
changeActiveLink('releases');
2018-10-17 09:33:10 +08:00
return loadPage('html/releases.html', params);
2017-12-14 20:48:41 +08:00
})
2018-10-17 09:33:10 +08:00
.on('/documentation', function (params) {
2017-12-14 20:48:41 +08:00
changeActiveLink('documentation');
2018-10-17 09:33:10 +08:00
return loadPage('html/documentation.html', params);
2017-12-14 20:48:41 +08:00
})
.on('/blog/:canonical', function (params) {
2018-10-17 09:33:10 +08:00
params.page = 'blog-article';
2017-12-14 20:48:41 +08:00
changeActiveLink('blog');
return loadPage('html/blog-article.html', params);
})
2018-10-17 09:33:10 +08:00
.on('/blog', function (params) {
2017-12-14 20:48:41 +08:00
changeActiveLink('blog');
2018-10-17 09:33:10 +08:00
return loadPage('html/blog.html', params);
2017-12-14 20:48:41 +08:00
})
2018-10-17 09:33:10 +08:00
.on('*', function (params) {
2017-12-14 20:48:41 +08:00
changeActiveLink('home');
2018-10-17 09:33:10 +08:00
return loadPage('html/main.html', params);
2017-12-14 20:48:41 +08:00
})
.resolve();
2017-12-12 04:01:58 +08:00
2017-12-14 20:48:41 +08:00
$('.internal-navigation').click(function() {
2017-12-12 04:01:58 +08:00
// smooth scroll to page
$('html, body').animate({
scrollTop: $('#page').offset().top
}, 500);
2017-12-14 20:48:41 +08:00
})
2017-12-12 04:01:58 +08:00
});