fediparty/themes/starter/scripts/hexo-helpers.js

39 lines
1 KiB
JavaScript
Raw Normal View History

2018-03-12 19:31:14 +00:00
/* global hexo */
hexo.extend.helper.register('is_root', isRoot);
hexo.extend.helper.register('get_langs', getLangs);
hexo.extend.helper.register('switch_lang', switchLang);
2018-12-18 23:09:37 +00:00
hexo.extend.helper.register('getPath', getPath);
hexo.extend.helper.register('stripHTTPS', stripHTTPS);
2018-03-12 19:31:14 +00:00
function isRoot() {
2019-11-29 20:46:17 +00:00
const lang = this.page.lang;
return this.page.path === 'index.html' || this.page.path === `${lang}/index.html`;
2018-03-12 19:31:14 +00:00
}
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 '';
}
2018-12-18 23:09:37 +00:00
function getPath() {
const url = this.page.path;
const filename = url.substr(3);
const splitPath = filename.split('/');
2018-12-18 23:09:37 +00:00
return splitPath[0];
}
function stripHTTPS(x) {
return x.split('://')[1];
}