Skip to content

Commit

Permalink
Merge pull request #3661 from curbengh/ograph-canonical
Browse files Browse the repository at this point in the history
fix(open_graph): remove index.html from url
  • Loading branch information
curbengh authored Sep 27, 2019
2 parents ffe4eaa + 7f61736 commit af99506
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
8 changes: 7 additions & 1 deletion lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function openGraphHelper(options = {}) {
const keywords = page.keywords || (page.tags && page.tags.length ? page.tags : undefined) || config.keywords;
const title = options.title || page.title || config.title;
const type = options.type || (this.is_post() ? 'article' : 'website');
const url = options.url || this.url;
let url = options.url || this.url;
const siteName = options.site_name || config.title;
const twitterCard = options.twitter_card || 'summary';
const updated = options.updated !== false ? options.updated || page.updated : false;
Expand Down Expand Up @@ -85,7 +85,13 @@ 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$/, '');
}

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

result += og('og:site_name', siteName);
if (description) {
result += og('og:description', description, false);
Expand Down
48 changes: 35 additions & 13 deletions test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const moment = require('moment');
const cheerio = require('cheerio');

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

it('url - should not ends with index.html', () => {
hexo.config.pretty_urls.trailing_index = false;
const result = openGraph.call({
page: {},
config: hexo.config,
is_post: isPost,
url: 'http://yoursite.com/page/index.html'
});

const $ = cheerio.load(result);

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

hexo.config.pretty_urls.trailing_index = true;
});

it('images - content', () => {
const result = openGraph.call({
page: {
Expand Down Expand Up @@ -401,7 +418,7 @@ describe('open_graph', () => {
it('updated - options', () => {
const result = openGraph.call({
page: { updated: moment('2016-05-23T21:20:21.372Z') },
config: {},
config: hexo.config,
is_post: isPost
}, { });

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

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

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

Expand All @@ -442,7 +459,7 @@ describe('open_graph', () => {
it('keywords - page keywords string', () => {
const ctx = {
page: { keywords: 'optimize,web' },
config: {},
config: hexo.config,
is_post: isPost
};

Expand All @@ -455,7 +472,7 @@ describe('open_graph', () => {
it('keywords - page keywords array', () => {
const ctx = {
page: { keywords: ['optimize', 'web'] },
config: {},
config: hexo.config,
is_post: isPost
};

Expand All @@ -468,7 +485,7 @@ describe('open_graph', () => {
it('keywords - page tags', () => {
const ctx = {
page: { tags: ['optimize', 'web'] },
config: {},
config: hexo.config,
is_post: isPost
};

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

it('keywords - config keywords string', () => {
hexo.config.keywords = 'optimize,web';
const ctx = {
page: {},
config: { keywords: 'optimize,web' },
config: hexo.config,
is_post: isPost
};

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

it('keywords - config keywords array', () => {
hexo.config.keywords = ['optimize', 'web'];
const ctx = {
page: {},
config: { keywords: ['optimize', 'web'] },
config: hexo.config,
is_post: isPost
};

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

it('keywords - page keywords first', () => {
hexo.config.keywords = 'web5,web6';
const ctx = {
page: {
keywords: ['web1', 'web2'],
tags: ['web3', 'web4']
},
config: { keywords: 'web5,web6' },
config: hexo.config,
is_post: isPost
};

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

it('keywords - page tags second', () => {
hexo.config.keywords = 'web5,web6';
const ctx = {
page: { tags: ['optimize', 'web'] },
config: { keywords: 'web5,web6' },
config: hexo.config,
is_post: isPost
};

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

it('keywords - page tags empty', () => {
hexo.config.keywords = 'web5,web6';
const ctx = {
page: { tags: [] },
config: { keywords: 'web5,web6' },
config: hexo.config,
is_post: isPost
};

Expand All @@ -549,7 +571,7 @@ describe('open_graph', () => {
it('keywords - escape', () => {
const ctx = {
page: { keywords: 'optimize,web&<>"\'/,site' },
config: {},
config: hexo.config,
is_post: isPost
};

Expand Down

0 comments on commit af99506

Please sign in to comment.