From 3cd4b5dc4737821deb219d4b8b15df369e02c438 Mon Sep 17 00:00:00 2001 From: ppoffice Date: Wed, 3 Jul 2019 23:54:26 -0400 Subject: [PATCH] feat(config): make meta global configurable --- includes/helpers/page.js | 37 +------------------------------------ includes/helpers/site.js | 35 +++++++++++++++++++++++++++++++++++ includes/specs/meta.spec.js | 9 +++++++++ layout/common/head.ejs | 2 +- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/includes/helpers/page.js b/includes/helpers/page.js index a44c37b..3e38b79 100644 --- a/includes/helpers/page.js +++ b/includes/helpers/page.js @@ -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 ''; - }); - 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); diff --git a/includes/helpers/site.js b/includes/helpers/site.js index 7641295..f9bd525 100644 --- a/includes/helpers/site.js +++ b/includes/helpers/site.js @@ -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 ''; + }); + return metaDOMArray.join('\n'); + }); } \ No newline at end of file diff --git a/includes/specs/meta.spec.js b/includes/specs/meta.spec.js index 0004d61..5b59fb0 100644 --- a/includes/specs/meta.spec.js +++ b/includes/specs/meta.spec.js @@ -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 = style.\nE.g., name=theme-color;content=#123456 => ' + } + }, rss: { [type]: 'string', [doc]: 'Path or URL to RSS atom.xml', diff --git a/layout/common/head.ejs b/layout/common/head.ejs index f8ddefd..25ddad6 100644 --- a/layout/common/head.ejs +++ b/layout/common/head.ejs @@ -1,7 +1,7 @@ <%= page_title() %> -<%- meta(page) %> +<%- meta() %> <% if (has_config('open_graph')) { %> <%- open_graph({