From 9ea66544c64da9f1e4f141c44806f769f299c568 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Wed, 15 Jul 2020 11:55:42 +0800 Subject: [PATCH] New eslint rule `prefer-const` --- scripts/events/lib/config.js | 8 ++--- scripts/events/lib/highlight.js | 8 ++--- scripts/events/lib/injects.js | 8 ++--- scripts/filters/comment/changyan.js | 4 +-- scripts/filters/comment/default-config.js | 8 ++--- scripts/filters/comment/disqus.js | 4 +-- scripts/filters/comment/disqusjs.js | 2 +- scripts/filters/comment/gitalk.js | 2 +- scripts/filters/comment/livere.js | 2 +- scripts/filters/comment/valine.js | 4 +-- scripts/filters/default-injects.js | 2 +- scripts/filters/post.js | 2 +- scripts/helpers/engine.js | 2 +- scripts/helpers/next-config.js | 4 +-- scripts/helpers/next-url.js | 2 +- scripts/helpers/next-vendors.js | 4 +-- scripts/helpers/tagcloud.js | 2 +- scripts/tags/button.js | 2 +- scripts/tags/group-pictures.js | 2 +- scripts/tags/pdf.js | 2 +- source/js/algolia-search.js | 6 ++-- source/js/local-search.js | 36 +++++++++++------------ source/js/utils.js | 18 ++++++------ 23 files changed, 67 insertions(+), 67 deletions(-) diff --git a/scripts/events/lib/config.js b/scripts/events/lib/config.js index f47d512..d3f115c 100644 --- a/scripts/events/lib/config.js +++ b/scripts/events/lib/config.js @@ -8,7 +8,7 @@ try { } module.exports = hexo => { - let data = hexo.locals.get('data'); + const data = hexo.locals.get('data'); /** * Merge configs from _data/next.yml into hexo.theme.config. @@ -36,15 +36,15 @@ module.exports = hexo => { // Custom languages support. Introduced in NexT v6.3.0. if (data.languages) { - let { language } = hexo.config; - let { i18n } = hexo.theme; + const { language } = hexo.config; + const { i18n } = hexo.theme; const mergeLang = lang => { if (data.languages[lang]) i18n.set(lang, merge(i18n.get([lang]), data.languages[lang])); }; if (Array.isArray(language)) { - for (let lang of language) { + for (const lang of language) { mergeLang(lang); } } else { diff --git a/scripts/events/lib/highlight.js b/scripts/events/lib/highlight.js index 0bbfa1c..128dc1f 100644 --- a/scripts/events/lib/highlight.js +++ b/scripts/events/lib/highlight.js @@ -6,8 +6,8 @@ function resolve(package) { } function highlightTheme(name) { - let file = `${resolve('highlight.js')}/styles/${name}.css`; - let css = fs.readFileSync(file).toString(); + const file = `${resolve('highlight.js')}/styles/${name}.css`; + const css = fs.readFileSync(file).toString(); let rule = ''; let background = ''; let foreground = ''; @@ -34,8 +34,8 @@ function prismTheme(name) { } module.exports = hexo => { - let { config } = hexo; - let theme = hexo.theme.config; + const { config } = hexo; + const theme = hexo.theme.config; config.highlight.hljs = false; config.prismjs = config.prismjs || {}; theme.highlight = { diff --git a/scripts/events/lib/injects.js b/scripts/events/lib/injects.js index 0ee8f2a..d588744 100644 --- a/scripts/events/lib/injects.js +++ b/scripts/events/lib/injects.js @@ -42,7 +42,7 @@ class ViewInject { // Init injects function initInject(base_dir) { - let injects = {}; + const injects = {}; points.styles.forEach(item => { injects[item] = new StylusInject(base_dir); }); @@ -54,7 +54,7 @@ function initInject(base_dir) { module.exports = hexo => { // Exec theme_inject filter - let injects = initInject(hexo.base_dir); + const injects = initInject(hexo.base_dir); hexo.execFilterSync('theme_inject', injects); hexo.theme.config.injects = {}; @@ -65,11 +65,11 @@ module.exports = hexo => { // Inject views points.views.forEach(type => { - let configs = Object.create(null); + const configs = Object.create(null); hexo.theme.config.injects[type] = []; // Add or override view. injects[type].raws.forEach((injectObj, index) => { - let name = `inject/${type}/${injectObj.name}`; + const name = `inject/${type}/${injectObj.name}`; hexo.theme.setView(name, injectObj.raw); configs[name] = { layout : name, diff --git a/scripts/filters/comment/changyan.js b/scripts/filters/comment/changyan.js index aa226e5..fd4938c 100644 --- a/scripts/filters/comment/changyan.js +++ b/scripts/filters/comment/changyan.js @@ -7,7 +7,7 @@ const { iconText } = require('./common'); // Add comment hexo.extend.filter.register('theme_inject', injects => { - let theme = hexo.theme.config; + const theme = hexo.theme.config; if (!theme.changyan.enable || !theme.changyan.appid || !theme.changyan.appkey) return; injects.comment.raw('changyan', ` @@ -22,7 +22,7 @@ hexo.extend.filter.register('theme_inject', injects => { // Add post_meta hexo.extend.filter.register('theme_inject', injects => { - let theme = hexo.theme.config; + const theme = hexo.theme.config; if (!theme.changyan.enable || !theme.changyan.appid || !theme.changyan.appkey) return; injects.postMeta.raw('changyan', ` diff --git a/scripts/filters/comment/default-config.js b/scripts/filters/comment/default-config.js index 33ea804..316a174 100644 --- a/scripts/filters/comment/default-config.js +++ b/scripts/filters/comment/default-config.js @@ -7,22 +7,22 @@ const path = require('path'); hexo.extend.filter.register('theme_inject', injects => { injects.comment.raws.forEach(element => { // Set default button content - let injectName = path.basename(element.name, path.extname(element.name)); + const injectName = path.basename(element.name, path.extname(element.name)); element.args[0] = Object.assign({ configKey: injectName, class : injectName, button : injectName }, element.args[0]); // Get locals and config - let locals = element.args[0]; - let config = hexo.theme.config.comments; + const locals = element.args[0]; + const config = hexo.theme.config.comments; // Set activeClass if (config.active === locals.configKey) { config.activeClass = locals.class; } // Set custom button content if (config.nav) { - let nav = config.nav[locals.configKey] || {}; + const nav = config.nav[locals.configKey] || {}; if (nav.order) { element.args[2] = nav.order; } diff --git a/scripts/filters/comment/disqus.js b/scripts/filters/comment/disqus.js index f2a3843..eaffdb9 100644 --- a/scripts/filters/comment/disqus.js +++ b/scripts/filters/comment/disqus.js @@ -7,7 +7,7 @@ const { iconText } = require('./common'); // Add comment hexo.extend.filter.register('theme_inject', injects => { - let theme = hexo.theme.config; + const theme = hexo.theme.config; if (!theme.disqus.enable || !theme.disqus.shortname) return; injects.comment.raw('disqus', ` @@ -24,7 +24,7 @@ hexo.extend.filter.register('theme_inject', injects => { // Add post_meta hexo.extend.filter.register('theme_inject', injects => { - let theme = hexo.theme.config; + const theme = hexo.theme.config; if (!theme.disqus.enable || !theme.disqus.shortname || !theme.disqus.count) return; injects.postMeta.raw('disqus', ` diff --git a/scripts/filters/comment/disqusjs.js b/scripts/filters/comment/disqusjs.js index 30ff682..1590738 100644 --- a/scripts/filters/comment/disqusjs.js +++ b/scripts/filters/comment/disqusjs.js @@ -6,7 +6,7 @@ const path = require('path'); // Add comment hexo.extend.filter.register('theme_inject', injects => { - let theme = hexo.theme.config; + const theme = hexo.theme.config; if (!theme.disqusjs.enable || !theme.disqusjs.shortname || !theme.disqusjs.apikey) return; injects.comment.raw('disqusjs', ` diff --git a/scripts/filters/comment/gitalk.js b/scripts/filters/comment/gitalk.js index 2d841a1..df5ebb8 100644 --- a/scripts/filters/comment/gitalk.js +++ b/scripts/filters/comment/gitalk.js @@ -6,7 +6,7 @@ const path = require('path'); // Add comment hexo.extend.filter.register('theme_inject', injects => { - let theme = hexo.theme.config; + const theme = hexo.theme.config; if (!theme.gitalk.enable) return; injects.comment.raw('gitalk', '
', {}, {cache: true}); diff --git a/scripts/filters/comment/livere.js b/scripts/filters/comment/livere.js index 6f80426..4832f65 100644 --- a/scripts/filters/comment/livere.js +++ b/scripts/filters/comment/livere.js @@ -6,7 +6,7 @@ const path = require('path'); // Add comment hexo.extend.filter.register('theme_inject', injects => { - let theme = hexo.theme.config; + const theme = hexo.theme.config; if (!theme.livere_uid) return; injects.comment.raw('livere', ` diff --git a/scripts/filters/comment/valine.js b/scripts/filters/comment/valine.js index 041fbff..5cd1ae6 100644 --- a/scripts/filters/comment/valine.js +++ b/scripts/filters/comment/valine.js @@ -7,7 +7,7 @@ const { iconText } = require('./common'); // Add comment hexo.extend.filter.register('theme_inject', injects => { - let theme = hexo.theme.config; + const theme = hexo.theme.config; if (!theme.valine.enable || !theme.valine.appId || !theme.valine.appKey) return; injects.comment.raw('valine', '
', {}, {cache: true}); @@ -18,7 +18,7 @@ hexo.extend.filter.register('theme_inject', injects => { // Add post_meta hexo.extend.filter.register('theme_inject', injects => { - let theme = hexo.theme.config; + const theme = hexo.theme.config; if (!theme.valine.enable || !theme.valine.appId || !theme.valine.appKey) return; injects.postMeta.raw('valine', ` diff --git a/scripts/filters/default-injects.js b/scripts/filters/default-injects.js index 5f614b9..22bd7ed 100644 --- a/scripts/filters/default-injects.js +++ b/scripts/filters/default-injects.js @@ -5,7 +5,7 @@ const points = require('../events/lib/injects-point'); hexo.extend.filter.register('theme_inject', injects => { - let filePath = hexo.theme.config.custom_file_path; + const filePath = hexo.theme.config.custom_file_path; if (!filePath) return; diff --git a/scripts/filters/post.js b/scripts/filters/post.js index 744d6af..23c8452 100644 --- a/scripts/filters/post.js +++ b/scripts/filters/post.js @@ -17,7 +17,7 @@ hexo.extend.filter.register('after_post_render', data => { if (!href) return match; // Exit if the url has same host with `config.url`, which means it's an internal link. - let link = url.parse(href); + const link = url.parse(href); if (!link.protocol || link.hostname === siteHost) return match; return `${html}`; diff --git a/scripts/helpers/engine.js b/scripts/helpers/engine.js index 12201fe..fdcb18d 100644 --- a/scripts/helpers/engine.js +++ b/scripts/helpers/engine.js @@ -53,7 +53,7 @@ hexo.extend.helper.register('post_nav', function(post) { }); hexo.extend.helper.register('gitalk_md5', function(path) { - let str = this.url_for(path); + const str = this.url_for(path); str.replace('index.html', ''); return crypto.createHash('md5').update(str).digest('hex'); }); diff --git a/scripts/helpers/next-config.js b/scripts/helpers/next-config.js index a4596c2..5fe3a36 100644 --- a/scripts/helpers/next-config.js +++ b/scripts/helpers/next-config.js @@ -8,9 +8,9 @@ const url = require('url'); * Export theme config to js */ hexo.extend.helper.register('next_config', function() { - let { config, theme, next_version } = this; + const { config, theme, next_version } = this; config.algolia = config.algolia || {}; - let exportConfig = { + const exportConfig = { hostname : url.parse(config.url).hostname || config.url, root : config.root, scheme : theme.scheme, diff --git a/scripts/helpers/next-url.js b/scripts/helpers/next-url.js index 26aab87..de12fe0 100644 --- a/scripts/helpers/next-url.js +++ b/scripts/helpers/next-url.js @@ -26,7 +26,7 @@ hexo.extend.helper.register('next_url', function(path, text, options = {}) { }; } - for (let key in options) { + for (const key in options) { /** * If option have `class` attribute, add it to diff --git a/scripts/helpers/next-vendors.js b/scripts/helpers/next-vendors.js index 7d07b15..da09645 100644 --- a/scripts/helpers/next-vendors.js +++ b/scripts/helpers/next-vendors.js @@ -3,8 +3,8 @@ 'use strict'; hexo.extend.helper.register('js_vendors', function() { - let { config, theme } = this; - let vendors = { + const { config, theme } = this; + const vendors = { anime: 'lib/anime.min.js' }; if (config.prismjs.enable && !config.prismjs.preprocess) { diff --git a/scripts/helpers/tagcloud.js b/scripts/helpers/tagcloud.js index f05c74e..03b476d 100644 --- a/scripts/helpers/tagcloud.js +++ b/scripts/helpers/tagcloud.js @@ -43,7 +43,7 @@ hexo.extend.helper.register('next_tagcloud', function(options) { tags.forEach(tag => { const ratio = length ? sizes.indexOf(tag.length) / length : 0; const size = min + ((max - min) * ratio); - let style = `font-size: ${parseFloat(size.toFixed(2))}${unit};`; + const style = `font-size: ${parseFloat(size.toFixed(2))}${unit};`; result.push( `${transform ? transform(tag.name) : tag.name}` diff --git a/scripts/tags/button.js b/scripts/tags/button.js index 8e0a865..021c433 100644 --- a/scripts/tags/button.js +++ b/scripts/tags/button.js @@ -8,7 +8,7 @@ function postButton(args) { args = args.join(' ').split(','); - let url = args[0]; + const url = args[0]; let text = args[1] || ''; let icon = args[2] || ''; let title = args[3] || ''; diff --git a/scripts/tags/group-pictures.js b/scripts/tags/group-pictures.js index 99fc4ee..0ab46e8 100644 --- a/scripts/tags/group-pictures.js +++ b/scripts/tags/group-pictures.js @@ -73,7 +73,7 @@ const LAYOUTS = { function groupBy(group, data) { const r = []; - for (let count of group) { + for (const count of group) { r.push(data.slice(0, count)); data = data.slice(count); } diff --git a/scripts/tags/pdf.js b/scripts/tags/pdf.js index a4953bd..78303ca 100644 --- a/scripts/tags/pdf.js +++ b/scripts/tags/pdf.js @@ -7,7 +7,7 @@ 'use strict'; function pdf(args) { - let theme = hexo.theme.config; + const theme = hexo.theme.config; return `
`; } diff --git a/source/js/algolia-search.js b/source/js/algolia-search.js index 0c9e012..9ad8151 100644 --- a/source/js/algolia-search.js +++ b/source/js/algolia-search.js @@ -4,7 +4,7 @@ document.addEventListener('DOMContentLoaded', () => { const algoliaSettings = CONFIG.algolia; const { indexName, appID, apiKey } = algoliaSettings; - let search = instantsearch({ + const search = instantsearch({ indexName, searchClient : algoliasearch(appID, apiKey), searchFunction: helper => { @@ -40,7 +40,7 @@ document.addEventListener('DOMContentLoaded', () => { container: '#algolia-stats', templates: { text: data => { - let stats = algoliaSettings.labels.hits_stats + const stats = algoliaSettings.labels.hits_stats .replace(/\$\{hits}/, data.nbHits) .replace(/\$\{time}/, data.processingTimeMS); return `${stats} @@ -56,7 +56,7 @@ document.addEventListener('DOMContentLoaded', () => { container: '#algolia-hits', templates: { item: data => { - let link = data.permalink ? data.permalink : CONFIG.root + data.path; + const link = data.permalink ? data.permalink : CONFIG.root + data.path; return `${data._highlightResult.title.value}`; }, empty: data => { diff --git a/source/js/local-search.js b/source/js/local-search.js index 48541bf..da5d276 100644 --- a/source/js/local-search.js +++ b/source/js/local-search.js @@ -17,15 +17,15 @@ document.addEventListener('DOMContentLoaded', () => { const getIndexByWord = (word, text, caseSensitive) => { if (CONFIG.localsearch.unescape) { - let div = document.createElement('div'); + const div = document.createElement('div'); div.innerText = word; word = div.innerHTML; } - let wordLen = word.length; + const wordLen = word.length; if (wordLen === 0) return []; let startPosition = 0; let position = []; - let index = []; + const index = []; if (!caseSensitive) { text = text.toLowerCase(); word = word.toLowerCase(); @@ -41,7 +41,7 @@ document.addEventListener('DOMContentLoaded', () => { const mergeIntoSlice = (start, end, index, searchText) => { let item = index[index.length - 1]; let { position, word } = item; - let hits = []; + const hits = []; let searchTextCountInSlice = 0; while (position + word.length <= end && index.length !== 0) { if (word === searchText) { @@ -51,7 +51,7 @@ document.addEventListener('DOMContentLoaded', () => { position, length: word.length }); - let wordEnd = position + word.length; + const wordEnd = position + word.length; // Move to next position of hit index.pop(); @@ -80,7 +80,7 @@ document.addEventListener('DOMContentLoaded', () => { let prevEnd = slice.start; slice.hits.forEach(hit => { result += text.substring(prevEnd, hit.position); - let end = hit.position + hit.length; + const end = hit.position + hit.length; result += `${text.substring(hit.position, end)}`; prevEnd = end; }); @@ -90,17 +90,17 @@ document.addEventListener('DOMContentLoaded', () => { const inputEventFunction = () => { if (!isfetched) return; - let searchText = input.value.trim().toLowerCase(); - let keywords = searchText.split(/[-\s]+/); + const searchText = input.value.trim().toLowerCase(); + const keywords = searchText.split(/[-\s]+/); if (keywords.length > 1) { keywords.push(searchText); } - let resultItems = []; + const resultItems = []; if (searchText.length > 0) { // Perform local searching datas.forEach(({ title, content, url }) => { - let titleInLowerCase = title.toLowerCase(); - let contentInLowerCase = content.toLowerCase(); + const titleInLowerCase = title.toLowerCase(); + const contentInLowerCase = content.toLowerCase(); let indexOfTitle = []; let indexOfContent = []; let searchTextCount = 0; @@ -111,7 +111,7 @@ document.addEventListener('DOMContentLoaded', () => { // Show search results if (indexOfTitle.length > 0 || indexOfContent.length > 0) { - let hitCount = indexOfTitle.length + indexOfContent.length; + const hitCount = indexOfTitle.length + indexOfContent.length; // Sort index by position of keyword [indexOfTitle, indexOfContent].forEach(index => { index.sort((itemLeft, itemRight) => { @@ -122,17 +122,17 @@ document.addEventListener('DOMContentLoaded', () => { }); }); - let slicesOfTitle = []; + const slicesOfTitle = []; if (indexOfTitle.length !== 0) { - let tmp = mergeIntoSlice(0, title.length, indexOfTitle, searchText); + const tmp = mergeIntoSlice(0, title.length, indexOfTitle, searchText); searchTextCount += tmp.searchTextCountInSlice; slicesOfTitle.push(tmp); } let slicesOfContent = []; while (indexOfContent.length !== 0) { - let item = indexOfContent[indexOfContent.length - 1]; - let { position, word } = item; + const item = indexOfContent[indexOfContent.length - 1]; + const { position, word } = item; // Cut out 100 characters let start = position - 20; let end = position + 80; @@ -145,7 +145,7 @@ document.addEventListener('DOMContentLoaded', () => { if (end > content.length) { end = content.length; } - let tmp = mergeIntoSlice(start, end, indexOfContent, searchText); + const tmp = mergeIntoSlice(start, end, indexOfContent, searchText); searchTextCount += tmp.searchTextCountInSlice; slicesOfContent.push(tmp); } @@ -161,7 +161,7 @@ document.addEventListener('DOMContentLoaded', () => { }); // Select top N slices in content - let upperBound = parseInt(CONFIG.localsearch.top_n_per_article, 10); + const upperBound = parseInt(CONFIG.localsearch.top_n_per_article, 10); if (upperBound >= 0) { slicesOfContent = slicesOfContent.slice(0, upperBound); } diff --git a/source/js/utils.js b/source/js/utils.js index 3013c4a..4b6be52 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -45,7 +45,7 @@ NexT.utils = { registerExtURL: function() { document.querySelectorAll('span.exturl').forEach(element => { - let link = document.createElement('a'); + const link = document.createElement('a'); // https://stackoverflow.com/questions/30106476/using-javascripts-atob-to-decode-base64-doesnt-properly-decode-utf-8-strings link.href = decodeURIComponent(atob(element.dataset.url).split('').map(c => { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); @@ -122,8 +122,8 @@ NexT.utils = { const box = document.createElement('div'); box.className = 'video-container'; element.wrap(box); - let width = Number(element.width); - let height = Number(element.height); + const width = Number(element.width); + const height = Number(element.height); if (width && height) { box.style.paddingTop = (height / width * 100) + '%'; } @@ -278,10 +278,10 @@ NexT.utils = { }, supportsPDFs: function() { - let ua = navigator.userAgent; - let isFirefoxWithPDFJS = ua.includes('irefox') && parseInt(ua.split('rv:')[1].split('.')[0], 10) > 18; - let supportsPdfMimeType = typeof navigator.mimeTypes['application/pdf'] !== 'undefined'; - let isIOS = /iphone|ipad|ipod/i.test(ua.toLowerCase()); + const ua = navigator.userAgent; + const isFirefoxWithPDFJS = ua.includes('irefox') && parseInt(ua.split('rv:')[1].split('.')[0], 10) > 18; + const supportsPdfMimeType = typeof navigator.mimeTypes['application/pdf'] !== 'undefined'; + const isIOS = /iphone|ipad|ipod/i.test(ua.toLowerCase()); return isFirefoxWithPDFJS || (supportsPdfMimeType && !isIOS); }, @@ -350,8 +350,8 @@ NexT.utils = { callback(); return; } - let intersectionObserver = new IntersectionObserver((entries, observer) => { - let entry = entries[0]; + const intersectionObserver = new IntersectionObserver((entries, observer) => { + const entry = entries[0]; if (entry.isIntersecting) { callback(); observer.disconnect();