Skip to content

Commit

Permalink
feat(open_graph): different URLs for og:image and twitter:image (#4748)
Browse files Browse the repository at this point in the history
  • Loading branch information
KentarouTakeda committed Sep 22, 2021
1 parent 0c979bf commit a2ec6b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ function openGraphHelper(options = {}) {

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

if (images.length) {
if (options.twitter_image) {
let twitter_image = options.twitter_image;
if (!parse(twitter_image).host) {
twitter_image = resolve(url || config.url, twitter_image);
}
result += meta('twitter:image', twitter_image, false);
} else if (images.length) {
result += meta('twitter:image', images[0], false);
}

Expand Down
30 changes: 30 additions & 0 deletions test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,36 @@ describe('open_graph', () => {
result.should.have.string(meta({property: 'og:image', content: urlFn.resolve(config.url, '/foo/bar/test.jpg')}));
});

it('twitter_image - default same as og:image', () => {
const result = openGraph.call({
page: {},
config: hexo.config,
is_post: isPost
}, {images: 'image.jpg'});

result.should.have.string(meta({name: 'twitter:image', content: hexo.config.url + '/image.jpg'}));
});

it('twitter_image - different URLs for og:image and twitter:image', () => {
const result = openGraph.call({
page: {},
config: hexo.config,
is_post: isPost
}, {twitter_image: 'twitter.jpg', images: 'image.jpg'});

result.should.have.string(meta({name: 'twitter:image', content: hexo.config.url + '/twitter.jpg'}));
});

it('images - twitter_image absolute url', () => {
const result = openGraph.call({
page: {},
config: hexo.config,
is_post: isPost
}, {twitter_image: 'https://hexo.io/twitter.jpg', images: 'image.jpg'});

result.should.have.string(meta({name: 'twitter:image', content: 'https://hexo.io/twitter.jpg'}));
});

it('site_name - options', () => {
const result = openGraph.call({
page: {},
Expand Down

0 comments on commit a2ec6b2

Please sign in to comment.