hexo-theme-icarus/includes/helpers/site.js

66 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-10-16 05:28:42 +00:00
/**
* Helper functions related the site properties.
*
* @example
* <%- is_same_link(url_a, url_b) %>
* <%- get_domain(url) %>
2018-10-16 05:28:42 +00:00
* <%- post_count() %>
* <%- category_count() %>
* <%- tag_count() %>
* <%- duration() %>
* <%- word_count(content) %>
* <%- md5(data) %>
2018-10-16 05:28:42 +00:00
*/
const URL = require('url').URL;
2018-10-16 05:28:42 +00:00
const moment = require('moment');
const crypto = require('crypto');
2018-10-16 05:28:42 +00:00
module.exports = function (hexo) {
hexo.extend.helper.register('is_same_link', function (a, b) {
function santize(url) {
let paths = url.replace(/(^\w+:|^)\/\//, '').split('#')[0].split('/').filter(p => p.trim() !== '');
if (paths.length > 0 && paths[paths.length - 1].trim() === 'index.html') {
paths = paths.slice(0, paths.length - 1)
}
return paths.join('/');
}
return santize(this.url_for(a)) == santize(this.url_for(b));
});
hexo.extend.helper.register('get_domain', function (link) {
const url = new URL(link);
return url.hostname;
});
2018-10-16 05:28:42 +00:00
hexo.extend.helper.register('post_count', function () {
return this.site.posts.length;
});
hexo.extend.helper.register('category_count', function () {
return this.site.categories.filter(category => category.length).length;
});
hexo.extend.helper.register('tag_count', function () {
return this.site.tags.filter(tag => tag.length).length;
});
/**
* Export moment.duration
*/
hexo.extend.helper.register('duration', function () {
return moment.duration.apply(moment, arguments);
});
/**
* Get the word count of a paragraph.
*/
hexo.extend.helper.register('word_count', function (content) {
content = content.replace(/<\/?[a-z][^>]*>/gi, '');
content = content.trim();
return content ? (content.match(/[\u00ff-\uffff]|[a-zA-Z]+/g) || []).length : 0;
});
hexo.extend.helper.register('md5', function (data) {
return crypto.createHash('md5').update(data).digest("hex")
});
2018-10-16 05:28:42 +00:00
}