提交 499ab9f2 编写于 作者: EvanOne(文一)'s avatar EvanOne(文一)

refactor: Make some error handing

上级 180fd3b9
......@@ -26,7 +26,7 @@ html(lang=config.language)
if page.comments
include ./_partials/widgets/comments.pug
if theme.sidebar.enable
if theme.sidebar && theme.sidebar.enable
aside#sidebar.sidebar
div.sidebar-inner
include ./_partials/sidebar/sidebar.pug
......@@ -39,7 +39,7 @@ html(lang=config.language)
include ./_partials/widgets/loading-bar.pug
if theme.back2top.enable
if theme.back2top && theme.back2top.enable
include ./_partials/widgets/back2top.pug
include ./_partials/search/index.pug
......
-
var algolia = "undefined";
var env = process.env;
if (theme.algolia_search.enable) {
if (theme.algolia_search && theme.algolia_search.enable) {
algolia = JSON.stringify({
appId: env.ALGOLIA_APP_ID || config.algolia.appId || config.algolia.applicationID,
apiKey: env.ALGOLIA_API_KEY || config.algolia.apiKey,
......@@ -23,25 +23,27 @@
}
var sidebar = "undefined";
if (theme.sidebar.enable) {
if (theme.sidebar && theme.sidebar.enable) {
sidebar = JSON.stringify({
offsetTop: theme.sidebar.offsetTop,
renderTocDepth: theme.toc.max_depth
tocMaxDepth: theme.toc.max_depth
});
}
var back2top = "undefined";
if (theme.back2top.enable) {
if (theme.back2top && theme.back2top.enable) {
back2top = JSON.stringify({
enable: theme.back2top.enable,
animation: theme.back2top.icon.animation
enable: theme.back2top.enable
});
}
var header = "undefined";
if (theme.header) {
var isScrollDownIcon =
(theme.header.scroll_down_icon && theme.header.scroll_down_icon.enable) || false;
header = JSON.stringify({
scrollDownIcon: theme.header.scroll_down_icon.enable,
scrollDownIcon: isScrollDownIcon,
});
}
......@@ -53,40 +55,60 @@
}
var gallery_waterfall = "undefined";
if (theme.gallery_waterfall.enable) {
if (theme.gallery_waterfall && theme.gallery_waterfall.enable) {
gallery_waterfall = JSON.stringify(theme.gallery_waterfall);
}
var lazyload = "undefined";
if (theme.lazyload.enable) {
if (theme.lazyload && theme.lazyload.enable) {
lazyload = JSON.stringify(theme.lazyload);
}
var zoom_image = "undefined";
if (theme.zoom_image.enable) {
if (theme.zoom_image && theme.zoom_image.enable) {
zoom_image = JSON.stringify(theme.zoom_image);
}
var pjax_config = "undefined";
if (theme.pjax.enable) {
if (theme.pjax && theme.pjax.enable) {
pjax_config = JSON.stringify({
scrollTo2screen: theme.pjax.scrollTo2screen
});
}
var external_link = "undefined";
if (theme.external_link.icon.enable) {
external_link = JSON.stringify({
icon: {
enable: theme.external_link.icon.enable,
name: theme.external_link.icon.name
if (theme.external_link) {
var iconConfig = {};
var iconEnable = false;
var iconName = '';
if (theme.external_link.icon) {
iconEnable = theme.external_link.icon.enable;
if (iconEnable) {
iconName = theme.external_link.icon.name;
}
iconConfig = { enable: iconEnable, name: iconName }
}
external_link = JSON.stringify({
icon: iconConfig
});
}
var shortcuts = JSON.stringify({
switch_post: theme.shortcuts.switch_post.enable
});
var shortcuts = "undefined";
if (theme.shortcuts) {
isSwitchPost = false;
if (theme.shortcuts.switch_post && theme.shortcuts.switch_post.enable) {
isSwitchPost = true;
}
shortcuts = JSON.stringify({
switch_post: isSwitchPost
});
}
var prompt = JSON.stringify({
copy_success: __("prompt.copy.success"),
......
......@@ -78,13 +78,13 @@ if theme.fancybox
- if (theme.cdn.fancybox_css) fancybox_css = theme.cdn.fancybox_css
link(rel="stylesheet" href=fancybox_css type="text/css")
if page.comments || theme.pjax.enable
if page.comments || (theme.pjax && theme.pjax.enable)
if theme.gitalk && theme.gitalk.enable
- var gitalk_css = "https://cdn.jsdelivr.net/npm/gitalk@latest/dist/gitalk.min.css"
- if (theme.cdn.gitalk_css) gitalk_css = theme.cdn.gitalk_css
link(rel="stylesheet" href=gitalk_css type="text/css")
if theme.algolia_search.enable
if theme.algolia_search && theme.algolia_search.enable
- var instantsearch_css = "https://cdn.jsdelivr.net/npm/instantsearch.js@2.1.1/dist/instantsearch.min.css"
- if (theme.cdn.instantsearch_css) instantsearch_css = theme.cdn.instantsearch_css
link(rel="stylesheet" href=instantsearch_css type="text/css")
......
......@@ -8,7 +8,7 @@ nav.header-nav
div.header-nav-menu
+menuItem(theme.menu)
if theme.algolia_search.enable || theme.local_search.enable
if (theme.algolia_search && theme.algolia_search.enable) || (theme.local_search && theme.local_search.enable)
div.header-nav-search
if !theme.menu_settings.text_only
i(class=`${fa_prefix} fa-search`)
......@@ -27,6 +27,6 @@ div.header-info(style=`${
div.header-info-title= config.title
div.header-info-subtitle= config.subtitle
if theme.header.scroll_down_icon && theme.header.scroll_down_icon.enable
if theme.header && theme.header.scroll_down_icon && theme.header.scroll_down_icon.enable
div.header-info-scrolldown
i.fa.fa-angle-down.header-info-scrolldown__icon
if theme.algolia_search.enable
if theme.algolia_search && theme.algolia_search.enable
include ./algolia.pug
else if theme.local_search.enable
else if theme.local_search && theme.local_search.enable
include ./localsearch.pug
if page.comments || theme.pjax.enable
if page.comments || (theme.pjax && theme.pjax.enable)
if theme.disqus && theme.disqus.enable
include ./disqus.pug
else if theme.valine && theme.valine.enable
......
......@@ -53,7 +53,7 @@ script.
document.addEventListener('pjax:send', function () {
$('.header-nav-menu').removeClass('show');
if (CONFIG.pjax.scrollTo2screen) {
if (CONFIG.pjax && CONFIG.pjax.scrollTo2screen) {
$('html').velocity('scroll', {
duration: 500,
offset: $('#header').height(),
......@@ -95,9 +95,9 @@ script.
$(this).parent().append($(this).remove());
});
Stun.utils.pjaxReloadBoot();
Stun.utils.pjaxReloadScroll();
Stun.utils.pjaxReloadHeader();
Stun.utils.pjaxReloadSidebar();
Stun.utils.pjaxReloadBoot && Stun.utils.pjaxReloadBoot();
Stun.utils.pjaxReloadScroll && Stun.utils.pjaxReloadScroll();
Stun.utils.pjaxReloadHeader && Stun.utils.pjaxReloadHeader();
Stun.utils.pjaxReloadSidebar && Stun.utils.pjaxReloadSidebar();
}, false);
}, false);
......@@ -41,7 +41,7 @@ script.
var algolia = CONFIG.algolia;
if (!(algolia.appId && algolia.apiKey && algolia.indexName)) {
if (!algolia && !(algolia.appId && algolia.apiKey && algolia.indexName)) {
return console.error('Algolia setting is invalid.');
}
......
if theme.algolia_search.enable
if theme.algolia_search && theme.algolia_search.enable
include ./algolia.pug
else if theme.local_search.enable
else if theme.local_search && theme.local_search.enable
include ./localsearch.pug
......@@ -4,7 +4,7 @@
hexo.extend.filter.register('after_post_render', function(data) {
var theme = hexo.theme.config;
if (!theme.external_link.icon.enable) return;
if (!(theme.external_link && theme.external_link.icon && theme.external_link.icon.enable)) return;
var url = require('url');
var cheerio;
......
......@@ -4,7 +4,7 @@
hexo.extend.filter.register('after_post_render', function(data) {
var theme = hexo.theme.config;
if (!theme.lazyload.enable) return;
if (!(theme.lazyload && theme.lazyload.enable)) return;
var cheerio;
......
......@@ -7,7 +7,7 @@ hexo.extend.filter.register('after_generate', () => {
if (!theme.shake_file) return;
if (!theme.sidebar.enable) {
if (!(theme.sidebar && theme.sidebar.enable)) {
hexo.route.remove('js/sidebar.js');
}
});
......@@ -29,7 +29,7 @@ $md-width = 992px - 0.02px
$lg-width = 1200px - 0.02px
// Width
$sidebar-width = convert(hexo-config('sidebar.width') || '280px')
$sidebar-width = convert(hexo-config('sidebar.width') || '280px')
$content-aside-gap = 20px
$content-width = $sm-width
$main-sidebar-gap = 30px
......
......@@ -167,7 +167,7 @@ $(document).ready(function () {
});
Stun.utils.pjaxReloadHeader = function () {
if (CONFIG.header.scrollDownIcon) {
if (CONFIG.header && CONFIG.header.scrollDownIcon) {
$('.header-info-scrolldown').on('click', function () {
$('#container').velocity('scroll', {
offset: $('#header').height()
......
......@@ -42,6 +42,9 @@ $(document).ready(function () {
prevScrollTop = scrollTop;
}
// Initializaiton
headerNavScroll();
function scrollHeadingToTop (anchor) {
$(anchor)
.velocity('stop')
......@@ -51,6 +54,7 @@ $(document).ready(function () {
});
}
var isBack2topEnable = CONFIG.back2top && CONFIG.back2top.enable;
var isBack2topShow = false;
// Back the page to top.
......@@ -71,19 +75,22 @@ $(document).ready(function () {
}
}
// Initialization
headerNavScroll();
back2top();
if (isBack2topEnable) {
// Initializaiton
back2top();
$('#back2top').on('click', function () {
$('body').velocity('stop').velocity('scroll');
});
}
$(window).on('DOMContentLoaded', back2top);
$(window).on('scroll', Stun.utils.throttle(function () {
headerNavScroll();
back2top();
}, 100));
$('#back2top').on('click', function () {
$('body').velocity('stop').velocity('scroll');
});
if (isBack2topEnable) {
back2top();
}
}, 100));
Stun.utils.pjaxReloadScroll = function () {
// Click the heading.
......
$(document).ready(function () {
var tocDepth = CONFIG.sidebar.renderTocDepth;
var tocDepth = (CONFIG.sidebar && CONFIG.sidebar.tocMaxDepth) || 4;
// Optimize selector by theme config.
var HEADING_SELECTOR = 'h1,h2,h3,h4,h5,h6,'.slice(0, tocDepth * 3).slice(0, -1);
......@@ -97,7 +97,11 @@ $(document).ready(function () {
}
// Distance from sidebar to top.
var SIDEBAR_STICKY_TOP = parseInt(CONFIG.sidebar.offsetTop);
var sidebarToTop = 0;
if (CONFIG.sidebar && CONFIG.sidebar.offsetTop) {
sidebarToTop = parseInt(CONFIG.sidebar.offsetTop);
}
// Sticky the sidebar when it arrived the top.
function sidebarSticky () {
......@@ -105,7 +109,7 @@ $(document).ready(function () {
var targetY =
document.getElementById('main').getBoundingClientRect().top;
if (targetY < SIDEBAR_STICKY_TOP) {
if (targetY < sidebarToTop) {
$sidebar.addClass('sticky');
} else {
$sidebar.removeClass('sticky');
......
$(document).ready(function () {
Stun.utils.showThemeInConsole();
CONFIG.shortcuts.switch_post && Stun.utils.registerHotkeyToSwitchPost();
if (CONFIG.shortcuts && CONFIG.shortcuts.switch_post) {
Stun.utils.registerHotkeyToSwitchPost();
}
// Not reload this, because it's changeless.
if (CONFIG.external_link) {
......@@ -16,9 +19,9 @@ $(document).ready(function () {
CONFIG.gallery_waterfall && this.showImageToWaterfall();
if (CONFIG.external_link) {
var WRAPPER = '.archive, .post-header-title';
var CONTAINER = '.archive, .post-header-title';
this.addIconToExternalLink(WRAPPER);
this.addIconToExternalLink(CONTAINER);
}
if (CONFIG.fancybox) {
......
......@@ -261,7 +261,7 @@ Stun.utils = Stun.$u = {
$('.stun-message').remove();
}
var faPrefix = CONFIG.fontawesome.prefix;
var faPrefix = (CONFIG.fontawesome && CONFIG.fontawesome.prefix) || 'fa';
var $alert = $(
'<div class="stun-message">' +
'<div class="stun-alert stun-alert-' + status + '">' +
......@@ -398,7 +398,7 @@ Stun.utils = Stun.$u = {
return;
}
var faPrefix = CONFIG.fontawesome.prefix;
var faPrefix = (CONFIG.fontawesome && CONFIG.fontawesome.prefix) || 'fa';
var $wrapper = $('<span class="external-link"></span>');
var $icon = $(
'<i class="' + faPrefix + ' fa-' +
......@@ -539,7 +539,7 @@ Stun.utils = Stun.$u = {
}
});
var faPrefix = CONFIG.fontawesome.prefix;
var faPrefix = (CONFIG.fontawesome && CONFIG.fontawesome.prefix) || 'fa';
var $copyIcon = $(
'<div class="copy-button" data-popover=' +
CONFIG.prompt.copy_button +
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册