diff --git a/includes/helpers/page.js b/includes/helpers/page.js index 4da91a0..a44c37b 100644 --- a/includes/helpers/page.js +++ b/includes/helpers/page.js @@ -8,6 +8,7 @@ * <%- meta(post) %> * <%- has_thumbnail(post) %> * <%- get_thumbnail(post) %> +* <%- get_og_image(post) %> */ module.exports = function (hexo) { function trim(str) { @@ -93,4 +94,29 @@ module.exports = function (hexo) { const hasThumbnail = hexo.extend.helper.get('has_thumbnail').bind(this)(post); return this.url_for(hasThumbnail ? post.thumbnail : 'images/thumbnail.svg'); }); -} \ No newline at end of file + + hexo.extend.helper.register('has_og_image', function (post) { + return post.hasOwnProperty('og_image'); + }); + + hexo.extend.helper.register('get_og_image', function (post) { + const getConfig = hexo.extend.helper.get('get_config').bind(this); + const hasConfig = hexo.extend.helper.get('has_config').bind(this); + + const hasOGImage = hexo.extend.helper.get('has_og_image').bind(this)(post); + const hasThumbnail = hexo.extend.helper.get('has_thumbnail').bind(this)(post); + + const getThumbnail = hexo.extend.helper.get('get_thumbnail').bind(this); + + let og_image + + if(hasOGImage) + og_image = post.og_image + else if (hasThumbnail) + og_image = getThumbnail(post); + else + og_image = getConfig('article.og_image', '/images/og_image.png'); + + return this.url_for(og_image); + }); +} diff --git a/layout/common/head.ejs b/layout/common/head.ejs index 307e2c7..f8ddefd 100644 --- a/layout/common/head.ejs +++ b/layout/common/head.ejs @@ -4,17 +4,14 @@ <%- meta(page) %> <% if (has_config('open_graph')) { %> - <% const og_options = { + <%- open_graph({ twitter_id: get_config('open_graph.twitter_id'), twitter_site: get_config('open_graph.twitter_site'), google_plus: get_config('open_graph.google_plus'), fb_admins: get_config('open_graph.fb_admins'), - fb_app_id: get_config('open_graph.fb_app_id') - }; - if (has_thumbnail(page)) { - og_options['image'] = get_thumbnail(page); - } %> - <%- open_graph(og_options) %> + fb_app_id: get_config('open_graph.fb_app_id'), + image: get_og_image(page) + }) %> <% } %> <% if (has_config('canonical_url')) { %> diff --git a/source/images/og_image.png b/source/images/og_image.png new file mode 100644 index 0000000..3aa9340 Binary files /dev/null and b/source/images/og_image.png differ