Skip to content

Commit d2662d4

Browse files
authored
Merge pull request #3686 from curbengh/url-encoding
fix(open_graph): percent-encode url, not html escape
2 parents 18d459f + 6f7fe0c commit d2662d4

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

lib/plugins/helper/open_graph.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,16 @@
22

33
const urlFn = require('url');
44
const moment = require('moment');
5-
const { escapeHTML, htmlTag, stripHTML } = require('hexo-util');
6-
7-
function meta(name, content, escape) {
8-
if (escape !== false && typeof content === 'string') {
9-
content = escapeHTML(content);
10-
}
5+
const { encodeURL, htmlTag, stripHTML } = require('hexo-util');
116

7+
function meta(name, content) {
128
return `${htmlTag('meta', {
139
name,
1410
content
1511
})}\n`;
1612
}
1713

18-
function og(name, content, escape) {
19-
if (escape !== false && typeof content === 'string') {
20-
content = escapeHTML(content);
21-
}
22-
14+
function og(name, content) {
2315
return `${htmlTag('meta', {
2416
property: name,
2517
content
@@ -70,7 +62,7 @@ function openGraphHelper(options = {}) {
7062
let result = '';
7163

7264
if (description) {
73-
result += meta('description', description, false);
65+
result += meta('description', description);
7466
}
7567

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

89-
if (config.pretty_urls.trailing_index === false) {
90-
url = url.replace(/index\.html$/, '');
81+
if (url) {
82+
if (config.pretty_urls.trailing_index === false) {
83+
url = url.replace(/index\.html$/, '');
84+
}
85+
url = encodeURL(url);
9186
}
92-
93-
result += og('og:url', url, false);
87+
result += og('og:url', url);
9488

9589
result += og('og:site_name', siteName);
9690
if (description) {
97-
result += og('og:description', description, false);
91+
result += og('og:description', description);
9892
}
9993

10094
if (language) {
101-
result += og('og:locale', language, false);
95+
result += og('og:locale', language);
10296
}
10397

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

114108
images.forEach(path => {
115-
result += og('og:image', path, false);
109+
result += og('og:image', path);
116110
});
117111

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

126120
if (images.length) {
127-
result += meta('twitter:image', images[0], false);
121+
result += meta('twitter:image', images[0]);
128122
}
129123

130124
if (options.twitter_id) {
@@ -135,7 +129,7 @@ function openGraphHelper(options = {}) {
135129
}
136130

137131
if (options.twitter_site) {
138-
result += meta('twitter:site', options.twitter_site, false);
132+
result += meta('twitter:site', options.twitter_site);
139133
}
140134

141135
if (options.google_plus) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"hexo-fs": "^2.0.0",
4646
"hexo-i18n": "^1.0.0",
4747
"hexo-log": "^1.0.0",
48-
"hexo-util": "^1.3.1",
48+
"hexo-util": "^1.4.0",
4949
"js-yaml": "^3.12.0",
5050
"lodash": "^4.17.11",
5151
"micromatch": "^4.0.2",

test/scripts/helpers/open_graph.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ describe('open_graph', () => {
129129
hexo.config.pretty_urls.trailing_index = true;
130130
});
131131

132+
it('url - IDN', () => {
133+
const ctx = {
134+
page: {},
135+
config: hexo.config,
136+
is_post: isPost,
137+
url: 'https://foô.com/bár'
138+
};
139+
140+
const result = openGraph.call(ctx);
141+
142+
result.should.contain(meta({property: 'og:url', content: 'https://xn--fo-9ja.com/b%C3%A1r'}));
143+
});
144+
132145
it('images - content', () => {
133146
const result = openGraph.call({
134147
page: {

test/scripts/tags/asset_img.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('asset_img', () => {
5050
});
5151

5252
it('default', () => {
53-
assetImg('bar title').should.eql('<img src="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/foo/bar" class="" title="title">');
53+
assetImg('bar "a title"').should.eql('<img src="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/foo/bar" class="" title="a title">');
5454
});
5555

5656
it('with space', () => {
@@ -60,13 +60,13 @@ describe('asset_img', () => {
6060
});
6161

6262
it('with alt and title', () => {
63-
assetImgTag.call(post, ['bar', '"title"', '"alt"'])
64-
.should.eql('<img src="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/foo/bar" class="" title="title" alt="alt">');
63+
assetImgTag.call(post, ['bar', '"a title"', '"an alt"'])
64+
.should.eql('<img src="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/foo/bar" class="" title="a title" alt="an alt">');
6565
});
6666

6767
it('with width height alt and title', () => {
68-
assetImgTag.call(post, ['bar', '100', '200', '"title"', '"alt"'])
69-
.should.eql('<img src="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/foo/bar" class="" width="100" height="200" title="title" alt="alt">');
68+
assetImgTag.call(post, ['bar', '100', '200', '"a title"', '"an alt"'])
69+
.should.eql('<img src="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/foo/bar" class="" width="100" height="200" title="a title" alt="an alt">');
7070
});
7171

7272
it('no slug', () => {

0 commit comments

Comments
 (0)