Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(open_graph-helper): pass all pretty_urls options #3983

Merged
merged 3 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 3 additions & 8 deletions lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const { parse, resolve } = require('url');
const { isMoment, isDate } = require('moment');
const { encodeURL, htmlTag, stripHTML, escapeHTML } = require('hexo-util');
const { prettyUrls, htmlTag, stripHTML, escapeHTML } = require('hexo-util');

const localeMap = {
'en': 'en_US',
'de': 'de_DE',
Expand Down Expand Up @@ -44,7 +45,7 @@ function openGraphHelper(options = {}) {
let 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');
let url = options.url || this.url;
const url = prettyUrls(options.url || this.url, config.pretty_urls);
const siteName = options.site_name || config.title;
const twitterCard = options.twitter_card || 'summary';
const date = options.date !== false ? options.date || page.date : false;
Expand Down Expand Up @@ -82,12 +83,6 @@ function openGraphHelper(options = {}) {
result += og('og:type', type);
result += og('og:title', title);

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

result += og('og:site_name', siteName);
Expand Down
40 changes: 38 additions & 2 deletions test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('open_graph', () => {
result.should.contain(meta({property: 'og:url', content: 'https://hexo.io/bar'}));
});

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

const $ = cheerio.load(result);

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

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

it('url - pretty_urls.trailing_html', () => {
hexo.config.pretty_urls.trailing_html = false;
const result = openGraph.call({
page: {},
config: hexo.config,
is_post: isPost,
url: 'http://yoursite.com/page/about.html'
});

const $ = cheerio.load(result);

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

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

it('url - null pretty_urls', () => {
hexo.config.pretty_urls = null;
const url = 'http://yoursite.com/page/about.html';
const result = openGraph.call({
page: {},
config: hexo.config,
is_post: isPost,
url
});

const $ = cheerio.load(result);

$('meta[property="og:url"]').attr('content').should.eql(url);

hexo.config.pretty_urls = {
trailing_index: true,
trailing_html: true
};
});

it('url - IDN', () => {
const ctx = {
page: {},
Expand Down