From bd3961d350c904704272f37d42e09b2827888a20 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Wed, 20 Nov 2024 17:56:01 +0800 Subject: [PATCH] Use optional chaining --- scripts/events/lib/config.js | 4 ++-- scripts/helpers/font.js | 2 +- scripts/helpers/next-config.js | 4 ++-- source/js/next-boot.js | 2 +- source/js/utils.js | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/scripts/events/lib/config.js b/scripts/events/lib/config.js index ad35061..8b91116 100644 --- a/scripts/events/lib/config.js +++ b/scripts/events/lib/config.js @@ -15,11 +15,11 @@ module.exports = hexo => { hexo.log.warn(`Since ${args[0]} is turned on, the ${args[1]} is disabled to avoid potential hazards.`); }; - if (cache && cache.enable && language_switcher) { + if (cache?.enable && language_switcher) { warning('language_switcher', 'caching'); cache.enable = false; } - if (cache && cache.enable && hexo.config.relative_link) { + if (cache?.enable && hexo.config.relative_link) { warning('caching', '`relative_link` option in Hexo `_config.yml`'); hexo.config.relative_link = false; } diff --git a/scripts/helpers/font.js b/scripts/helpers/font.js index 517e8e4..6cf37dc 100644 --- a/scripts/helpers/font.js +++ b/scripts/helpers/font.js @@ -12,7 +12,7 @@ module.exports = function() { // Get a font list from config let fontFamilies = []; ['global', 'title', 'headings', 'posts', 'codes'].forEach(item => { - if (config[item] && config[item].family && config[item].external) { + if (config[item]?.family && config[item].external) { fontFamilies = fontFamilies.concat(config[item].family.split(',')); } }); diff --git a/scripts/helpers/next-config.js b/scripts/helpers/next-config.js index 4a57c48..c987d35 100644 --- a/scripts/helpers/next-config.js +++ b/scripts/helpers/next-config.js @@ -36,7 +36,7 @@ hexo.extend.helper.register('next_config', function() { hits : __('search.hits', '${hits}') } }; - if (config.algolia && theme.algolia_search && theme.algolia_search.enable) { + if (config.algolia && theme.algolia_search?.enable) { exportConfig.algolia = { appID : config.algolia.applicationID || config.algolia.appId, apiKey : config.algolia.apiKey, @@ -44,7 +44,7 @@ hexo.extend.helper.register('next_config', function() { hits : theme.algolia_search.hits }; } - if (config.search && theme.local_search && theme.local_search.enable) { + if (config.search && theme.local_search?.enable) { exportConfig.path = url_for(config.search.path); exportConfig.localsearch = theme.local_search; } diff --git a/source/js/next-boot.js b/source/js/next-boot.js index e732f84..8cbc090 100644 --- a/source/js/next-boot.js +++ b/source/js/next-boot.js @@ -27,7 +27,7 @@ NexT.boot.registerEvents = function() { const tHash = location.hash; if (tHash !== '' && !tHash.match(/%\S{2}/)) { const target = document.querySelector(`.tabs ul.nav-tabs li a[href="${tHash}"]`); - target && target.click(); + target?.click(); } }); diff --git a/source/js/utils.js b/source/js/utils.js index 78c255b..5803d7d 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -169,7 +169,7 @@ NexT.utils = { updateActiveNav() { if (!Array.isArray(this.sections)) return; let index = this.sections.findIndex(element => { - return element && element.getBoundingClientRect().top > 10; + return element?.getBoundingClientRect().top > 10; }); if (index === -1) { index = this.sections.length - 1; @@ -198,7 +198,7 @@ NexT.utils = { this.updateActiveNav(); }, { passive: true }); - backToTop && backToTop.addEventListener('click', () => { + backToTop?.addEventListener('click', () => { window.anime({ targets : document.scrollingElement, duration : 500,