UNPKG

hexo-theme-amazing

Version:

A simple, delicate, and modern theme for Hexo

172 lines (150 loc) 7.35 kB
/* eslint-disable node/no-unsupported-features/node-builtins */ function loadMainJs($, moment, ClipboardJS, config) { $('.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('<p class="has-text-centered is-size-6 caption">' + this.alt + '</p>'); } } }); if (typeof $.fn.lightGallery === 'function') { $('.article').lightGallery({selector: '.gallery-item'}); } if (typeof $.fn.justifiedGallery === 'function') { if ($('.justified-gallery > p > .gallery-item').length) { $('.justified-gallery > p > .gallery-item').unwrap(); } // 调整gallery图片渲染尺寸 $('.justified-gallery').justifiedGallery({rowHeight: 230, margins: 4}); } 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('justify-content-start'); } else { $('.navbar-main .navbar-menu').removeClass('justify-content-start'); } } adjustNavbar(); $(window).resize(adjustNavbar); function toggleFold(codeBlock, isFolded) { const $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'); } function createFoldButton(fold) { return '<span class="fold">' + (fold === 'unfolded' ? '<i class="fas fa-angle-down"></i>' : '<i class="fas fa-angle-right"></i>') + '</span>'; } $('figure.highlight table').wrap('<div class="highlight-body">'); if (typeof config !== 'undefined' && typeof config.article !== 'undefined' && typeof config.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]); } }); const clipboard = config.article.highlight.clipboard; const fold = config.article.highlight.fold.trim(); $('figure.highlight').each(function () { if ($(this).find('figcaption').length) { $(this).find('figcaption').addClass('level is-mobile'); $(this).find('figcaption').append('<div class="level-left">'); $(this).find('figcaption').append('<div class="level-right">'); $(this).find('figcaption div.level-left').append($(this).find('figcaption').find('span')); $(this).find('figcaption div.level-right').append($(this).find('figcaption').find('a')); } else { if (clipboard || fold) { $(this).prepend('<figcaption class="level is-mobile"><div class="level-left"></div><div class="level-right"></div></figcaption>'); } } }); if (typeof ClipboardJS !== 'undefined' && clipboard) { $('figure.highlight').each(function () { const id = 'code-' + Date.now() + (Math.random() * 1000 | 0); const button = '<a href="javascript:;" class="copy" title="Copy" data-clipboard-target="#' + id + ' .code"><i class="fas fa-copy"></i></a>'; $(this).attr('id', id); $(this).find('figcaption div.level-right').append(button); }); new ClipboardJS('.highlight .copy'); // eslint-disable-line no-new } if (fold) { $('figure.highlight').each(function () { if ($(this).find('figcaption').find('span').length > 0) { const span = $(this).find('figcaption').find('span'); if (span[0].innerText.indexOf('>folded') > -1) { span[0].innerText = span[0].innerText.replace('>folded', ''); $(this).find('figcaption div.level-left').prepend(createFoldButton('folded')); toggleFold(this, true); return; } } $(this).find('figcaption div.level-left').prepend(createFoldButton(fold)); toggleFold(this, fold === 'folded'); }); $('figure.highlight figcaption .fold').click(function () { const $code = $(this).closest('figure.highlight'); toggleFold($code.eq(0), !$code.hasClass('folded')); }); } } const $toc = $('#toc'); if ($toc.length > 0) { $toc.addClass('column-left is-sticky'); const $mask = $('<div>'); $mask.attr('id', 'toc-mask'); $('body').append($mask); function toggleToc() { // eslint-disable-line no-inner-declarations $toc.toggleClass('is-active'); $mask.toggleClass('is-active'); } $toc.on('click', toggleToc); $mask.on('click', toggleToc); $('.navbar-main .catalogue').on('click', toggleToc); } } function loadMathJax() { //加载mathjax $.getScript("//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML", function () { MathJax.Hub.Config({ tex2jax: { inlineMath: [['$', '$'], ['\\(', '\\)']] } }); var math = document.getElementsByClassName("entry-content")[0]; MathJax.Hub.Queue(["Typeset", MathJax.Hub, math]); }); } $(document).ready(function () { loadMainJs(jQuery, window.moment, window.ClipboardJS, window.IcarusThemeSettings); /* 添加背景色 */ var navbar = $(".is-fixed-top"); var navbar1 = $(".justify-content-start"); if (navbar.offset().top > 12) { navbar.addClass("navbar-highlight"); navbar1.addClass("navbar-highlight"); } else { navbar.removeClass("navbar-highlight"); navbar1.removeClass("navbar-highlight"); } $(window).scroll(function () { if (navbar.offset().top > 12) { navbar.addClass("navbar-highlight"); navbar1.addClass("navbar-highlight"); } else { navbar.removeClass("navbar-highlight"); navbar1.removeClass("navbar-highlight"); } }); });