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.
This commit is contained in:
Alexander Batischev 2022-11-03 23:40:45 +03:00
parent 8663e47585
commit 1304dbeb25
No known key found for this signature in database
GPG Key ID: 356961A20C8BFD03
3 changed files with 16 additions and 3 deletions

View File

@ -17,7 +17,7 @@
</div>
<div class="one-tag-list">
<span class="tag__text"><%= title %></span>
<% 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){ %>
<h3 class="tags__year"><%- -year %></h3>
<ul class="tags__list">

View File

@ -10,11 +10,11 @@ layout: layout
<% }) %>
</div>
<div class="one-tag-list">
<% 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){ %>
<h3 class="tags__year"><%- -year %></h3>
<ul class="tags__list">
<%- partial('_partial/taglist', {posts: _.sortBy(groupObj[year], 'date').reverse()}) %>
<%- partial('_partial/taglist', {posts: groupObj[year]}) %>
</ul>
<% } %>
</div>

View File

@ -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;
}