feat(config): make meta global configurable
This commit is contained in:
parent
58019ab0cd
commit
3cd4b5dc47
|
@ -5,25 +5,11 @@
|
||||||
* <%- is_categories(page) %>
|
* <%- is_categories(page) %>
|
||||||
* <%- is_tags(page) %>
|
* <%- is_tags(page) %>
|
||||||
* <%- page_title(page) %>
|
* <%- page_title(page) %>
|
||||||
* <%- meta(post) %>
|
|
||||||
* <%- has_thumbnail(post) %>
|
* <%- has_thumbnail(post) %>
|
||||||
* <%- get_thumbnail(post) %>
|
* <%- get_thumbnail(post) %>
|
||||||
* <%- get_og_image(post) %>
|
* <%- get_og_image(post) %>
|
||||||
*/
|
*/
|
||||||
module.exports = function (hexo) {
|
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) {
|
hexo.extend.helper.register('is_categories', function (page = null) {
|
||||||
return (page === null ? this.page : page).__categories;
|
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(' - ');
|
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) {
|
hexo.extend.helper.register('has_thumbnail', function (post) {
|
||||||
const getConfig = hexo.extend.helper.get('get_config').bind(this);
|
const getConfig = hexo.extend.helper.get('get_config').bind(this);
|
||||||
const allowThumbnail = getConfig('article.thumbnail', true);
|
const allowThumbnail = getConfig('article.thumbnail', true);
|
||||||
|
@ -110,7 +75,7 @@ module.exports = function (hexo) {
|
||||||
|
|
||||||
let og_image
|
let og_image
|
||||||
|
|
||||||
if(hasOGImage)
|
if (hasOGImage)
|
||||||
og_image = post.og_image
|
og_image = post.og_image
|
||||||
else if (hasThumbnail)
|
else if (hasThumbnail)
|
||||||
og_image = getThumbnail(post);
|
og_image = getThumbnail(post);
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
* <%- duration() %>
|
* <%- duration() %>
|
||||||
* <%- word_count(content) %>
|
* <%- word_count(content) %>
|
||||||
* <%- md5(data) %>
|
* <%- md5(data) %>
|
||||||
|
* <%- meta() %>
|
||||||
*/
|
*/
|
||||||
const URL = require('url').URL;
|
const URL = require('url').URL;
|
||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
|
@ -63,4 +64,38 @@ module.exports = function (hexo) {
|
||||||
hexo.extend.helper.register('md5', function (data) {
|
hexo.extend.helper.register('md5', function (data) {
|
||||||
return crypto.createHash('md5').update(data).digest("hex")
|
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');
|
||||||
|
});
|
||||||
}
|
}
|
|
@ -6,6 +6,15 @@ module.exports = {
|
||||||
[doc]: 'Path or URL to the website\'s icon',
|
[doc]: 'Path or URL to the website\'s icon',
|
||||||
[defaultValue]: '/images/favicon.svg',
|
[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: {
|
rss: {
|
||||||
[type]: 'string',
|
[type]: 'string',
|
||||||
[doc]: 'Path or URL to RSS atom.xml',
|
[doc]: 'Path or URL to RSS atom.xml',
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title><%= page_title() %></title>
|
<title><%= page_title() %></title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
|
||||||
<%- meta(page) %>
|
<%- meta() %>
|
||||||
|
|
||||||
<% if (has_config('open_graph')) { %>
|
<% if (has_config('open_graph')) { %>
|
||||||
<%- open_graph({
|
<%- open_graph({
|
||||||
|
|
Loading…
Reference in New Issue