From e3aff2760adbd1dd0651fe9e96a4a6d826e72393 Mon Sep 17 00:00:00 2001 From: restran Date: Tue, 25 Apr 2017 15:49:03 +0800 Subject: [PATCH] support keywords from page.keywords, page.tag or theme.keywords --- lib/plugins/helper/open_graph.js | 34 ++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index 035c25e1e4..12e47ceecc 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -5,16 +5,23 @@ var moment = require('moment'); var util = require('hexo-util'); var htmlTag = util.htmlTag; var stripHTML = util.stripHTML; +var escapeHTML = util.escapeHTML; var cheerio; -function meta(name, content) { +function meta(name, content, escape) { + if (escape !== false) { + content = escapeHTML(content); + } return htmlTag('meta', { name: name, content: content }) + '\n'; } -function og(name, content) { +function og(name, content, escape) { + if (escape !== false) { + content = escapeHTML(content); + } return htmlTag('meta', { property: name, content: content @@ -28,10 +35,12 @@ function openGraphHelper(options) { var page = this.page; var config = this.config; + var theme = this.theme; var content = page.content; var images = options.image || options.images || page.photos || []; var description = options.description || page.description || page.excerpt || content || config.description; - var keywords = page.tags; + // support keywords from page.keywords, page.tag or theme.keywords + var keywords = page.keywords || page.tags || theme.keywords; var title = options.title || page.title || config.title; var type = options.type || (this.is_post() ? 'article' : 'website'); var url = options.url || this.url; @@ -68,17 +77,22 @@ function openGraphHelper(options) { result += meta('description', description); } - if (keywords && keywords.toArray()) { - result += meta('keywords', keywords.map(function(tag) { - return tag.name; - }).filter(function(keyword) { - return !!keyword; - }).join()); + // support keywords in type of string or array + if (keywords) { + if (typeof keywords === 'string') { + result += meta('keywords', keywords); + } else if (keywords.toArray()) { + result += meta('keywords', keywords.map(function(tag) { + return tag.name; + }).filter(function(keyword) { + return !!keyword; + }).join()); + } } result += og('og:type', type); result += og('og:title', title); - result += og('og:url', url); + result += og('og:url', url, false); result += og('og:site_name', siteName); if (description) { result += og('og:description', description);