feat(config): make meta global configurable

This commit is contained in:
ppoffice 2019-07-03 23:54:26 -04:00
parent 58019ab0cd
commit 3cd4b5dc47
4 changed files with 46 additions and 37 deletions

View File

@ -5,25 +5,11 @@
* <%- is_categories(page) %>
* <%- is_tags(page) %>
* <%- page_title(page) %>
* <%- meta(post) %>
* <%- has_thumbnail(post) %>
* <%- get_thumbnail(post) %>
* <%- get_og_image(post) %>
*/
module.exports = function (hexo) {
function trim(str) {
return str.trim().replace(/^"(.*)"$/, '$1').replace(/^'(.*)'$/, '$1');
}
function split(str, sep) {
var result = [];
var matched = null;
while (matched = sep.exec(str)) {
result.push(matched[0]);
}
return result;
}
hexo.extend.helper.register('is_categories', function (page = null) {
return (page === null ? this.page : page).__categories;
});
@ -60,27 +46,6 @@ module.exports = function (hexo) {
return [title, siteTitle].filter(str => typeof (str) !== 'undefined' && str.trim() !== '').join(' - ');
});
hexo.extend.helper.register('meta', function (post) {
var metas = post.meta || [];
var output = '';
var metaDOMArray = metas.map(function (meta) {
var entities = split(meta, /(?:[^\\;]+|\\.)+/g);
var entityArray = entities.map(function (entity) {
var keyValue = split(entity, /(?:[^\\=]+|\\.)+/g);
if (keyValue.length < 2) {
return null;
}
var key = trim(keyValue[0]);
var value = trim(keyValue[1]);
return key + '="' + value + '"';
}).filter(function (entity) {
return entity;
});
return '<meta ' + entityArray.join(' ') + ' />';
});
return metaDOMArray.join('\n');
});
hexo.extend.helper.register('has_thumbnail', function (post) {
const getConfig = hexo.extend.helper.get('get_config').bind(this);
const allowThumbnail = getConfig('article.thumbnail', true);
@ -110,7 +75,7 @@ module.exports = function (hexo) {
let og_image
if(hasOGImage)
if (hasOGImage)
og_image = post.og_image
else if (hasThumbnail)
og_image = getThumbnail(post);

View File

@ -10,6 +10,7 @@
* <%- duration() %>
* <%- word_count(content) %>
* <%- md5(data) %>
* <%- meta() %>
*/
const URL = require('url').URL;
const moment = require('moment');
@ -63,4 +64,38 @@ module.exports = function (hexo) {
hexo.extend.helper.register('md5', function (data) {
return crypto.createHash('md5').update(data).digest("hex")
});
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');
});
}

View File

@ -6,6 +6,15 @@ module.exports = {
[doc]: 'Path or URL to the website\'s icon',
[defaultValue]: '/images/favicon.svg',
},
meta: {
[type]: 'array',
[doc]: 'Additional HTML meta tags in an array.',
[defaultValue]: null,
'*': {
[type]: 'string',
[doc]: 'Meta tag specified in <attribute>=<value> style.\nE.g., name=theme-color;content=#123456 => <meta name="theme-color" content="#123456">'
}
},
rss: {
[type]: 'string',
[doc]: 'Path or URL to RSS atom.xml',

View File

@ -1,7 +1,7 @@
<meta charset="utf-8">
<title><%= page_title() %></title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<%- meta(page) %>
<%- meta() %>
<% if (has_config('open_graph')) { %>
<%- open_graph({