mirror of
https://codeberg.org/fediverse/fediparty.git
synced 2024-11-28 11:21:28 +00:00
1304dbeb25
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.
52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
|
|
/* global hexo */
|
|
hexo.extend.helper.register('is_root', isRoot);
|
|
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;
|
|
return this.page.path === 'index.html' || this.page.path === `${lang}/index.html`;
|
|
}
|
|
|
|
function getLangs() {
|
|
return this.config.language.filter(lang => lang !== 'default');
|
|
}
|
|
|
|
function switchLang(lang) {
|
|
if (typeof lang === 'undefined') return '';
|
|
if (this.is_root()) return this.url_for(lang);
|
|
if (this.page.lang === lang) return '';
|
|
const langReg = new RegExp(`^${this.page.lang}/`);
|
|
if (langReg.test(this.page.path)) {
|
|
return this.url_for(this.page.path.replace(langReg, `${lang}/`));
|
|
}
|
|
return '';
|
|
}
|
|
|
|
function getPath() {
|
|
const url = this.page.path;
|
|
const filename = url.substr(3);
|
|
const splitPath = filename.split('/');
|
|
return splitPath[0];
|
|
}
|
|
|
|
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;
|
|
}
|