Skip to content

Commit

Permalink
support keywords from page.keywords, page.tag or theme.keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
restran committed Apr 25, 2017
1 parent fbdee90 commit e3aff27
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e3aff27

Please sign in to comment.