Skip to content

Commit

Permalink
fix(open_graph): null-handle keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Mar 10, 2020
1 parent a1e30c9 commit 8cef984
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function openGraphHelper(options = {}) {
const { content } = page;
let images = options.image || options.images || page.photos || [];
let description = options.description || page.description || page.excerpt || content || config.description;
let keywords = (page.tags && page.tags.length ? page.tags : undefined) || config.keywords;
let keywords = (page.tags && page.tags.length ? page.tags : undefined) || config.keywords || false;
const title = options.title || page.title || config.title;
const type = options.type || (this.is_post() ? 'article' : 'website');
const url = prettyUrls(options.url || this.url, config.pretty_urls);
Expand Down
22 changes: 20 additions & 2 deletions test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const moment = require('moment');
const cheerio = require('cheerio');
const { encodeURL } = require('hexo-util');
const defaultConfig = require('../../../lib/hexo/default_config');

describe('open_graph', () => {
const Hexo = require('../../../lib/hexo');
Expand All @@ -17,10 +18,15 @@ describe('open_graph', () => {
}

before(() => {
hexo.config.permalink = ':title';
return hexo.init();
});

beforeEach(() => {
// Reset config
hexo.config = { ...defaultConfig };
hexo.config.permalink = ':title';
});

it('default', async () => {
let post = await Post.insert({
source: 'foo.md',
Expand Down Expand Up @@ -586,7 +592,7 @@ describe('open_graph', () => {
result.should.have.string(meta({property: 'article:tag', content: keywords[1]}));
});

it('keywords - page tags empty', () => {
it('keywords - use config.keywords if no tags', () => {
hexo.config.keywords = ['web5', 'web6'];
const ctx = {
page: { tags: [] },
Expand All @@ -601,6 +607,18 @@ describe('open_graph', () => {
result.should.have.string(meta({property: 'article:tag', content: keywords[1]}));
});

it('keywords - null', () => {
const ctx = {
page: {},
config: hexo.config,
is_post: isPost
};

const result = openGraph.call(ctx);

result.should.not.have.string('<meta property="article:tag"');
});

it('keywords - escape', () => {
const ctx = {
page: { tags: ['optimize', 'web&<>"\'/', 'site'] },
Expand Down

0 comments on commit 8cef984

Please sign in to comment.