From 58243a2ee7bc1f08eb1bb6c47ac35ba0dea7897a Mon Sep 17 00:00:00 2001 From: KFL Date: Mon, 9 Jan 2017 00:11:00 -0800 Subject: [PATCH 1/5] Fix issue #942 --- lib/plugins/helper/open_graph.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index 718b31ee1a..d97807764f 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -85,8 +85,10 @@ function openGraphHelper(options) { images = images.map(function(path) { if (!urlFn.parse(path).host) { - if (path[0] !== '/') path = '/' + path; - return config.url + path; + if (path[0] !== '/') { + // resolve `path`'s absolute path relative to current page's url + return urlFn.resolve(url, path); + } } return path; From f5e15e0b524cf31daaf2ba23fbc3891c47d0b53e Mon Sep 17 00:00:00 2001 From: KFL Date: Mon, 9 Jan 2017 00:36:33 -0800 Subject: [PATCH 2/5] Fix test cases --- lib/plugins/helper/open_graph.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index d97807764f..8180a956dc 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -85,10 +85,9 @@ function openGraphHelper(options) { images = images.map(function(path) { if (!urlFn.parse(path).host) { - if (path[0] !== '/') { - // resolve `path`'s absolute path relative to current page's url - return urlFn.resolve(url, path); - } + // resolve `path`'s absolute path relative to current page's url + // `path` can be both absolute (starts with `/`) or relative. + return urlFn.resolve(url || config.url, path); } return path; From 3562a796856bd2ea1ed9c559c1154dab33a9f860 Mon Sep 17 00:00:00 2001 From: KFL Date: Mon, 9 Jan 2017 00:52:27 -0800 Subject: [PATCH 3/5] Add test coverage --- test/scripts/helpers/open_graph.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index 7fd948682c..59a8c39bea 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -217,6 +217,23 @@ describe('open_graph', function() { result.should.contain(meta({property: 'og:image', content: hexo.config.url + '/test.jpg'})); }); + it('images - resolve relative path when site is hosted in subdirectory', function() { + var urlFn = require('url'); + var config = hexo.config; + config.url = urlFn.resolve(config.url, "blog"); + config.root = "/blog/"; + var postUrl = urlFn.resolve(config.url, "/foo/bar/index.html"); + + var result = openGraph.call({ + page: {}, + config, + is_post: isPost, + url: postUrl + }, {images: 'test.jpg'}); + + result.should.contain(meta({property: 'og:image', content: urlFn.resolve(config.url, "/foo/bar/test.jpg")})); + }); + it('site_name - options', function() { var result = openGraph.call({ page: {}, From e075c7819cf2d687f74a7d88315904635f27e5d7 Mon Sep 17 00:00:00 2001 From: KFL Date: Mon, 9 Jan 2017 00:57:51 -0800 Subject: [PATCH 4/5] Fix eslint --- test/scripts/helpers/open_graph.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index 59a8c39bea..ec0fa72d1d 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -226,7 +226,7 @@ describe('open_graph', function() { var result = openGraph.call({ page: {}, - config, + config: config, is_post: isPost, url: postUrl }, {images: 'test.jpg'}); From 91f620bb730d3571874d7a947674f99faf2d56c8 Mon Sep 17 00:00:00 2001 From: KFL Date: Mon, 9 Jan 2017 01:04:18 -0800 Subject: [PATCH 5/5] Fix jscs failure --- test/scripts/helpers/open_graph.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index ec0fa72d1d..7e042b0af7 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -220,9 +220,9 @@ describe('open_graph', function() { it('images - resolve relative path when site is hosted in subdirectory', function() { var urlFn = require('url'); var config = hexo.config; - config.url = urlFn.resolve(config.url, "blog"); - config.root = "/blog/"; - var postUrl = urlFn.resolve(config.url, "/foo/bar/index.html"); + config.url = urlFn.resolve(config.url, 'blog'); + config.root = 'blog'; + var postUrl = urlFn.resolve(config.url, '/foo/bar/index.html'); var result = openGraph.call({ page: {}, @@ -231,7 +231,7 @@ describe('open_graph', function() { url: postUrl }, {images: 'test.jpg'}); - result.should.contain(meta({property: 'og:image', content: urlFn.resolve(config.url, "/foo/bar/test.jpg")})); + result.should.contain(meta({property: 'og:image', content: urlFn.resolve(config.url, '/foo/bar/test.jpg')})); }); it('site_name - options', function() {