Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open graph fully absolute urls support #947

Closed
4 changes: 2 additions & 2 deletions assets/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ email:
language:

# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
## If your site is put in a 'child' subdirectory, set url as 'http://yoursite.com' and root as '/child/'
url: http://yoursite.com
root: /
permalink: :year/:month/:day/:title/
Expand Down Expand Up @@ -85,4 +85,4 @@ exclude_generator:
# Deployment
## Docs: http://hexo.io/docs/deployment.html
deploy:
type:
type:
4 changes: 4 additions & 0 deletions lib/loaders/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ module.exports = function(callback){
var config = hexo.config = _.extend(defaults, result);
hexo.env.init = true;

if (config.root.charAt(0) !== '/'){
config.root = '/' + config.root;
}

if (_.last(config.root) !== '/'){
config.root += '/';
}
Expand Down
4 changes: 2 additions & 2 deletions lib/model/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var isEndWith = function(str, last){
};

var permalinkGetter = function(){
var url = hexo.config.url;
var url = hexo.config.url + hexo.config.root;

return url + (isEndWith(url, '/') ? '' : '/') + this.path;
};
Expand Down Expand Up @@ -144,4 +144,4 @@ var Asset = exports.Asset = new Schema({
modified: {type: Boolean, default: true},
post_id: {type: String, ref: 'Post'},
post_path: {type: String}
});
});
6 changes: 3 additions & 3 deletions lib/plugins/filter/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ module.exports = function(data, callback){
// Exit if the link doesn't have protocol, which means it's a internal link
if (!data.protocol) return;

// Exit if the url is started with site url
if (data.hostname === config.url) return match;
// Exit if the url is on the site fomain
if (data.hostname === url.parse(config.url).hostname) return match;

$(this)
.attr('target', '_blank')
Expand All @@ -32,4 +32,4 @@ module.exports = function(data, callback){
data.content = $.html();

callback(null, data);
};
};
4 changes: 2 additions & 2 deletions lib/plugins/helper/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ exports.search_form = function(opts){
return '<form action="//google.com/search" method="get" accept-charset="UTF-8" class="' + options.class + '">' +
'<input type="search" name="q" results="0" class="' + options.class + '-input"' + (options.text ? ' placeholder="' + options.text + '"' : '') + '>' +
(options.button ? '<input type="submit" value="' + (typeof options.button === 'string' ? options.button : options.text) + '" class="' + options.class + '-submit">' : '') +
'<input type="hidden" name="q" value="site:' + config.url + '">' +
'<input type="hidden" name="q" value="site:' + config.url + config.root + '">' +
'</form>';
};
};
38 changes: 23 additions & 15 deletions lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function(options){
var page = this.page,
config = this.config || hexo.config,
content = page.content,
images = page.photos || [];
self = this;

var description = page.description || '';

Expand All @@ -44,15 +44,6 @@ module.exports = function(options){
.replace(/\"/g, '\'')
.replace(/\>/g, "&amp;");

if (!images.length && content){
var $ = cheerio.load(content);

$('img').each(function(){
var src = $(this).attr('src');
if (src) images.push(src);
});
}

var data = _.extend({
title: page.title || config.title,
type: this.is_post() ? 'article' : 'website',
Expand All @@ -70,17 +61,34 @@ module.exports = function(options){

var str = '';

if (!data.image){
var images = page.photos || [];
if (!images.length && content){
var $ = cheerio.load(content);

$('img').each(function(){
var src = $(this).attr('src');
if (src) images.push(src);
});
}
data.image = images;
}

if (!_.isArray(data.image)){
data.image = [data.image];
}

data.image.forEach(function(image){
str += metaTag('og:image', self.url_for(image, {absolute: true}));
});

str += metaTag('description', data.description);
str += metaTag('og:type', data.type);
str += metaTag('og:title', data.title);
str += metaTag('og:url', data.url);
str += metaTag('og:site_name', data.site_name);
str += metaTag('og:description', data.description);

images.forEach(function(image){
str += metaTag('og:image', image);
});

str += metaTag('twitter:card', data.twitter_card);
str += metaTag('twitter:title', data.title);
str += metaTag('twitter:description', data.description);
Expand Down Expand Up @@ -109,4 +117,4 @@ module.exports = function(options){
}

return str;
};
};
8 changes: 6 additions & 2 deletions lib/plugins/helper/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ exports.relative_url = function(from, to){
return out.join('/');
};

exports.url_for = function(path){
exports.url_for = function(path, options){
path = path || '/';
absolute = options && 'absolute' in options ? options.absolute : false;

var config = this.config || hexo.config,
root = config.root,
data = url.parse(path);

if (!data.protocol && path.substring(0, 2) !== '//'){
if (config.relative_link){
if (!absolute && config.relative_link){
path = this.relative_url(this.path, path);
} else {
if (path.substring(0, root.length) !== root){
Expand All @@ -57,6 +58,9 @@ exports.url_for = function(path){
path = root + path;
}
}
if (absolute) {
path = config.url + path;
}
}
}

Expand Down
26 changes: 25 additions & 1 deletion test/scripts/helper/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ describe('url_for', function(){
}, 'index.html').should.eql('../../index.html');
});

it('internal url (absolute)', function(){
url_for.call({
config: {url: 'http://hexo.io', root: '/'},
relative_url: relative_url
}, 'index.html', {absolute:true}).should.eql('http://hexo.io/index.html');

url_for.call({
config: {url: 'http://hexo.io', root: '/blog/'},
relative_url: relative_url
}, 'index.html', {absolute:true}).should.eql('http://hexo.io/blog/index.html');

url_for.call({
config: {url: 'http://hexo.io', root: '/', relative_link: true},
path: '',
relative_url: relative_url
}, 'index.html', {absolute:true}).should.eql('http://hexo.io/index.html');

url_for.call({
config: {url: 'http://hexo.io', root: '/', relative_link: true},
path: 'foo/bar/',
relative_url: relative_url
}, 'index.html', {absolute:true}).should.eql('http://hexo.io/index.html');
});

it('external url', function(){
[
'http://zespia.tw/',
Expand All @@ -57,4 +81,4 @@ describe('url_for', function(){
}, url).should.eql(url);
});
});
});
});