2018-10-16 05:28:42 +00:00
|
|
|
/**
|
|
|
|
* Helper functions related the site properties.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* <%- is_same_link(url_a, url_b) %>
|
2018-12-23 18:53:18 +00:00
|
|
|
* <%- get_domain(url) %>
|
2018-10-16 05:28:42 +00:00
|
|
|
* <%- post_count() %>
|
|
|
|
* <%- category_count() %>
|
|
|
|
* <%- tag_count() %>
|
|
|
|
* <%- duration() %>
|
|
|
|
* <%- word_count(content) %>
|
2019-01-03 02:03:06 +00:00
|
|
|
* <%- md5(data) %>
|
2019-07-04 03:54:26 +00:00
|
|
|
* <%- meta() %>
|
2019-08-15 04:05:21 +00:00
|
|
|
* <%- hexo_version() %>
|
2018-10-16 05:28:42 +00:00
|
|
|
*/
|
2018-12-23 18:53:18 +00:00
|
|
|
const URL = require('url').URL;
|
2018-10-16 05:28:42 +00:00
|
|
|
const moment = require('moment');
|
2019-01-03 02:03:06 +00:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
|
2018-12-23 18:53:18 +00:00
|
|
|
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;
|
|
|
|
});
|
2019-01-03 02:03:06 +00:00
|
|
|
|
|
|
|
hexo.extend.helper.register('md5', function (data) {
|
2019-08-15 04:05:21 +00:00
|
|
|
return crypto.createHash('md5').update(data).digest("hex");
|
2019-01-03 02:03:06 +00:00
|
|
|
});
|
2019-07-04 03:54:26 +00:00
|
|
|
|
|
|
|
hexo.extend.helper.register('meta', function () {
|
|
|
|
function trim(str) {
|
|
|
|
return str.trim().replace(/^"(.*)"$/, '$1').replace(/^'(.*)'$/, '$1');
|
|
|
|
}
|
|
|
|
|
|
|
|
function split(str, sep) {
|
|
|
|
const result = [];
|
|
|
|
let matched = null;
|
|
|
|
while (matched = sep.exec(str)) {
|
|
|
|
result.push(matched[0]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const getConfig = hexo.extend.helper.get('get_config').bind(this);
|
|
|
|
const metas = getConfig('meta', []);
|
|
|
|
const metaDOMArray = metas.map(function (meta) {
|
|
|
|
const entities = split(meta, /(?:[^\\;]+|\\.)+/g);
|
|
|
|
const entityArray = entities.map(function (entity) {
|
|
|
|
const keyValue = split(entity, /(?:[^\\=]+|\\.)+/g);
|
|
|
|
if (keyValue.length < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const key = trim(keyValue[0]);
|
|
|
|
const value = trim(keyValue[1]);
|
|
|
|
return key + '="' + value + '"';
|
|
|
|
}).filter(function (entity) {
|
|
|
|
return entity;
|
|
|
|
});
|
|
|
|
return '<meta ' + entityArray.join(' ') + ' />';
|
|
|
|
});
|
|
|
|
return metaDOMArray.join('\n');
|
|
|
|
});
|
2019-08-15 04:05:21 +00:00
|
|
|
|
|
|
|
hexo.extend.helper.register('hexo_version', function (data) {
|
|
|
|
return hexo.version;
|
|
|
|
});
|
2018-10-16 05:28:42 +00:00
|
|
|
}
|