mirror of
https://github.com/amehime/hexo-theme-shoka.git
synced 2026-04-05 18:49:07 +08:00
172 lines
4.5 KiB
JavaScript
172 lines
4.5 KiB
JavaScript
/* global hexo */
|
|
|
|
'use strict';
|
|
|
|
const { htmlTag } = require('hexo-util');
|
|
const url = require('url');
|
|
const crypto = require('crypto');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const yaml = require('js-yaml');
|
|
|
|
const imageListFile = fs.readFileSync(path.join(__dirname, '../../_images.yml'));
|
|
const imageList = yaml.safeLoad(imageListFile);
|
|
|
|
const randomServer = function() {
|
|
return [1,2,3,4][Math.floor(Math.random() * 4)]
|
|
}
|
|
|
|
const randomBG = function(count = 1) {
|
|
if(count && count > 1) {
|
|
var shuffled = imageList.slice(0), i = imageList.length, min = i - count, temp, index;
|
|
while (i-- > min) {
|
|
index = Math.floor((i + 1) * Math.random());
|
|
temp = shuffled[index];
|
|
shuffled[index] = shuffled[i];
|
|
shuffled[i] = temp;
|
|
}
|
|
|
|
return shuffled.slice(min).map(function(img) {
|
|
return 'https://tva'+randomServer()+'.sinaimg.cn/large/'+img
|
|
});
|
|
}
|
|
return 'https://tva'+randomServer()+'.sinaimg.cn/mw690/'+imageList[Math.floor(Math.random() * imageList.length)]
|
|
}
|
|
|
|
|
|
hexo.extend.helper.register('hexo_env', function (type) {
|
|
return this.env[type]
|
|
})
|
|
|
|
|
|
hexo.extend.helper.register('theme_env', function (type) {
|
|
var env = require('../../package.json')
|
|
return env[type]
|
|
})
|
|
|
|
hexo.extend.helper.register('_css', function(...urls) {
|
|
const { statics, css } = hexo.theme.config;
|
|
|
|
return urls.map(url => this.css(`${statics}${css}/${url}`)).join('');
|
|
});
|
|
|
|
|
|
hexo.extend.helper.register('_js', function(...urls) {
|
|
const { statics, js } = hexo.theme.config;
|
|
|
|
return urls.map(url => this.js(`${statics}${js}/${url}`)).join('');
|
|
});
|
|
|
|
|
|
hexo.extend.helper.register('_url', function(path, text, options = {}) {
|
|
const { config } = this;
|
|
const data = url.parse(path);
|
|
const siteHost = url.parse(config.url).hostname || config.url;
|
|
|
|
const theme = hexo.theme.config;
|
|
let exturl = '';
|
|
let tag = 'a';
|
|
let attrs = { href: this.url_for(path) };
|
|
|
|
// If `exturl` enabled, set spanned links only on external links.
|
|
if (theme.exturl && data.protocol && data.hostname !== siteHost) {
|
|
tag = 'span';
|
|
exturl = 'exturl';
|
|
const encoded = Buffer.from(path).toString('base64');
|
|
attrs = {
|
|
class : exturl,
|
|
'data-url': encoded
|
|
};
|
|
}
|
|
|
|
for (let key in options) {
|
|
|
|
/**
|
|
* If option have `class` attribute, add it to
|
|
* 'exturl' class if `exturl` option enabled.
|
|
*/
|
|
if (exturl !== '' && key === 'class') {
|
|
attrs[key] += ' ' + options[key];
|
|
} else {
|
|
attrs[key] = options[key];
|
|
}
|
|
}
|
|
|
|
if (attrs.class && Array.isArray(attrs.class)) {
|
|
attrs.class = attrs.class.join(' ');
|
|
}
|
|
|
|
// If it's external link, rewrite attributes.
|
|
if (data.protocol && data.hostname !== siteHost) {
|
|
attrs.external = null;
|
|
|
|
if (!theme.exturl) {
|
|
// Only for simple link need to rewrite/add attributes.
|
|
attrs.rel = 'noopener';
|
|
attrs.target = '_blank';
|
|
} else {
|
|
// Remove rel attributes for `exturl` in main menu.
|
|
attrs.rel = null;
|
|
}
|
|
}
|
|
|
|
return htmlTag(tag, attrs, decodeURI(text), false);
|
|
});
|
|
|
|
|
|
hexo.extend.helper.register('_cover', function(item, num) {
|
|
var that = this
|
|
const { statics, js } = hexo.theme.config;
|
|
|
|
var format = function(img) {
|
|
if (img.startsWith('//') || img.startsWith('http')) {
|
|
return img
|
|
} else {
|
|
return that.url_for(statics + img)
|
|
}
|
|
}
|
|
|
|
if(item.cover) {
|
|
return format(item.cover)
|
|
} else if (item.photos && item.photos.length > 0) {
|
|
return format(item.photos[0])
|
|
} else {
|
|
return randomBG(num || 1);
|
|
}
|
|
|
|
})
|
|
|
|
hexo.extend.helper.register('_md5', function(path) {
|
|
let str = this.url_for(path);
|
|
str.replace('index.html', '');
|
|
return crypto.createHash('md5').update(str).digest('hex');
|
|
});
|
|
|
|
|
|
hexo.extend.helper.register('canonical', function() {
|
|
// https://support.google.com/webmasters/answer/139066
|
|
const { permalink } = hexo.config;
|
|
let url = this.url.replace(/index\.html$/, '');
|
|
if (!permalink.endsWith('.html')) {
|
|
url = url.replace(/\.html$/, '');
|
|
}
|
|
return `<link rel="canonical" href="${url}">`;
|
|
});
|
|
|
|
/**
|
|
* Get page path given a certain language tag
|
|
*/
|
|
hexo.extend.helper.register('i18n_path', function(language) {
|
|
const { path, lang } = this.page;
|
|
const base = path.startsWith(lang) ? path.slice(lang.length + 1) : path;
|
|
return this.url_for(`${this.languages.indexOf(language) === 0 ? '' : '/' + language}/${base}`);
|
|
});
|
|
|
|
/**
|
|
* Get the language name
|
|
*/
|
|
hexo.extend.helper.register('language_name', function(language) {
|
|
const name = hexo.theme.i18n.__(language)('name');
|
|
return name === 'name' ? language : name;
|
|
});
|