Skip to content

Commit af99506

Browse files
authored
Merge pull request #3661 from curbengh/ograph-canonical
fix(open_graph): remove index.html from url
2 parents ffe4eaa + 7f61736 commit af99506

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

lib/plugins/helper/open_graph.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function openGraphHelper(options = {}) {
3535
const keywords = page.keywords || (page.tags && page.tags.length ? page.tags : undefined) || config.keywords;
3636
const title = options.title || page.title || config.title;
3737
const type = options.type || (this.is_post() ? 'article' : 'website');
38-
const url = options.url || this.url;
38+
let url = options.url || this.url;
3939
const siteName = options.site_name || config.title;
4040
const twitterCard = options.twitter_card || 'summary';
4141
const updated = options.updated !== false ? options.updated || page.updated : false;
@@ -85,7 +85,13 @@ function openGraphHelper(options = {}) {
8585

8686
result += og('og:type', type);
8787
result += og('og:title', title);
88+
89+
if (config.pretty_urls.trailing_index === false) {
90+
url = url.replace(/index\.html$/, '');
91+
}
92+
8893
result += og('og:url', url, false);
94+
8995
result += og('og:site_name', siteName);
9096
if (description) {
9197
result += og('og:description', description, false);

test/scripts/helpers/open_graph.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const moment = require('moment');
4+
const cheerio = require('cheerio');
45

56
describe('open_graph', () => {
67
const Hexo = require('../../../lib/hexo');
@@ -112,6 +113,22 @@ describe('open_graph', () => {
112113
result.should.contain(meta({property: 'og:url', content: 'https://hexo.io/bar'}));
113114
});
114115

116+
it('url - should not ends with index.html', () => {
117+
hexo.config.pretty_urls.trailing_index = false;
118+
const result = openGraph.call({
119+
page: {},
120+
config: hexo.config,
121+
is_post: isPost,
122+
url: 'http://yoursite.com/page/index.html'
123+
});
124+
125+
const $ = cheerio.load(result);
126+
127+
$('meta[property="og:url"]').attr('content').endsWith('index.html').should.be.false;
128+
129+
hexo.config.pretty_urls.trailing_index = true;
130+
});
131+
115132
it('images - content', () => {
116133
const result = openGraph.call({
117134
page: {
@@ -401,7 +418,7 @@ describe('open_graph', () => {
401418
it('updated - options', () => {
402419
const result = openGraph.call({
403420
page: { updated: moment('2016-05-23T21:20:21.372Z') },
404-
config: {},
421+
config: hexo.config,
405422
is_post: isPost
406423
}, { });
407424

@@ -411,7 +428,7 @@ describe('open_graph', () => {
411428
it('updated - options - allow overriding og:updated_time', () => {
412429
const result = openGraph.call({
413430
page: { updated: moment('2016-05-23T21:20:21.372Z') },
414-
config: {},
431+
config: hexo.config,
415432
is_post: isPost
416433
}, { updated: moment('2015-04-22T20:19:20.371Z') });
417434

@@ -421,7 +438,7 @@ describe('open_graph', () => {
421438
it('updated - options - allow disabling og:updated_time', () => {
422439
const result = openGraph.call({
423440
page: { updated: moment('2016-05-23T21:20:21.372Z') },
424-
config: {},
441+
config: hexo.config,
425442
is_post: isPost
426443
}, { updated: false });
427444

@@ -431,7 +448,7 @@ describe('open_graph', () => {
431448
it('description - do not add /(?:og:)?description/ meta tags if there is no description', () => {
432449
const result = openGraph.call({
433450
page: { },
434-
config: {},
451+
config: hexo.config,
435452
is_post: isPost
436453
}, { });
437454

@@ -442,7 +459,7 @@ describe('open_graph', () => {
442459
it('keywords - page keywords string', () => {
443460
const ctx = {
444461
page: { keywords: 'optimize,web' },
445-
config: {},
462+
config: hexo.config,
446463
is_post: isPost
447464
};
448465

@@ -455,7 +472,7 @@ describe('open_graph', () => {
455472
it('keywords - page keywords array', () => {
456473
const ctx = {
457474
page: { keywords: ['optimize', 'web'] },
458-
config: {},
475+
config: hexo.config,
459476
is_post: isPost
460477
};
461478

@@ -468,7 +485,7 @@ describe('open_graph', () => {
468485
it('keywords - page tags', () => {
469486
const ctx = {
470487
page: { tags: ['optimize', 'web'] },
471-
config: {},
488+
config: hexo.config,
472489
is_post: isPost
473490
};
474491

@@ -479,9 +496,10 @@ describe('open_graph', () => {
479496
});
480497

481498
it('keywords - config keywords string', () => {
499+
hexo.config.keywords = 'optimize,web';
482500
const ctx = {
483501
page: {},
484-
config: { keywords: 'optimize,web' },
502+
config: hexo.config,
485503
is_post: isPost
486504
};
487505

@@ -492,9 +510,10 @@ describe('open_graph', () => {
492510
});
493511

494512
it('keywords - config keywords array', () => {
513+
hexo.config.keywords = ['optimize', 'web'];
495514
const ctx = {
496515
page: {},
497-
config: { keywords: ['optimize', 'web'] },
516+
config: hexo.config,
498517
is_post: isPost
499518
};
500519

@@ -505,12 +524,13 @@ describe('open_graph', () => {
505524
});
506525

507526
it('keywords - page keywords first', () => {
527+
hexo.config.keywords = 'web5,web6';
508528
const ctx = {
509529
page: {
510530
keywords: ['web1', 'web2'],
511531
tags: ['web3', 'web4']
512532
},
513-
config: { keywords: 'web5,web6' },
533+
config: hexo.config,
514534
is_post: isPost
515535
};
516536

@@ -521,9 +541,10 @@ describe('open_graph', () => {
521541
});
522542

523543
it('keywords - page tags second', () => {
544+
hexo.config.keywords = 'web5,web6';
524545
const ctx = {
525546
page: { tags: ['optimize', 'web'] },
526-
config: { keywords: 'web5,web6' },
547+
config: hexo.config,
527548
is_post: isPost
528549
};
529550

@@ -534,9 +555,10 @@ describe('open_graph', () => {
534555
});
535556

536557
it('keywords - page tags empty', () => {
558+
hexo.config.keywords = 'web5,web6';
537559
const ctx = {
538560
page: { tags: [] },
539-
config: { keywords: 'web5,web6' },
561+
config: hexo.config,
540562
is_post: isPost
541563
};
542564

@@ -549,7 +571,7 @@ describe('open_graph', () => {
549571
it('keywords - escape', () => {
550572
const ctx = {
551573
page: { keywords: 'optimize,web&<>"\'/,site' },
552-
config: {},
574+
config: hexo.config,
553575
is_post: isPost
554576
};
555577

0 commit comments

Comments
 (0)