Skip to content

Commit

Permalink
refactor(helper/open_graph): use whatwg url api (#5007)
Browse files Browse the repository at this point in the history
Co-authored-by: Sukka <isukkaw@gmail.com>

Co-authored-by: Sukka <isukkaw@gmail.com>
  • Loading branch information
renbaoshuo and SukkaW committed Jun 24, 2022
1 parent 24db105 commit 3bd5f2b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
16 changes: 2 additions & 14 deletions lib/plugins/helper/open_graph.js
@@ -1,6 +1,5 @@
'use strict';

const { parse, resolve } = require('url');
const { isMoment, isDate } = require('moment');
const { encodeURL, prettyUrls, htmlTag, stripHTML, escapeHTML } = require('hexo-util');
const { default: moize } = require('moize');
Expand Down Expand Up @@ -56,7 +55,6 @@ const og = (name, content, escape) => {
};

function openGraphHelper(options = {}) {

const { config, page } = this;
const { content } = page;
let images = options.image || options.images || page.photos || [];
Expand Down Expand Up @@ -117,15 +115,7 @@ function openGraphHelper(options = {}) {
result += og('og:locale', localeToTerritory(language), false);
}

images = images.map(path => {
if (!parse(path).host) {
// resolve `path`'s absolute path relative to current page's url
// `path` can be both absolute (starts with `/`) or relative.
return resolve(url || config.url, path);
}

return path;
});
images = images.map(path => new URL(path, url || config.url).toString());

images.forEach(path => {
result += og('og:image', path, false);
Expand Down Expand Up @@ -161,9 +151,7 @@ function openGraphHelper(options = {}) {

if (options.twitter_image) {
let twitter_image = options.twitter_image;
if (!parse(twitter_image).host) {
twitter_image = resolve(url || config.url, twitter_image);
}
twitter_image = new URL(twitter_image, url || config.url);
result += meta('twitter:image', twitter_image, false);
} else if (images.length) {
result += meta('twitter:image', images[0], false);
Expand Down
7 changes: 3 additions & 4 deletions test/scripts/helpers/open_graph.js
Expand Up @@ -301,11 +301,10 @@ describe('open_graph', () => {
});

it('images - resolve relative path when site is hosted in subdirectory', () => {
const urlFn = require('url');
const config = hexo.config;
config.url = urlFn.resolve(config.url, 'blog');
config.url = new URL('blog', config.url).toString();
config.root = 'blog';
const postUrl = urlFn.resolve(config.url, '/foo/bar/index.html');
const postUrl = new URL('/foo/bar/index.html', config.url).toString();

const result = openGraph.call({
page: {},
Expand All @@ -314,7 +313,7 @@ describe('open_graph', () => {
url: postUrl
}, {images: 'test.jpg'});

result.should.have.string(meta({property: 'og:image', content: urlFn.resolve(config.url, '/foo/bar/test.jpg')}));
result.should.have.string(meta({property: 'og:image', content: new URL('/foo/bar/test.jpg', config.url).toString()}));
});

it('twitter_image - default same as og:image', () => {
Expand Down

0 comments on commit 3bd5f2b

Please sign in to comment.