From da9c1dda7c8d95bbcdaf710c6be1bbb024732f37 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 13 Jun 2020 12:20:43 +0800 Subject: [PATCH] Code style update --- layout/_partials/head/head.njk | 2 +- layout/_third-party/quicklink.njk | 2 +- layout/_third-party/search/algolia-search.njk | 4 +-- scripts/events/lib/config.js | 28 ++++--------------- scripts/helpers/engine.js | 10 +++---- scripts/helpers/font.js | 14 ++++------ 6 files changed, 19 insertions(+), 41 deletions(-) diff --git a/layout/_partials/head/head.njk b/layout/_partials/head/head.njk index 23ac59b..03879ff 100644 --- a/layout/_partials/head/head.njk +++ b/layout/_partials/head/head.njk @@ -47,7 +47,7 @@ {%- if theme.fancybox %} - {%- set fancybox_css_uri = theme.vendors.fancybox_css or next_vendors('//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css') %} + {%- set fancybox_css_uri = theme.vendors.fancybox_css or '//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css' %} {%- endif %} diff --git a/layout/_third-party/quicklink.njk b/layout/_third-party/quicklink.njk index 4ff4e87..02b30f6 100644 --- a/layout/_third-party/quicklink.njk +++ b/layout/_third-party/quicklink.njk @@ -1,5 +1,5 @@ {%- if page.quicklink.enable %} - {%- set quicklink_uri = theme.vendors.quicklink or next_vendors('//cdn.jsdelivr.net/npm/quicklink@1/dist/quicklink.umd.js') %} + {%- set quicklink_uri = theme.vendors.quicklink or '//cdn.jsdelivr.net/npm/quicklink@1/dist/quicklink.umd.js' %} diff --git a/scripts/events/lib/config.js b/scripts/events/lib/config.js index 87b3b20..4b9806e 100644 --- a/scripts/events/lib/config.js +++ b/scripts/events/lib/config.js @@ -1,37 +1,19 @@ 'use strict'; -function isObject(item) { - return item && typeof item === 'object' && !Array.isArray(item); -} - -function merge(target, source) { - for (const key in source) { - if (isObject(target[key]) && isObject(source[key])) { - merge(target[key], source[key]); - } else { - target[key] = source[key]; - } - } - return target; -} +const merge = require('hexo-util').deepMerge || require('lodash/merge'); module.exports = hexo => { let data = hexo.locals.get('data'); /** * Merge configs from _data/next.yml into hexo.theme.config. - * If `override`, configs in next.yml will rewrite configs in hexo.theme.config. * If next.yml not exists, merge all `theme_config.*` into hexo.theme.config. */ if (data.next) { - if (data.next.override) { - hexo.theme.config = data.next; - } else { - merge(hexo.config, data.next); - merge(hexo.theme.config, data.next); - } - } else { - merge(hexo.theme.config, hexo.config.theme_config); + hexo.config = merge(hexo.config, data.next); + hexo.theme.config = merge(hexo.theme.config, data.next); + } else if (hexo.config.theme_config) { + hexo.theme.config = merge(hexo.theme.config, hexo.config.theme_config); } if (hexo.theme.config.cache && hexo.theme.config.cache.enable && hexo.config.relative_link) { diff --git a/scripts/helpers/engine.js b/scripts/helpers/engine.js index f441a1e..473a7a0 100644 --- a/scripts/helpers/engine.js +++ b/scripts/helpers/engine.js @@ -5,24 +5,24 @@ const crypto = require('crypto'); hexo.extend.helper.register('next_inject', function(point) { - return hexo.theme.config.injects[point] + return this.theme.injects[point] .map(item => this.partial(item.layout, item.locals, item.options)) .join(''); }); hexo.extend.helper.register('next_js', function(...urls) { - const { js } = hexo.theme.config; + const { js } = this.theme; return urls.map(url => this.js(`${js}/${url}`)).join(''); }); hexo.extend.helper.register('next_vendors', function(url) { if (url.startsWith('//')) return url; - const internal = hexo.theme.config.vendors._internal; + const internal = this.theme.vendors._internal; return this.url_for(`${internal}/${url}`); }); hexo.extend.helper.register('post_edit', function(src) { - const theme = hexo.theme.config; + const { theme } = this; if (!theme.post_edit.enable) return ''; return this.next_url(theme.post_edit.url + src, '', { class: 'post-edit-link', @@ -31,7 +31,7 @@ hexo.extend.helper.register('post_edit', function(src) { }); hexo.extend.helper.register('post_nav', function(post) { - const theme = hexo.theme.config; + const { theme } = this; if (theme.post_navigation === false || (!post.prev && !post.next)) return ''; const prev = theme.post_navigation === 'right' ? post.prev : post.next; const next = theme.post_navigation === 'right' ? post.next : post.prev; diff --git a/scripts/helpers/font.js b/scripts/helpers/font.js index d346468..1fd124d 100644 --- a/scripts/helpers/font.js +++ b/scripts/helpers/font.js @@ -2,13 +2,11 @@ 'use strict'; -hexo.extend.helper.register('next_font', () => { - const config = hexo.theme.config.font; +hexo.extend.helper.register('next_font', function() { + const config = this.theme.font; if (!config || !config.enable) return ''; - const fontDisplay = '&display=swap'; - const fontSubset = '&subset=latin,latin-ext'; const fontStyles = ':300,300italic,400,400italic,700,700italic'; const fontHost = config.host || '//fonts.googleapis.com'; @@ -18,12 +16,10 @@ hexo.extend.helper.register('next_font', () => { return config[item].family + fontStyles; } return ''; - }); + }).filter(item => item !== ''); - fontFamilies = fontFamilies.filter(item => item !== ''); - fontFamilies = [...new Set(fontFamilies)]; - fontFamilies = fontFamilies.join('|'); + fontFamilies = [...new Set(fontFamilies)].join('|'); // Merge extra parameters to the final processed font string - return fontFamilies ? `` : ''; + return fontFamilies ? `` : ''; });