Skip to content

Commit

Permalink
Merge pull request #3686 from curbengh/url-encoding
Browse files Browse the repository at this point in the history
fix(open_graph): percent-encode url, not html escape
  • Loading branch information
curbengh authored Oct 8, 2019
2 parents 18d459f + 6f7fe0c commit d2662d4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
36 changes: 15 additions & 21 deletions lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@

const urlFn = require('url');
const moment = require('moment');
const { escapeHTML, htmlTag, stripHTML } = require('hexo-util');

function meta(name, content, escape) {
if (escape !== false && typeof content === 'string') {
content = escapeHTML(content);
}
const { encodeURL, htmlTag, stripHTML } = require('hexo-util');

function meta(name, content) {
return `${htmlTag('meta', {
name,
content
})}\n`;
}

function og(name, content, escape) {
if (escape !== false && typeof content === 'string') {
content = escapeHTML(content);
}

function og(name, content) {
return `${htmlTag('meta', {
property: name,
content
Expand Down Expand Up @@ -70,7 +62,7 @@ function openGraphHelper(options = {}) {
let result = '';

if (description) {
result += meta('description', description, false);
result += meta('description', description);
}

if (keywords) {
Expand All @@ -86,19 +78,21 @@ function openGraphHelper(options = {}) {
result += og('og:type', type);
result += og('og:title', title);

if (config.pretty_urls.trailing_index === false) {
url = url.replace(/index\.html$/, '');
if (url) {
if (config.pretty_urls.trailing_index === false) {
url = url.replace(/index\.html$/, '');
}
url = encodeURL(url);
}

result += og('og:url', url, false);
result += og('og:url', url);

result += og('og:site_name', siteName);
if (description) {
result += og('og:description', description, false);
result += og('og:description', description);
}

if (language) {
result += og('og:locale', language, false);
result += og('og:locale', language);
}

images = images.map(path => {
Expand All @@ -112,7 +106,7 @@ function openGraphHelper(options = {}) {
});

images.forEach(path => {
result += og('og:image', path, false);
result += og('og:image', path);
});

if (updated) {
Expand All @@ -124,7 +118,7 @@ function openGraphHelper(options = {}) {
result += meta('twitter:card', twitterCard);

if (images.length) {
result += meta('twitter:image', images[0], false);
result += meta('twitter:image', images[0]);
}

if (options.twitter_id) {
Expand All @@ -135,7 +129,7 @@ function openGraphHelper(options = {}) {
}

if (options.twitter_site) {
result += meta('twitter:site', options.twitter_site, false);
result += meta('twitter:site', options.twitter_site);
}

if (options.google_plus) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"hexo-fs": "^2.0.0",
"hexo-i18n": "^1.0.0",
"hexo-log": "^1.0.0",
"hexo-util": "^1.3.1",
"hexo-util": "^1.4.0",
"js-yaml": "^3.12.0",
"lodash": "^4.17.11",
"micromatch": "^4.0.2",
Expand Down
13 changes: 13 additions & 0 deletions test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ describe('open_graph', () => {
hexo.config.pretty_urls.trailing_index = true;
});

it('url - IDN', () => {
const ctx = {
page: {},
config: hexo.config,
is_post: isPost,
url: 'https://foô.com/bár'
};

const result = openGraph.call(ctx);

result.should.contain(meta({property: 'og:url', content: 'https://xn--fo-9ja.com/b%C3%A1r'}));
});

it('images - content', () => {
const result = openGraph.call({
page: {
Expand Down
10 changes: 5 additions & 5 deletions test/scripts/tags/asset_img.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('asset_img', () => {
});

it('default', () => {
assetImg('bar title').should.eql('<img src="/foo/bar" class="" title="title">');
assetImg('bar "a title"').should.eql('<img src="/foo/bar" class="" title="a title">');
});

it('with space', () => {
Expand All @@ -60,13 +60,13 @@ describe('asset_img', () => {
});

it('with alt and title', () => {
assetImgTag.call(post, ['bar', '"title"', '"alt"'])
.should.eql('<img src="/foo/bar" class="" title="title" alt="alt">');
assetImgTag.call(post, ['bar', '"a title"', '"an alt"'])
.should.eql('<img src="/foo/bar" class="" title="a title" alt="an alt">');
});

it('with width height alt and title', () => {
assetImgTag.call(post, ['bar', '100', '200', '"title"', '"alt"'])
.should.eql('<img src="/foo/bar" class="" width="100" height="200" title="title" alt="alt">');
assetImgTag.call(post, ['bar', '100', '200', '"a title"', '"an alt"'])
.should.eql('<img src="/foo/bar" class="" width="100" height="200" title="a title" alt="an alt">');
});

it('no slug', () => {
Expand Down

0 comments on commit d2662d4

Please sign in to comment.