From 1304dbeb25ef7fd556c0a0210b681688eb193c93 Mon Sep 17 00:00:00 2001 From: Alexander Batischev Date: Thu, 3 Nov 2022 23:40:45 +0300 Subject: [PATCH] Avoid dependency on lodash This commit re-implements `_.groupBy`. The call to `_.sortBy` was unnecessary and was thus removed entirely. This should simplify migration to newer releases of Hexo that no longer include lodash. --- themes/starter/layout/tag.ejs | 2 +- themes/starter/layout/tags.ejs | 4 ++-- themes/starter/scripts/hexo-helpers.js | 13 +++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/themes/starter/layout/tag.ejs b/themes/starter/layout/tag.ejs index 07e01e1..e0e57fc 100644 --- a/themes/starter/layout/tag.ejs +++ b/themes/starter/layout/tag.ejs @@ -17,7 +17,7 @@
<%= title %> - <% const groupObj = _.groupBy(page.posts.toArray(), function(p) {return -p.date.format('YYYY')}) %> + <% const groupObj = groupBy(page.posts.toArray(), function(p) {return -p.date.format('YYYY')}) %> <% for (let year in groupObj){ %>

<%- -year %>

- <% const groupObj = _.groupBy(site.posts.sort('date', -1).toArray(), function(p){return -p.date.format('YYYY')}) %> + <% const groupObj = groupBy(site.posts.sort('date', -1).toArray(), function(p){return -p.date.format('YYYY')}) %> <% for (let year in groupObj){ %>

<%- -year %>

<% } %>
diff --git a/themes/starter/scripts/hexo-helpers.js b/themes/starter/scripts/hexo-helpers.js index 2087d2b..88bfdb3 100644 --- a/themes/starter/scripts/hexo-helpers.js +++ b/themes/starter/scripts/hexo-helpers.js @@ -5,6 +5,7 @@ hexo.extend.helper.register('get_langs', getLangs); hexo.extend.helper.register('switch_lang', switchLang); hexo.extend.helper.register('getPath', getPath); hexo.extend.helper.register('stripHTTPS', stripHTTPS); +hexo.extend.helper.register('groupBy', groupBy); function isRoot() { const lang = this.page.lang; @@ -36,3 +37,15 @@ function getPath() { function stripHTTPS(x) { return x.split('://')[1]; } + +function groupBy(arr, fn) { + let result = {}; + for (const element of arr) { + const key = fn(element); + if (!(key in result)) { + result[key] = new Array(); + } + result[key].push(element); + } + return result; +}