Skip to content

Commit 04d34a7

Browse files
authored
Merge pull request #3983 from curbengh/og-pretty-urls
fix(open_graph-helper): pass all pretty_urls options
2 parents 56593ca + 859bf77 commit 04d34a7

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

lib/plugins/helper/open_graph.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const { parse, resolve } = require('url');
44
const { isMoment, isDate } = require('moment');
5-
const { encodeURL, htmlTag, stripHTML, escapeHTML } = require('hexo-util');
5+
const { prettyUrls, htmlTag, stripHTML, escapeHTML } = require('hexo-util');
6+
67
const localeMap = {
78
'en': 'en_US',
89
'de': 'de_DE',
@@ -44,7 +45,7 @@ function openGraphHelper(options = {}) {
4445
let keywords = page.keywords || (page.tags && page.tags.length ? page.tags : undefined) || config.keywords;
4546
const title = options.title || page.title || config.title;
4647
const type = options.type || (this.is_post() ? 'article' : 'website');
47-
let url = options.url || this.url;
48+
const url = prettyUrls(options.url || this.url, config.pretty_urls);
4849
const siteName = options.site_name || config.title;
4950
const twitterCard = options.twitter_card || 'summary';
5051
const date = options.date !== false ? options.date || page.date : false;
@@ -82,12 +83,6 @@ function openGraphHelper(options = {}) {
8283
result += og('og:type', type);
8384
result += og('og:title', title);
8485

85-
if (url) {
86-
if (config.pretty_urls.trailing_index === false) {
87-
url = url.replace(/index\.html$/, '');
88-
}
89-
url = encodeURL(url);
90-
}
9186
result += og('og:url', url);
9287

9388
result += og('og:site_name', siteName);

test/scripts/helpers/open_graph.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe('open_graph', () => {
117117
result.should.contain(meta({property: 'og:url', content: 'https://hexo.io/bar'}));
118118
});
119119

120-
it('url - should not ends with index.html', () => {
120+
it('url - pretty_urls.trailing_index', () => {
121121
hexo.config.pretty_urls.trailing_index = false;
122122
const result = openGraph.call({
123123
page: {},
@@ -128,11 +128,47 @@ describe('open_graph', () => {
128128

129129
const $ = cheerio.load(result);
130130

131-
$('meta[property="og:url"]').attr('content').endsWith('index.html').should.be.false;
131+
$('meta[property="og:url"]').attr('content').endsWith('index.html').should.eql(false);
132132

133133
hexo.config.pretty_urls.trailing_index = true;
134134
});
135135

136+
it('url - pretty_urls.trailing_html', () => {
137+
hexo.config.pretty_urls.trailing_html = false;
138+
const result = openGraph.call({
139+
page: {},
140+
config: hexo.config,
141+
is_post: isPost,
142+
url: 'http://yoursite.com/page/about.html'
143+
});
144+
145+
const $ = cheerio.load(result);
146+
147+
$('meta[property="og:url"]').attr('content').endsWith('.html').should.eql(false);
148+
149+
hexo.config.pretty_urls.trailing_html = true;
150+
});
151+
152+
it('url - null pretty_urls', () => {
153+
hexo.config.pretty_urls = null;
154+
const url = 'http://yoursite.com/page/about.html';
155+
const result = openGraph.call({
156+
page: {},
157+
config: hexo.config,
158+
is_post: isPost,
159+
url
160+
});
161+
162+
const $ = cheerio.load(result);
163+
164+
$('meta[property="og:url"]').attr('content').should.eql(url);
165+
166+
hexo.config.pretty_urls = {
167+
trailing_index: true,
168+
trailing_html: true
169+
};
170+
});
171+
136172
it('url - IDN', () => {
137173
const ctx = {
138174
page: {},

0 commit comments

Comments
 (0)