Update layout/head.ejs, improve og image supoort (#440)

This commit is contained in:
Liyang Zhang 2019-03-28 03:33:52 +08:00 committed by Ruipeng Zhang
parent 0a35069a8b
commit d674e6b61c
3 changed files with 31 additions and 8 deletions

View File

@ -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');
});
}
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);
});
}

View File

@ -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')) { %>

BIN
source/images/og_image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB