Skip to content

Commit

Permalink
Allow open_graph generates meta tag "keywords" by page.keywords, page…
Browse files Browse the repository at this point in the history
….tags or config.keywords (#2535)

* support keywords from page.keywords, page.tag or theme.keywords

* fix code style error

* change to config.keywords

* no escape for image

* fix escape for description

* add test case and fix bug

* fix code style
  • Loading branch information
restran authored and NoahDragon committed Apr 26, 2017
1 parent fbdee90 commit 1bb19c8
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 16 deletions.
45 changes: 29 additions & 16 deletions lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ 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 && typeof content === 'string') {
content = escapeHTML(content);
}

return htmlTag('meta', {
name: name,
content: content
}) + '\n';
}

function og(name, content) {
function og(name, content, escape) {
if (escape !== false && typeof content === 'string') {
content = escapeHTML(content);
}

return htmlTag('meta', {
property: name,
content: content
Expand All @@ -31,7 +40,7 @@ function openGraphHelper(options) {
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;
var keywords = page.keywords || (page.tags && page.tags.length ? page.tags : undefined) || config.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 @@ -65,23 +74,27 @@ function openGraphHelper(options) {
}

if (description) {
result += meta('description', description);
result += meta('description', description, false);
}

if (keywords && keywords.toArray()) {
result += meta('keywords', keywords.map(function(tag) {
return tag.name;
}).filter(function(keyword) {
return !!keyword;
}).join());
if (keywords) {
if (typeof keywords === 'string') {
result += meta('keywords', keywords);
} else if (keywords.length) {
result += meta('keywords', keywords.map(function(tag) {
return tag.name ? tag.name : tag;
}).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);
result += og('og:description', description, false);
}

images = images.map(function(path) {
Expand All @@ -95,7 +108,7 @@ function openGraphHelper(options) {
});

images.forEach(function(path) {
result += og('og:image', path);
result += og('og:image', path, false);
});

if (updated) {
Expand All @@ -107,11 +120,11 @@ function openGraphHelper(options) {
result += meta('twitter:card', twitterCard);
result += meta('twitter:title', title);
if (description) {
result += meta('twitter:description', description);
result += meta('twitter:description', description, false);
}

if (images.length) {
result += meta('twitter:image', images[0]);
result += meta('twitter:image', images[0], false);
}

if (options.twitter_id) {
Expand All @@ -122,7 +135,7 @@ function openGraphHelper(options) {
}

if (options.twitter_site) {
result += meta('twitter:site', options.twitter_site);
result += meta('twitter:site', options.twitter_site, false);
}

if (options.google_plus) {
Expand Down
119 changes: 119 additions & 0 deletions test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,123 @@ describe('open_graph', () => {
result.should.not.contain(meta({property: 'description'}));
});

it('keywords - page keywords string', () => {
var ctx = {
page: { keywords: 'optimize,web' },
config: {},
is_post: isPost
};

var result = openGraph.call(ctx);
var escaped = 'optimize,web';

result.should.contain(meta({name: 'keywords', content: escaped}));
});

it('keywords - page keywords array', () => {
var ctx = {
page: { keywords: ['optimize','web'] },
config: {},
is_post: isPost
};

var result = openGraph.call(ctx);
var keywords = 'optimize,web';

result.should.contain(meta({name: 'keywords', content: keywords}));
});

it('keywords - page tags', () => {
var ctx = {
page: { tags: ['optimize','web'] },
config: {},
is_post: isPost
};

var result = openGraph.call(ctx);
var keywords = 'optimize,web';

result.should.contain(meta({name: 'keywords', content: keywords}));
});

it('keywords - config keywords string', () => {
var ctx = {
page: {},
config: { keywords: 'optimize,web' },
is_post: isPost
};

var result = openGraph.call(ctx);
var keywords = 'optimize,web';

result.should.contain(meta({name: 'keywords', content: keywords}));
});

it('keywords - config keywords array', () => {
var ctx = {
page: {},
config: { keywords: ['optimize', 'web'] },
is_post: isPost
};

var result = openGraph.call(ctx);
var keywords = 'optimize,web';

result.should.contain(meta({name: 'keywords', content: keywords}));
});

it('keywords - page keywords first', () => {
var ctx = {
page: {
keywords: ['web1', 'web2'],
tags: ['web3', 'web4']
},
config: { keywords: 'web5,web6' },
is_post: isPost
};

var result = openGraph.call(ctx);
var keywords = 'web1,web2';

result.should.contain(meta({name: 'keywords', content: keywords}));
});

it('keywords - page tags second', () => {
var ctx = {
page: { tags: ['optimize','web'] },
config: { keywords: 'web5,web6' },
is_post: isPost
};

var result = openGraph.call(ctx);
var keywords = 'optimize,web';

result.should.contain(meta({name: 'keywords', content: keywords}));
});

it('keywords - page tags empty', () => {
var ctx = {
page: { tags: [] },
config: { keywords: 'web5,web6' },
is_post: isPost
};

var result = openGraph.call(ctx);
var keywords = 'web5,web6';

result.should.contain(meta({name: 'keywords', content: keywords}));
});

it('keywords - escape', () => {
var ctx = {
page: { keywords: 'optimize,web&<>"\'/,site' },
config: {},
is_post: isPost
};

var result = openGraph.call(ctx);
var keywords = 'optimize,web&amp;&lt;&gt;&quot;&#39;&#x2F;,site';

result.should.contain(meta({name: 'keywords', content: keywords}));
});
});

0 comments on commit 1bb19c8

Please sign in to comment.