Skip to content

Commit

Permalink
feat(open_graph): add article:author & article:tag (#3805)
Browse files Browse the repository at this point in the history
meta[keyword] is dropped in this commit.
  • Loading branch information
SukkaW committed Oct 27, 2019
2 parents 17c2bbb + 947bc49 commit 182fdf4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 30 deletions.
27 changes: 16 additions & 11 deletions lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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;
const keywords = page.keywords || (page.tags && page.tags.length ? page.tags : undefined) || config.keywords;
let keywords = page.keywords || (page.tags && page.tags.length ? page.tags : undefined) || config.keywords;
const title = options.title || page.title || config.title;
const type = options.type || (this.is_post() ? 'article' : 'website');
let url = options.url || this.url;
Expand All @@ -33,6 +33,7 @@ function openGraphHelper(options = {}) {
const date = options.date !== false ? options.date || page.date : false;
const updated = options.updated !== false ? options.updated || page.updated : false;
const language = options.language || page.lang || page.language || config.language;
const author = options.author || config.author;

if (!Array.isArray(images)) images = [images];

Expand Down Expand Up @@ -66,16 +67,6 @@ function openGraphHelper(options = {}) {
result += meta('description', description);
}

if (keywords) {
if (typeof keywords === 'string') {
result += meta('keywords', keywords);
} else if (keywords.length) {
result += meta('keywords', keywords.map(tag => {
return tag.name ? tag.name : tag;
}).filter(keyword => !!keyword).join());
}
}

result += og('og:type', type);
result += og('og:title', title);

Expand Down Expand Up @@ -122,6 +113,20 @@ function openGraphHelper(options = {}) {
}
}

if (author) {
result += og('article:author', author);
}

if (keywords) {
if (typeof keywords === 'string') keywords = keywords.split(',');

keywords.map(tag => {
return tag.name ? tag.name : tag;
}).filter(Boolean).forEach(keyword => {
result += og('article:tag', keyword);
});
}

result += meta('twitter:card', twitterCard);

if (images.length) {
Expand Down
81 changes: 62 additions & 19 deletions test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ describe('open_graph', () => {
config: hexo.config,
is_post: isPost
}).should.eql([
meta({name: 'keywords', content: 'optimize,web'}),
meta({property: 'og:type', content: 'website'}),
meta({property: 'og:title', content: hexo.config.title}),
meta({property: 'og:url'}),
meta({property: 'og:site_name', content: hexo.config.title}),
meta({property: 'og:locale', content: 'en'}),
meta({property: 'article:published_time', content: post.date.toISOString()}),
meta({property: 'article:modified_time', content: post.updated.toISOString()}),
meta({property: 'article:author', content: hexo.config.author}),
meta({property: 'article:tag', content: 'optimize,web'}),
meta({name: 'twitter:card', content: 'summary'})
].join('\n'));

Expand Down Expand Up @@ -478,9 +479,10 @@ describe('open_graph', () => {
};

const result = openGraph.call(ctx);
const escaped = 'optimize,web';
const escaped = ['optimize', 'web'];

result.should.contain(meta({name: 'keywords', content: escaped}));
result.should.contain(meta({property: 'article:tag', content: escaped[0]}));
result.should.contain(meta({property: 'article:tag', content: escaped[1]}));
});

it('keywords - page keywords array', () => {
Expand All @@ -491,9 +493,10 @@ describe('open_graph', () => {
};

const result = openGraph.call(ctx);
const keywords = 'optimize,web';
const keywords = ['optimize', 'web'];

result.should.contain(meta({name: 'keywords', content: keywords}));
result.should.contain(meta({property: 'article:tag', content: keywords[0]}));
result.should.contain(meta({property: 'article:tag', content: keywords[1]}));
});

it('keywords - page tags', () => {
Expand All @@ -504,9 +507,10 @@ describe('open_graph', () => {
};

const result = openGraph.call(ctx);
const keywords = 'optimize,web';
const keywords = ['optimize', 'web'];

result.should.contain(meta({name: 'keywords', content: keywords}));
result.should.contain(meta({property: 'article:tag', content: keywords[0]}));
result.should.contain(meta({property: 'article:tag', content: keywords[1]}));
});

it('keywords - config keywords string', () => {
Expand All @@ -518,9 +522,10 @@ describe('open_graph', () => {
};

const result = openGraph.call(ctx);
const keywords = 'optimize,web';
const keywords = ['optimize', 'web'];

result.should.contain(meta({name: 'keywords', content: keywords}));
result.should.contain(meta({property: 'article:tag', content: keywords[0]}));
result.should.contain(meta({property: 'article:tag', content: keywords[1]}));
});

it('keywords - config keywords array', () => {
Expand All @@ -532,9 +537,10 @@ describe('open_graph', () => {
};

const result = openGraph.call(ctx);
const keywords = 'optimize,web';
const keywords = ['optimize', 'web'];

result.should.contain(meta({name: 'keywords', content: keywords}));
result.should.contain(meta({property: 'article:tag', content: keywords[0]}));
result.should.contain(meta({property: 'article:tag', content: keywords[1]}));
});

it('keywords - page keywords first', () => {
Expand All @@ -549,9 +555,10 @@ describe('open_graph', () => {
};

const result = openGraph.call(ctx);
const keywords = 'web1,web2';
const keywords = ['web1', 'web2'];

result.should.contain(meta({name: 'keywords', content: keywords}));
result.should.contain(meta({property: 'article:tag', content: keywords[0]}));
result.should.contain(meta({property: 'article:tag', content: keywords[1]}));
});

it('keywords - page tags second', () => {
Expand All @@ -563,9 +570,10 @@ describe('open_graph', () => {
};

const result = openGraph.call(ctx);
const keywords = 'optimize,web';
const keywords = ['optimize', 'web'];

result.should.contain(meta({name: 'keywords', content: keywords}));
result.should.contain(meta({property: 'article:tag', content: keywords[0]}));
result.should.contain(meta({property: 'article:tag', content: keywords[1]}));
});

it('keywords - page tags empty', () => {
Expand All @@ -577,9 +585,10 @@ describe('open_graph', () => {
};

const result = openGraph.call(ctx);
const keywords = 'web5,web6';
const keywords = ['web5', 'web6'];

result.should.contain(meta({name: 'keywords', content: keywords}));
result.should.contain(meta({property: 'article:tag', content: keywords[0]}));
result.should.contain(meta({property: 'article:tag', content: keywords[1]}));
});

it('keywords - escape', () => {
Expand All @@ -590,9 +599,11 @@ describe('open_graph', () => {
};

const result = openGraph.call(ctx);
const keywords = 'optimize,web&<>"'/,site';
const keywords = 'optimize,web&<>"'/,site'.split(',');

result.should.contain(meta({name: 'keywords', content: keywords}));
result.should.contain(meta({property: 'article:tag', content: keywords[0]}));
result.should.contain(meta({property: 'article:tag', content: keywords[1]}));
result.should.contain(meta({property: 'article:tag', content: keywords[2]}));
});

it('og:locale - options.language', () => {
Expand Down Expand Up @@ -646,4 +657,36 @@ describe('open_graph', () => {

result.should.not.contain(meta({property: 'og:locale'}));
});

it('article:author - options.author', () => {
const result = openGraph.call({
page: {},
config: hexo.config,
is_post: isPost
}, {author: 'Jane Doe'});

result.should.contain(meta({property: 'article:author', content: 'Jane Doe'}));
});

it('article:author - config.language', () => {
hexo.config.language = 'es-pa';

const result = openGraph.call({
page: {},
config: hexo.config,
is_post: isPost
});

result.should.contain(meta({property: 'article:author', content: 'John Doe'}));
});

it('article:author - no author set', () => {
const result = openGraph.call({
page: {},
config: { author: undefined },
is_post: isPost
});

result.should.not.contain(meta({property: 'article:author'}));
});
});

0 comments on commit 182fdf4

Please sign in to comment.