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

refactor(helper/open_graph): use whatwg url api #5007

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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