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

refactor: fully use standard coding style and lint check

上级 c4efb05b
/* global hexo */
'use strict'
'use strict';
hexo.extend.helper.register('hexo_env', function(type) {
return this.env[type];
});
hexo.extend.helper.register('hexo_env', function (type) {
return this.env[type]
})
hexo.extend.helper.register('stun_env', function (type) {
var env = require('../package.json');
return env[type];
});
var env = require('../package.json')
return env[type]
})
/* global hexo */
'use strict'
'use strict';
hexo.extend.filter.register('after_post_render', function (data) {
var theme = hexo.theme.config;
if (
!theme.external_link ||
!theme.external_link.icon ||
!theme.external_link.icon.enable
) {
return;
}
hexo.extend.filter.register(
'after_post_render',
function (data) {
var theme = hexo.theme.config
if (
!theme.external_link ||
!theme.external_link.icon ||
!theme.external_link.icon.enable
) {
return
}
var url = require('url');
var config = this.config;
var siteHost = url.parse(config.url).hostname || config.url;
// Match 'a' tags that don't contain html children.
var regPureATag = /<a([^>]*)href="([^"]*)"([^>]*)>([^<]*)<\/a>/gim;
var config = this.config
var url = new URL(config.url)
var siteHost = url.hostname || config.url
// Match 'a' tags that don't contain html children.
var regPureATag = /<a([^>]*)href="([^"]*)"([^>]*)>([^<]*)<\/a>/gim
data.content = data.content.replace(
regPureATag,
function (match, attrBegin, href, attrEnd, html) {
data.content = data.content.replace(regPureATag, function (
match,
attrBegin,
href,
attrEnd,
html
) {
// Exit if the href attribute doesn't exists.
if (!href) {
return match;
return match
}
var link = ''
try {
link = new URL(href)
} catch (e) {
// Invalid url, e.g. Anchor link.
return match
}
// Exit if the url has same host with `config.url`, which means isn't an external link.
var link = url.parse(href);
if (!link.protocol || link.hostname === siteHost) {
return match;
};
return match
}
return (
'<span class="exturl">' +
`<a class="exturl__link" ${attrBegin} href="${href}" ${attrEnd}>${html}</a>` +
`<span class="exturl__icon"><i class="${theme.external_link.icon.name}"></i></span>` +
`<a class="exturl__link" ${attrBegin} href="${href}" ${attrEnd}>${html}</a>` +
`<span class="exturl__icon"><i class="${theme.external_link.icon.name}"></i></span>` +
'</span>'
);
}
);
}, 0);
)
})
},
0
)
/* global hexo */
'use strict';
hexo.extend.filter.register('after_post_render', function (data) {
data.content = data.content.replace(
// Match 'img' tags width the src attribute.
/<img([^>]*)src="([^"]*)"([^>]*)>/gim,
function (match, attrBegin, src, attrEnd) {
// Exit if the src doesn't exists.
if (!src) {
return match;
}
var search = src.split('?')[1];
// Exit if the src doesn't include query parameter.
if (!search) {
return match;
}
var params = search.split('&').filter(i => i !== '');
// Exit if the query parameter is empty.
if (!params.length) {
return match;
}
var size = '';
var show = '';
var style = '';
params.forEach(param => {
if (param.includes('size')) {
size = param.split('=')[1] || '';
'use strict'
hexo.extend.filter.register(
'after_post_render',
function (data) {
data.content = data.content.replace(
// Match 'img' tags width the src attribute.
/<img([^>]*)src="([^"]*)"([^>]*)>/gim,
function (match, attrBegin, src, attrEnd) {
// Exit if the src doesn't exists.
if (!src) {
return match
}
if (param.includes('show')) {
show = param.split('=')[1] || '';
var search = src.split('?')[1]
// Exit if the src doesn't include query parameter.
if (!search) {
return match
}
});
if (size !== '') {
// The sign between width and height.
var MULTIPLY_SIGN = 'x';
var w = size.split(MULTIPLY_SIGN)[0];
var h = size.split(MULTIPLY_SIGN)[1];
var params = search.split('&').filter(i => i !== '')
// Exit if the query parameter is empty.
if (!params.length) {
return match
}
if (w) {
style += `width: ${w}px;`;
var size = ''
var show = ''
var style = ''
params.forEach(param => {
if (param.includes('size')) {
size = param.split('=')[1] || ''
}
if (param.includes('show')) {
show = param.split('=')[1] || ''
}
})
if (size !== '') {
// The sign between width and height.
var MULTIPLY_SIGN = 'x'
var w = size.split(MULTIPLY_SIGN)[0]
var h = size.split(MULTIPLY_SIGN)[1]
if (w) {
style += `width: ${w}px;`
}
if (h) {
style += `height: ${h}px;`
}
}
if (h) {
style += `height: ${h}px;`;
if (show === 'inline') {
style += 'display: inline-block;'
}
}
if (show === 'inline') {
style += 'display: inline-block;';
}
return `<img ${attrBegin} src="${src}" style="${style}" ${attrEnd}>`;
}
);
}, 0);
return `<img ${attrBegin} src="${src}" style="${style}" ${attrEnd}>`
}
)
},
0
)
/* global hexo */
'use strict'
'use strict';
hexo.extend.filter.register('after_post_render', function (data) {
var theme = hexo.theme.config;
if (!theme.lazyload || !theme.lazyload.enable) {
return;
}
hexo.extend.filter.register(
'after_post_render',
function (data) {
var theme = hexo.theme.config
if (!theme.lazyload || !theme.lazyload.enable) {
return
}
data.content = data.content.replace(
// Match 'img' tags width the src attribute.
/<img([^>]*)src="([^"]*)"([^>]*)>/gim,
function (match, attrBegin, src, attrEnd) {
// Exit if the src doesn't exists.
if (!src) {
return match;
}
data.content = data.content.replace(
// Match 'img' tags width the src attribute.
/<img([^>]*)src="([^"]*)"([^>]*)>/gim,
function (match, attrBegin, src, attrEnd) {
// Exit if the src doesn't exists.
if (!src) {
return match
}
// Smallest 1 * 1 pixel transparent gif
var loadingBlock = 'data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=';
var loadingGIF = `/${theme.images}/loading.svg`;
var phClassName = theme.lazyload.placeholder;
var placeholder = '';
// Smallest 1 * 1 pixel transparent gif
var loadingBlock =
'data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs='
var loadingGIF = `/${theme.images}/loading.svg`
var phClassName = theme.lazyload.placeholder
var placeholder = ''
if (phClassName === 'gif') {
placeholder = loadingGIF;
} else if (phClassName === 'block') {
placeholder = loadingBlock;
}
if (phClassName === 'gif') {
placeholder = loadingGIF
} else if (phClassName === 'block') {
placeholder = loadingBlock
}
var className = `lazyload lazyload-${phClassName}`;
return `
var className = `lazyload lazyload-${phClassName}`
return `
<img ${attrBegin} class="${className}"
src="${placeholder}" data-src="${src}" ${attrEnd}>
`;
}
);
}, 1);
`
}
)
},
1
)
/* global hexo */
'use strict'
'use strict';
hexo.extend.filter.register(
'after_post_render',
function (data) {
var theme = hexo.theme.config
var tagName = 'h[1-6]'
var attrId = 'id="([^"]*)"'
var tagNotEnd = '([^>]*)'
var regAttrId = tagNotEnd + attrId + tagNotEnd
// Match the innermost 'h1~6' tags width the id attribute.
var regHTagInnermost = new RegExp(
`<(${tagName})${regAttrId}>((?:(?!<\\/?${tagName}${regAttrId}>)(?:\\s|\\S))*?)</${tagName}>`,
'gim'
)
hexo.extend.filter.register('after_post_render', function (data) {
var theme = hexo.theme.config;
var tagName = `h[1-6]`;
var attrId = `id="([^"]*)"`;
var tagNotEnd = `([^>]*)`;
var regAttrId = tagNotEnd + attrId + tagNotEnd;
// Match the innermost 'h1~6' tags width the id attribute.
var regHTagInnermost = new RegExp(
`<(${tagName})${regAttrId}>((?:(?!<\\/?${tagName}${regAttrId}>)(?:\\s|\\S))*?)<\/${tagName}>`,
'gim'
);
data.content = data.content.replace(
regHTagInnermost,
function (match, tName, attrBegin, id, attrEnd, html) {
data.content = data.content.replace(regHTagInnermost, function (
match,
tName,
attrBegin,
id,
attrEnd,
html
) {
if (!id) {
return match;
return match
}
var filterHtml = (html.replace(/<[^>]+>/gim, '') || '').trim();
var filterHtml = (html.replace(/<[^>]+>/gim, '') || '').trim()
return `
<${tName} id="${id}" ${attrBegin} ${attrEnd}>
<a href="#${id}" class="heading-link"><i class="${
theme.icon && theme.icon.post_heading
}"></i></a>${
filterHtml
}</${tName}>
`;
}
);
}, 0);
<a href="#${id}" class="heading-link"><i class="${theme.icon &&
theme.icon.post_heading}"></i></a>${filterHtml}</${tName}>
`
})
},
0
)
/* global hexo */
'use strict';
'use strict'
hexo.extend.filter.register('after_generate', function () {
var theme = hexo.theme.config;
var theme = hexo.theme.config
if (!theme.shake_file) {
return;
return
}
if (!(theme.sidebar && theme.sidebar.enable)) {
hexo.route.remove('js/sidebar.js');
hexo.route.remove('js/sidebar.js')
}
});
})
/* global hexo */
'use strict'
'use strict';
hexo.extend.filter.register(
'after_post_render',
function (data) {
data.content = data.content.replace(
// Match the innermost 'table' tag.
/(<table[^>]*>(?:(?!<\/?table>)(?:\s|\S))*?<\/table>)/gim,
function (match, table) {
if (!table) {
return match
}
hexo.extend.filter.register('after_post_render', function (data) {
data.content = data.content.replace(
// Match the innermost 'table' tag.
/(<table[^>]*>(?:(?!<\/?table>)(?:\s|\S))*?<\/table>)/gim,
function (match, table) {
if (!table) {
return match;
return `<div class="table-container">${table}</div>`
}
return `<div class="table-container">${table}</div>`;
}
);
}, 0);
)
},
0
)
/* global hexo */
'use strict';
'use strict'
hexo.on('generateBefore', function () {
var rootConfig = hexo.config;
var rootConfig = hexo.config
if (hexo.locals.get) {
var data = hexo.locals.get('data');
var data = hexo.locals.get('data')
if (data && data.stun) {
hexo.theme.config = data.stun;
hexo.theme.config = data.stun
}
}
hexo.theme.config.rootConfig = rootConfig;
});
hexo.theme.config.rootConfig = rootConfig
})
/* global hexo */
'use strict'
'use strict';
var pathFn = require('path')
var fs = require('hexo-fs')
var pathFn = require('path');
var fs = require('hexo-fs');
function friends (args) {
var path = pathFn.join(hexo.source_dir, args[0])
function friends(args) {
var path = pathFn.join(hexo.source_dir, args[0]);
fs.exists(path).then(function(exist) {
fs.exists(path).then(function (exist) {
if (!exist) {
hexo.log.error('Include file not found!');
return;
hexo.log.error('Include file not found!')
}
});
})
return fs.readFile(path).then(function(data) {
return fs.readFile(path).then(function (data) {
if (!data) {
hexo.log.warn('Include file empty.');
return;
hexo.log.warn('Include file empty.')
return
}
var imgClassName = 'friends-plugin__item-avatar ';
var theme = hexo.theme.config;
var imgClassName = 'friends-plugin__item-avatar '
var theme = hexo.theme.config
if (theme.lazyload && theme.lazyload.enable) {
imgClassName += `lazyload lazyload-${theme.lazyload.placeholder}`;
imgClassName += `lazyload lazyload-${theme.lazyload.placeholder}`
}
var friends = JSON.parse(data);
var renderHtml = '<div class="friends-plugin">';
var friends = JSON.parse(data)
var renderHtml = '<div class="friends-plugin">'
friends.forEach(f => {
renderHtml +=
`<a class="friends-plugin__item" href="${f.url}">` +
`<img class="${imgClassName}" src="${f.avatar}" data-zoom="none">` +
'<div class="friends-plugin__item-info">' +
`<p class="friends-plugin__item-info__name" title="${f.name}">${f.name}</p>` +
`<p class="friends-plugin__item-info__intro" title="${f.introduction}">${f.introduction}</p>` +
'</div>' +
'</a>';
});
renderHtml += '</div>';
return renderHtml;
});
`<img class="${imgClassName}" src="${f.avatar}" data-zoom="none">` +
'<div class="friends-plugin__item-info">' +
`<p class="friends-plugin__item-info__name" title="${f.name}">${f.name}</p>` +
`<p class="friends-plugin__item-info__intro" title="${f.introduction}">${f.introduction}</p>` +
'</div>' +
'</a>'
})
renderHtml += '</div>'
return renderHtml
})
}
hexo.extend.tag.register('friends', friends, { ends: false, async: true });
hexo.extend.tag.register('friends', friends, { ends: false, async: true })
/* global hexo */
'use strict'
'use strict';
function note(args, content) {
var theme = hexo.theme.config;
var icon = theme.icon && theme.icon.notetag_default;
var iconType = 'default';
var isIcon = true;
function note (args, content) {
var theme = hexo.theme.config
var icon = theme.icon && theme.icon.notetag_default
var iconType = 'default'
var isIcon = true
if (args.includes('no-icon')) {
isIcon = false;
isIcon = false
}
if (isIcon && theme.icon) {
var tagTypes = ['default', 'success', 'info', 'warning', 'danger'];
var tagTypes = ['default', 'success', 'info', 'warning', 'danger']
tagTypes.forEach(type => {
if (args.includes(type)) {
icon = theme.icon[`notetag_${type}`];
iconType = type;
icon = theme.icon[`notetag_${type}`]
iconType = type
}
});
})
}
var className = args.join(' ');
var className = args.join(' ')
return `
<div class="note-plugin ${className}">
${
......@@ -36,7 +34,7 @@ function note(args, content) {
.split('\n')
.join('')}
</div>
`;
`
}
hexo.extend.tag.register('note', note, { ends: true });
hexo.extend.tag.register('note', note, { ends: true })
/* global hexo */
'use strict'
'use strict';
var pathFn = require('path')
var fs = require('hexo-fs')
var pathFn = require('path');
var fs = require('hexo-fs');
function table (args) {
var path = pathFn.join(hexo.source_dir, args[0])
var headers = args[1].split(',')
function table(args) {
var path = pathFn.join(hexo.source_dir, args[0]);
var headers = args[1].split(',');
fs.exists(path).then(function(exist) {
fs.exists(path).then(function (exist) {
if (!exist) {
hexo.log.error('Include file not found!');
return;
hexo.log.error('Include file not found!')
}
});
})
return fs.readFile(path).then(function(data) {
return fs.readFile(path).then(function (data) {
if (!data) {
hexo.log.warn('Include file empty.');
return;
hexo.log.warn('Include file empty.')
return
}
var tableData = JSON.parse(data);
var result = '<table class="table-plugin"><thead><tr>';
var tableData = JSON.parse(data)
var result = '<table class="table-plugin"><thead><tr>'
headers.forEach(header => (result += `<th>${header}</th>`));
result += '</tr></thead><tbody>';
headers.forEach(header => (result += `<th>${header}</th>`))
result += '</tr></thead><tbody>'
tableData.forEach(item => {
result += '<tr style="text-align: center;">';
result += '<tr style="text-align: center;">'
for (var key in item) {
if (item.hasOwnProperty(key)) {
var value = item[key];
result += `<td>${value}</td>`;
if (Object.prototype.hasOwnProperty.call(item, key)) {
var value = item[key]
result += `<td>${value}</td>`
}
}
result += '</tr>';
});
result += '</tbody></table>';
result += '</tr>'
})
result += '</tbody></table>'
return result;
});
return result
})
}
hexo.extend.tag.register('table', table, { ends: false, async: true });
hexo.extend.tag.register('table', table, { ends: false, async: true })
$(document).ready(function () {
var $menuBtn = $('.header-nav-menubtn');
var $menu = $('.header-nav-menu');
var $menuItem = $('.header-nav-menu-item');
var $submenu = $('.header-nav-submenu');
var isMobile = $menuBtn.is(':visible');
var $menuBtn = $('.header-nav-menubtn')
var $menu = $('.header-nav-menu')
var $menuItem = $('.header-nav-menu-item')
var $submenu = $('.header-nav-submenu')
var isMobile = $menuBtn.is(':visible')
var isMenuShow = false;
var isSubmenuShow = false;
var isMenuShow = false
var isSubmenuShow = false
function resetMenuHeight () {
$menuItem.velocity(
......@@ -15,102 +15,104 @@ $(document).ready(function () {
},
{
complete: function () {
$submenu.css({ display: 'none', opacity: 0 });
$submenu.css({ display: 'none', opacity: 0 })
}
}
);
)
}
$(window).on(
'resize',
Stun.utils.throttle(function () {
isMobile = $menuBtn.is(':visible');
isMobile = $menuBtn.is(':visible')
if (isMobile) {
$submenu.removeClass('hide--force');
$submenu.removeClass('hide--force')
if (isSubmenuShow) {
resetMenuHeight();
isSubmenuShow = false;
resetMenuHeight()
isSubmenuShow = false
}
} else {
$submenu.css({ display: 'none', opacity: 0 });
$submenu.css({ display: 'none', opacity: 0 })
}
}, 200)
);
)
var isNightModeFocus = true;
var $nightMode = $('.mode');
var isNightModeFocus = true
var $nightMode = $('.mode')
$(document).on('click', function () {
if ($menu.is(':visible')) {
if (isMobile && isSubmenuShow) {
resetMenuHeight();
isSubmenuShow = false;
resetMenuHeight()
isSubmenuShow = false
}
$menu.css({ display: 'none' });
isMenuShow = false;
$menu.css({ display: 'none' })
isMenuShow = false
}
if (isNightModeFocus) {
$nightMode.removeClass('mode--focus');
isNightModeFocus = false;
$nightMode.removeClass('mode--focus')
isNightModeFocus = false
}
});
})
Stun.utils.pjaxReloadHeader = function () {
$menuBtn = $('.header-nav-menubtn');
$menu = $('.header-nav-menu');
$menuItem = $('.header-nav-menu-item');
$submenu = $('.header-nav-submenu');
isMobile = $menuBtn.is(':visible');
$menuBtn = $('.header-nav-menubtn')
$menu = $('.header-nav-menu')
$menuItem = $('.header-nav-menu-item')
$submenu = $('.header-nav-submenu')
isMobile = $menuBtn.is(':visible')
isMenuShow = false;
isSubmenuShow = false;
isMenuShow = false
isSubmenuShow = false
function getNightMode () {
var nightMode = false;
var nightMode = false
try {
if (parseInt(Stun.utils.Cookies().get(NIGHT_MODE_COOKIES_KEY))) {
nightMode = true;
nightMode = true
}
} catch (err) {}
return nightMode;
} catch (err) {
/* empty */
}
return nightMode
}
if (CONFIG.nightMode && CONFIG.nightMode.enable) {
var isNightMode = false;
var NIGHT_MODE_COOKIES_KEY = 'night_mode';
$nightMode = $('.mode');
isNightModeFocus = true;
var isNightMode = false
var NIGHT_MODE_COOKIES_KEY = 'night_mode'
$nightMode = $('.mode')
isNightModeFocus = true
if (getNightMode()) {
$nightMode.addClass('mode--checked');
$nightMode.addClass('mode--focus');
$('html').addClass('nightmode');
isNightMode = true;
$nightMode.addClass('mode--checked')
$nightMode.addClass('mode--focus')
$('html').addClass('nightmode')
isNightMode = true
} else {
isNightMode = false;
isNightMode = false
}
$('.mode').on('click', function (e) {
e.stopPropagation();
isNightMode = !isNightMode;
isNightModeFocus = true;
Stun.utils.Cookies().set(NIGHT_MODE_COOKIES_KEY, isNightMode ? 1 : 0);
$nightMode.toggleClass('mode--checked');
$nightMode.addClass('mode--focus');
$('html').toggleClass('nightmode');
});
e.stopPropagation()
isNightMode = !isNightMode
isNightModeFocus = true
Stun.utils.Cookies().set(NIGHT_MODE_COOKIES_KEY, isNightMode ? 1 : 0)
$nightMode.toggleClass('mode--checked')
$nightMode.addClass('mode--focus')
$('html').toggleClass('nightmode')
})
}
$menuBtn.on('click', function (e) {
e.stopPropagation();
e.stopPropagation()
if (isMobile && isMenuShow && isSubmenuShow) {
resetMenuHeight();
isSubmenuShow = false;
resetMenuHeight()
isSubmenuShow = false
}
if (!isMenuShow) {
isMenuShow = true;
isMenuShow = true
} else {
isMenuShow = false;
isMenuShow = false
}
$menu.velocity('stop').velocity(
{
......@@ -120,91 +122,91 @@ $(document).ready(function () {
duration: isMenuShow ? 200 : 0,
display: isMenuShow ? 'block' : 'none'
}
);
});
)
})
// Whether to allow events to bubble in the menu.
var isBubbleInMenu = false;
var isBubbleInMenu = false
$('.header-nav-submenu-item').on('click', function () {
isBubbleInMenu = true;
});
isBubbleInMenu = true
})
$menuItem.on('click', function (e) {
if (!isMobile) {
return;
return
}
var $submenu = $(this).find('.header-nav-submenu');
var $submenu = $(this).find('.header-nav-submenu')
if (!$submenu.length) {
return;
return
}
if (!isBubbleInMenu) {
e.stopPropagation();
e.stopPropagation()
} else {
isBubbleInMenu = false;
isBubbleInMenu = false
}
var menuItemHeight = $menuItem.outerHeight();
var menuItemHeight = $menuItem.outerHeight()
var submenuHeight =
menuItemHeight + Math.floor($submenu.outerHeight()) * $submenu.length;
var menuShowHeight = 0;
menuItemHeight + Math.floor($submenu.outerHeight()) * $submenu.length
var menuShowHeight = 0
if ($(this).outerHeight() > menuItemHeight) {
isSubmenuShow = false;
menuShowHeight = menuItemHeight;
isSubmenuShow = false
menuShowHeight = menuItemHeight
} else {
isSubmenuShow = true;
menuShowHeight = submenuHeight;
isSubmenuShow = true
menuShowHeight = submenuHeight
}
$submenu.css({ display: 'block', opacity: 1 });
$submenu.css({ display: 'block', opacity: 1 })
// Accordion effect.
$(this)
.velocity('stop')
.velocity({ height: menuShowHeight }, { duration: 300 })
.siblings()
.velocity({ height: menuItemHeight }, { duration: 300 });
});
.velocity({ height: menuItemHeight }, { duration: 300 })
})
$menuItem.on('mouseenter', function () {
var $submenu = $(this).find('.header-nav-submenu');
var $submenu = $(this).find('.header-nav-submenu')
if (!$submenu.length) {
return;
return
}
if (!$submenu.is(':visible')) {
if (isMobile) {
$submenu.css({ display: 'block', opacity: 1 });
$submenu.css({ display: 'block', opacity: 1 })
} else {
$submenu.removeClass('hide--force');
$submenu.removeClass('hide--force')
$submenu
.velocity('stop')
.velocity('transition.slideUpIn', { duration: 200 });
.velocity('transition.slideUpIn', { duration: 200 })
}
}
});
})
$menuItem.on('mouseleave', function () {
var $submenu = $(this).find('.header-nav-submenu');
var $submenu = $(this).find('.header-nav-submenu')
if (!$submenu.length) {
return;
return
}
if (!isMobile) {
$submenu.addClass('hide--force');
isSubmenuShow = false;
$submenu.addClass('hide--force')
isSubmenuShow = false
}
});
};
})
}
Stun.utils.pjaxReloadScrollIcon = function () {
if (CONFIG.header && CONFIG.header.scrollDownIcon) {
$('.header-banner-arrow').on('click', function (e) {
e.stopPropagation();
e.stopPropagation()
$('#container').velocity('scroll', {
offset: $('#header').outerHeight()
});
});
})
})
}
};
}
// Initializaiton
Stun.utils.pjaxReloadHeader();
Stun.utils.pjaxReloadScrollIcon();
});
Stun.utils.pjaxReloadHeader()
Stun.utils.pjaxReloadScrollIcon()
})
$(document).ready(function () {
var isHeaderEnable = CONFIG.header && CONFIG.header.enable;
var isShowHeaderOnPost = isHeaderEnable && CONFIG.header.showOnPost;
var isHeaderEnable = CONFIG.header && CONFIG.header.enable
var isShowHeaderOnPost = isHeaderEnable && CONFIG.header.showOnPost
// The previous distance from the page to the top.
var prevScrollTop = 0;
var isNavFix = false;
var isAnimation = true;
var prevScrollTop = 0
var isNavFix = false
var isAnimation = true
function headerNavScroll () {
var isPostPage = !!$('#is-post').length;
var isNoHeader = !isHeaderEnable || (isPostPage && !isShowHeaderOnPost);
var $headerNav = $('.header-nav');
var scrollTop = Math.floor($(window).scrollTop());
var delta = Math.floor(scrollTop - prevScrollTop);
var isPostPage = !!$('#is-post').length
var isNoHeader = !isHeaderEnable || (isPostPage && !isShowHeaderOnPost)
var $headerNav = $('.header-nav')
var scrollTop = Math.floor($(window).scrollTop())
var delta = Math.floor(scrollTop - prevScrollTop)
if (scrollTop === 0) {
if (isNoHeader) {
setTimeout(function () {
$headerNav.addClass('slider--clear');
isAnimation = false;
}, 200);
$headerNav.addClass('slider--clear')
isAnimation = false
}, 200)
}
$headerNav.removeClass('header-nav--sticky');
$headerNav.removeClass('slider--up');
$headerNav.addClass('slider--down');
$headerNav.removeClass('header-nav--sticky')
$headerNav.removeClass('slider--up')
$headerNav.addClass('slider--down')
} else {
if (isNoHeader && scrollTop < $headerNav.height()) {
return false;
return false
}
var MIN_SCROLL_TO_CHANGE_NAV = 5;
var MIN_SCROLL_TO_CHANGE_NAV = 5
// Make the state of nav bar not change due to tiny scrolling.
if (Math.abs(delta) > MIN_SCROLL_TO_CHANGE_NAV) {
if (isNoHeader) {
if (!isAnimation) {
isAnimation = true;
isAnimation = true
} else {
$headerNav.removeClass('slider--clear');
$headerNav.removeClass('slider--clear')
}
}
if (!isNavFix) {
isNavFix = true;
isNavFix = true
} else {
$headerNav.addClass('header-nav--sticky');
$headerNav.addClass('header-nav--sticky')
}
if (delta > 0) {
$headerNav.removeClass('slider--down');
$headerNav.addClass('slider--up');
$headerNav.removeClass('slider--down')
$headerNav.addClass('slider--up')
} else {
$headerNav.removeClass('slider--up');
$headerNav.addClass('slider--down');
$headerNav.removeClass('slider--up')
$headerNav.addClass('slider--down')
}
} else {
$headerNav.addClass('header-nav--sticky');
$headerNav.addClass('header-nav--sticky')
}
}
prevScrollTop = scrollTop;
prevScrollTop = scrollTop
}
var isBack2topEnable = CONFIG.back2top && CONFIG.back2top.enable;
var isBack2topShow = false;
var isBack2topEnable = CONFIG.back2top && CONFIG.back2top.enable
var isBack2topShow = false
// Back the page to top.
function back2top () {
var $back2top = $('#back2top');
var scrollTop = $(window).scrollTop();
var $back2top = $('#back2top')
var scrollTop = $(window).scrollTop()
if (scrollTop !== 0) {
if (!isBack2topShow) {
$back2top.addClass('back2top--show');
$back2top.removeClass('back2top--hide');
isBack2topShow = true;
$back2top.addClass('back2top--show')
$back2top.removeClass('back2top--hide')
isBack2topShow = true
}
} else {
$back2top.addClass('back2top--hide');
$back2top.removeClass('back2top--show');
isBack2topShow = false;
$back2top.addClass('back2top--hide')
$back2top.removeClass('back2top--show')
isBack2topShow = false
}
}
if (isBack2topEnable) {
// Initializaiton
back2top();
back2top()
$('#back2top').on('click', function () {
$('body')
.velocity('stop')
.velocity('scroll');
});
.velocity('scroll')
})
}
// Initializaiton
headerNavScroll();
headerNavScroll()
$(window).on(
'scroll',
Stun.utils.throttle(function () {
headerNavScroll();
headerNavScroll()
if (isBack2topEnable) {
back2top();
back2top()
}
}, 100)
);
});
)
})
$(document).ready(function () {
var tocDepth = (CONFIG.sidebar && CONFIG.sidebar.tocMaxDepth) || 4;
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);
.slice(0, -1)
function initTocDisplay () {
if ($('.post-body').find(HEADING_SELECTOR)[0]) {
return;
return
}
$('.sidebar-nav').addClass('hide');
$('.sidebar-toc').addClass('hide');
$('.sidebar-ov').removeClass('hide');
$('.sidebar-nav').addClass('hide')
$('.sidebar-toc').addClass('hide')
$('.sidebar-ov').removeClass('hide')
}
// The heading that reached the top currently.
var currHeading = null;
var currHeading = null
// The heading that reached the top last time.
var lastHeading = null;
var isRemovedTocClass = false;
var lastHeading = null
var isRemovedTocClass = false
// Automatically expand items in the article directory
// based on the scrolling of heading in the article.
function autoSpreadToc () {
var $postBody = $('.post-body');
var $allTocItem = $('.sidebar-toc li');
var $headings = $postBody.find(HEADING_SELECTOR);
var $firsetChild = $headings.first();
var $postBody = $('.post-body')
var $allTocItem = $('.sidebar-toc li')
var $headings = $postBody.find(HEADING_SELECTOR)
var $firsetChild = $headings.first()
$headings.each(function () {
var headingTop = this.getBoundingClientRect().top;
var headingTop = this.getBoundingClientRect().top
// The minimum distance from the top of the browser
// when heading is marked as active in toc.
var MIN_HEIGHT_TO_TOP = 5;
var MIN_HEIGHT_TO_TOP = 5
if (headingTop <= MIN_HEIGHT_TO_TOP) {
currHeading = this.getAttribute('id');
currHeading = this.getAttribute('id')
}
});
})
// All heading are not to the top.
if (
......@@ -47,38 +47,38 @@ $(document).ready(function () {
$firsetChild.offset().top - $(window).scrollTop() > 0
) {
if (!isRemovedTocClass) {
$allTocItem.removeClass('active current');
isRemovedTocClass = true;
$allTocItem.removeClass('active current')
isRemovedTocClass = true
}
return;
return
}
if (currHeading !== lastHeading) {
var $targetLink = $('.sidebar-toc a[href="#' + currHeading + '"]');
$allTocItem.removeClass('active current');
$targetLink.parents('li').addClass('active');
$targetLink.parent().addClass('current');
lastHeading = currHeading;
isRemovedTocClass = false;
var $targetLink = $('.sidebar-toc a[href="#' + currHeading + '"]')
$allTocItem.removeClass('active current')
$targetLink.parents('li').addClass('active')
$targetLink.parent().addClass('current')
lastHeading = currHeading
isRemovedTocClass = false
}
}
// Whether toc needs scrolling.
var isTocScroll = false;
var isTocScroll = false
// Scroll the post toc to the middle.
function scrollTocToMiddle () {
var $tocWrapHeight = $('.sidebar-toc').height();
var $tocHeight = $('.sidebar-toc > div').height();
var $tocWrapHeight = $('.sidebar-toc').height()
var $tocHeight = $('.sidebar-toc > div').height()
if ($tocHeight <= $tocWrapHeight) {
return;
return
}
var $tocWrap = $('.sidebar-toc');
var $currTocItem = $('.sidebar-toc .current a');
var $tocWrap = $('.sidebar-toc')
var $currTocItem = $('.sidebar-toc .current a')
if ($currTocItem[0] && $tocWrap[0]) {
var tocTop = $currTocItem.offset().top - $tocWrap.offset().top;
isTocScroll = tocTop > $tocWrapHeight || tocTop < 0;
var tocTop = $currTocItem.offset().top - $tocWrap.offset().top
isTocScroll = tocTop > $tocWrapHeight || tocTop < 0
}
if (isTocScroll) {
......@@ -87,27 +87,27 @@ $(document).ready(function () {
offset: -$tocWrapHeight / 2,
duration: 500,
easing: 'easeOutQuart'
});
})
}
}
// Distance from sidebar to top.
var sidebarToTop = 0;
var sidebarToTop = 0
if (CONFIG.sidebar && CONFIG.sidebar.offsetTop) {
sidebarToTop = parseInt(CONFIG.sidebar.offsetTop);
sidebarToTop = parseInt(CONFIG.sidebar.offsetTop)
}
// Sticky the sidebar when it arrived the top.
function sidebarSticky () {
var $sidebar = $('#sidebar');
var $sidebar = $('#sidebar')
var targetY = document
.getElementById('content-wrap')
.getBoundingClientRect().top;
.getBoundingClientRect().top
if (targetY < sidebarToTop) {
$sidebar.addClass('sidebar--sticky');
$sidebar.addClass('sidebar--sticky')
} else {
$sidebar.removeClass('sidebar--sticky');
$sidebar.removeClass('sidebar--sticky')
}
}
......@@ -115,108 +115,108 @@ $(document).ready(function () {
function readProgress () {
// Not on post page.
if ($('#is-post').length === 0) {
return;
return
}
var $post = $('.content');
var postTop = $post.offset().top;
var postEndTop = 0;
var postEndHeight = 0;
var postReadingHeight = 0;
var isEnablePostEnd = false;
var percent = 0;
var $post = $('.content')
var postTop = $post.offset().top
var postEndTop = 0
var postEndHeight = 0
var postReadingHeight = 0
var isEnablePostEnd = false
var percent = 0
if (CONFIG.postWidget && CONFIG.postWidget.endText) {
isEnablePostEnd = true;
isEnablePostEnd = true
}
if (isEnablePostEnd) {
postEndTop = $('.post-ending').offset().top;
postEndHeight = $('.post-ending').outerHeight();
postReadingHeight = postEndTop - postTop + postEndHeight;
postEndTop = $('.post-ending').offset().top
postEndHeight = $('.post-ending').outerHeight()
postReadingHeight = postEndTop - postTop + postEndHeight
} else {
postEndTop = $('.post-footer').offset().top;
postReadingHeight = postEndTop - postTop;
postEndTop = $('.post-footer').offset().top
postReadingHeight = postEndTop - postTop
}
var windowHeight = $(window).height();
var postScrollTop = 0;
var windowHeight = $(window).height()
var postScrollTop = 0
if ($post.length !== 0) {
postScrollTop =
parseInt($post[0].getBoundingClientRect().top * -1) + windowHeight;
parseInt($post[0].getBoundingClientRect().top * -1) + windowHeight
}
var percentNum = Number($('.sidebar-reading-info__num').text());
postReadingHeight = parseInt(Math.abs(postReadingHeight));
percent = parseInt((postScrollTop / postReadingHeight) * 100);
percent = percent > 100 ? 100 : percent < 0 ? 0 : percent;
var percentNum = Number($('.sidebar-reading-info__num').text())
postReadingHeight = parseInt(Math.abs(postReadingHeight))
percent = parseInt((postScrollTop / postReadingHeight) * 100)
percent = percent > 100 ? 100 : percent < 0 ? 0 : percent
// Has reached the maximum or minimum
if (
(percent === 0 && percentNum === 0) ||
(percent === 100 && percentNum === 100)
) {
return;
return
}
$('.sidebar-reading-info__num').text(percent);
$('.sidebar-reading-info__num').text(percent)
$('.sidebar-reading-line').css(
'transform',
'translateX(' + (percent - 100) + '%)'
);
)
}
// Initial run
autoSpreadToc();
sidebarSticky();
scrollTocToMiddle();
readProgress();
autoSpreadToc()
sidebarSticky()
scrollTocToMiddle()
readProgress()
$(window).on('scroll', function () {
sidebarSticky();
});
sidebarSticky()
})
$(window).on(
'scroll',
Stun.utils.throttle(function () {
autoSpreadToc();
scrollTocToMiddle();
readProgress();
autoSpreadToc()
scrollTocToMiddle()
readProgress()
}, 150)
);
)
Stun.utils.pjaxReloadSidebar = function () {
var $navToc = $('.sidebar-nav-toc');
var $navOv = $('.sidebar-nav-ov');
var $tocWrap = $('.sidebar-toc');
var $overview = $('.sidebar-ov');
var $navToc = $('.sidebar-nav-toc')
var $navOv = $('.sidebar-nav-ov')
var $tocWrap = $('.sidebar-toc')
var $overview = $('.sidebar-ov')
$navToc.on('click', function (e) {
e.stopPropagation();
e.stopPropagation()
if ($(this).hasClass('current')) {
return;
return
}
$navToc.addClass('current');
$navOv.removeClass('current');
$tocWrap.css('display', 'block');
$tocWrap.velocity('stop').velocity('fadeIn');
$overview.css('display', 'none');
$overview.velocity('stop').velocity('fadeOut');
});
$navToc.addClass('current')
$navOv.removeClass('current')
$tocWrap.css('display', 'block')
$tocWrap.velocity('stop').velocity('fadeIn')
$overview.css('display', 'none')
$overview.velocity('stop').velocity('fadeOut')
})
$navOv.on('click', function (e) {
e.stopPropagation();
e.stopPropagation()
if ($(this).hasClass('current')) {
return;
return
}
$navOv.addClass('current');
$navToc.removeClass('current');
$tocWrap.css('display', 'none');
$tocWrap.velocity('stop').velocity('fadeOut');
$overview.css('display', 'block');
$overview.velocity('stop').velocity('fadeIn');
});
initTocDisplay();
};
$navOv.addClass('current')
$navToc.removeClass('current')
$tocWrap.css('display', 'none')
$tocWrap.velocity('stop').velocity('fadeOut')
$overview.css('display', 'block')
$overview.velocity('stop').velocity('fadeIn')
})
initTocDisplay()
}
// Initialization
Stun.utils.pjaxReloadSidebar();
});
Stun.utils.pjaxReloadSidebar()
})
$(document).ready(function () {
Stun.utils.showThemeInConsole();
Stun.utils.showThemeInConsole()
if (CONFIG.shortcuts && CONFIG.shortcuts.switchPost) {
Stun.utils.registerSwitchPost();
Stun.utils.registerSwitchPost()
}
// Not reload this, because it's changeless.
if (CONFIG.externalLink) {
Stun.utils.addIconToExternalLink('#footer');
Stun.utils.addIconToExternalLink('#footer')
}
Stun.utils.pjaxReloadBoot = function () {
if (CONFIG.codeblock) {
var codeStyle = CONFIG.codeblock.style;
var codeStyle = CONFIG.codeblock.style
if (codeStyle === 'default') {
this.addCodeHeader();
this.addCopyButton();
this.addCodeHeader()
this.addCopyButton()
} else if (codeStyle === 'carbon') {
this.addCodeHeader('carbon');
this.addCopyButton('carbon');
this.addCodeHeader('carbon')
this.addCopyButton('carbon')
} else if (codeStyle === 'simple') {
this.addCopyButton('simple');
this.addCopyButton('simple')
}
this.registerCopyEvent();
this.registerCopyEvent()
}
if (CONFIG.reward) {
this.registerShowReward();
this.registerShowReward()
}
if (CONFIG.lazyload) {
this.lazyLoadImage();
this.lazyLoadImage()
}
if (CONFIG.galleryWaterfall) {
this.showImageToWaterfall();
this.showImageToWaterfall()
}
if (CONFIG.externalLink) {
var CONTAINER = '.archive, .post-title';
this.addIconToExternalLink(CONTAINER);
var CONTAINER = '.archive, .post-title'
this.addIconToExternalLink(CONTAINER)
}
if (CONFIG.fancybox) {
this.wrapImageWithFancyBox();
this.wrapImageWithFancyBox()
} else if (CONFIG.zoomImage) {
this.registerZoomImage();
this.registerZoomImage()
}
};
}
// Initializaiton
Stun.utils.pjaxReloadBoot();
});
Stun.utils.pjaxReloadBoot()
})
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册