hexo-theme-amane/source/js/main.js

155 lines
5.9 KiB
JavaScript

(function ($) {
$('.article img:not(".not-gallery-item")').each(function () {
// wrap images with link and add caption if possible
if ($(this).parent('a').length === 0) {
$(this).wrap('<a class="gallery-item" href="' + $(this).attr('src') + '"></a>');
if (this.alt) {
$(this).after('<div class="has-text-centered is-size-6 has-text-grey caption">' + this.alt + '</div>');
}
}
});
if (typeof (moment) === 'function') {
$('.article-meta time').each(function () {
$(this).text(moment($(this).attr('datetime')).fromNow());
});
}
$('.article > .content > table').each(function () {
if ($(this).width() > $(this).parent().width()) {
$(this).wrap('<div class="table-overflow"></div>');
}
});
function adjustNavbar() {
const navbarWidth = $('.navbar-main .navbar-start').outerWidth() + $('.navbar-main .navbar-end').outerWidth();
if ($(document).outerWidth() < navbarWidth) {
$('.navbar-main .navbar-menu').addClass('is-flex-start');
} else {
$('.navbar-main .navbar-menu').removeClass('is-flex-start');
}
}
adjustNavbar();
$(window).resize(adjustNavbar);
$('figure.highlight table').wrap('<div class="highlight-body">');
if (typeof (IcarusThemeSettings) !== 'undefined' &&
typeof (IcarusThemeSettings.article) !== 'undefined' &&
typeof (IcarusThemeSettings.article.highlight) !== 'undefined') {
$('figure.highlight').addClass('hljs');
$('figure.highlight .code .line span').each(function () {
const classes = $(this).attr('class').split(/\s+/);
if (classes.length === 1) {
$(this).addClass('hljs-' + classes[0]);
$(this).removeClass(classes[0]);
}
});
if (typeof (ClipboardJS) !== 'undefined' && IcarusThemeSettings.article.highlight.clipboard) {
$('figure.highlight').each(function () {
var id = 'code-' + Date.now() + (Math.random() * 1000 | 0);
var button = '<a href="javascript:;" class="copy" title="Copy" data-clipboard-target="#' + id + ' .code"><i class="fas fa-copy"></i></a>';
$(this).attr('id', id);
if ($(this).find('figcaption').length) {
$(this).find('figcaption').prepend(button);
} else {
$(this).prepend('<figcaption>' + button + '</figcaption>');
}
});
new ClipboardJS('.highlight .copy');
}
var fold = IcarusThemeSettings.article.highlight.fold;
if (fold.trim()) {
var button = '<span class="fold">' + (fold === 'unfolded' ? '<i class="fas fa-angle-down"></i>' : '<i class="fas fa-angle-right"></i>') + '</span>';
$('figure.highlight').each(function () {
if ($(this).find('figcaption').length) {
$(this).find('figcaption').prepend(button);
} else {
$(this).prepend('<figcaption>' + button + '</figcaption>');
}
});
function toggleFold(codeBlock, isFolded) {
var $toggle = $(codeBlock).find('.fold i');
!isFolded ? $(codeBlock).removeClass('folded') : $(codeBlock).addClass('folded');
!isFolded ? $toggle.removeClass('fa-angle-right') : $toggle.removeClass('fa-angle-down');
!isFolded ? $toggle.addClass('fa-angle-down') : $toggle.addClass('fa-angle-right');
}
$('figure.highlight').each(function () {
toggleFold(this, fold === 'folded');
});
$('figure.highlight figcaption .fold').click(function () {
var $code = $(this).closest('figure.highlight');
toggleFold($code.eq(0), !$code.hasClass('folded'));
});
}
}
var $toc = $('#toc');
if ($toc.length > 0) {
var $mask = $('<div>');
$mask.attr('id', 'toc-mask');
$('body').append($mask);
function toggleToc() {
$toc.toggleClass('is-active');
$mask.toggleClass('is-active');
}
$toc.on('click', toggleToc);
$mask.on('click', toggleToc);
$('.navbar-main .catalogue').on('click', toggleToc);
}
// hexo-util/lib/is_external_link.js
function isExternalLink(input, sitehost, exclude) {
try {
sitehost = new URL(sitehost).hostname;
} catch (e) { }
if (!sitehost) return false;
// handle relative url
const data = new URL(input, 'http://' + sitehost);
// handle mailto: javascript: vbscript: and so on
if (data.origin === 'null') return false;
const host = data.hostname;
if (exclude) {
exclude = Array.isArray(exclude) ? exclude : [exclude];
if (exclude && exclude.length) {
for (const i of exclude) {
if (host === i) return false;
}
}
}
if (host !== sitehost) return true;
return false;
}
if (typeof (IcarusThemeSettings) !== 'undefined' &&
typeof (IcarusThemeSettings.site.url) !== 'undefined' &&
typeof (IcarusThemeSettings.site.external_link) !== 'undefined' &&
IcarusThemeSettings.site.external_link.enable) {
$('.article .content a').filter(function (i, link) {
return link.href &&
!$(link).attr('href').startsWith('#') &&
link.classList.length === 0 &&
isExternalLink(link.href,
IcarusThemeSettings.site.url,
IcarusThemeSettings.site.external_link.exclude);
}).each(function (i, link) {
link.relList.add('noopener');
link.target = '_blank';
});
}
})(jQuery);