From a9b3b32f08ff1464bb6a827e34f3f53c76f5bf2c Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Wed, 18 Dec 2019 03:13:55 +0000 Subject: [PATCH 1/3] fix(open_graph-helper): pass all pretty_urls options - by utilizing prettyUrls() of hexo-util - to support pretty_urls.trailing_html --- lib/plugins/helper/open_graph.js | 14 ++++----- test/scripts/helpers/open_graph.js | 46 ++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index c4dad87ff3..1efd62bd5f 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -1,8 +1,14 @@ 'use strict'; +<<<<<<< HEAD const { parse, resolve } = require('url'); const { isMoment, isDate } = require('moment'); const { encodeURL, htmlTag, stripHTML, escapeHTML } = require('hexo-util'); +======= +const urlFn = require('url'); +const moment = require('moment'); +const { htmlTag, prettyUrls, stripHTML, escapeHTML } = require('hexo-util'); +>>>>>>> fix(open_graph-helper): pass all pretty_urls options const localeMap = { 'en': 'en_US', 'de': 'de_DE', @@ -44,7 +50,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.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; @@ -82,12 +88,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); diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index 6e5b3779b0..c384c1453c 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -2,7 +2,7 @@ const moment = require('moment'); const cheerio = require('cheerio'); -const { encodeURL } = require('hexo-util'); +const { escapeHTML } = require('hexo-util'); describe('open_graph', () => { const Hexo = require('../../../lib/hexo'); @@ -34,7 +34,7 @@ describe('open_graph', () => { }).should.eql([ meta({property: 'og:type', content: 'website'}), meta({property: 'og:title', content: hexo.config.title}), - meta({property: 'og:url'}), + meta({property: 'og:url', content: escapeHTML(hexo.config.url)}), meta({property: 'og:site_name', content: hexo.config.title}), meta({property: 'og:locale', content: 'en_US'}), meta({property: 'article:published_time', content: post.date.toISOString()}), @@ -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: {}, @@ -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: {}, @@ -143,7 +179,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); - result.should.contain(meta({property: 'og:url', content: encodeURL(ctx.url)})); + result.should.contain(meta({property: 'og:url', content: escapeHTML(ctx.url)})); }); it('images - content', () => { From 678d7c808b1af4e611d9463bd6baaf840d8a7d78 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Wed, 18 Dec 2019 04:41:19 +0000 Subject: [PATCH 2/3] fix(open_graph): incorrect usage of config.url --- lib/plugins/helper/open_graph.js | 11 +++-------- test/scripts/helpers/open_graph.js | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index 1efd62bd5f..bb228e90f9 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -1,14 +1,9 @@ 'use strict'; -<<<<<<< HEAD const { parse, resolve } = require('url'); const { isMoment, isDate } = require('moment'); -const { encodeURL, htmlTag, stripHTML, escapeHTML } = require('hexo-util'); -======= -const urlFn = require('url'); -const moment = require('moment'); -const { htmlTag, prettyUrls, stripHTML, escapeHTML } = require('hexo-util'); ->>>>>>> fix(open_graph-helper): pass all pretty_urls options +const { prettyUrls, htmlTag, stripHTML, escapeHTML } = require('hexo-util'); + const localeMap = { 'en': 'en_US', 'de': 'de_DE', @@ -50,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'); - const url = prettyUrls(options.url || this.url || config.url, config.pretty_urls); + 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; diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index c384c1453c..b6f88cba3e 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -34,7 +34,7 @@ describe('open_graph', () => { }).should.eql([ meta({property: 'og:type', content: 'website'}), meta({property: 'og:title', content: hexo.config.title}), - meta({property: 'og:url', content: escapeHTML(hexo.config.url)}), + meta({property: 'og:url'}), meta({property: 'og:site_name', content: hexo.config.title}), meta({property: 'og:locale', content: 'en_US'}), meta({property: 'article:published_time', content: post.date.toISOString()}), From 859bf771d74483b2eb682c675a50d59d237e7167 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Thu, 19 Dec 2019 23:14:55 +0000 Subject: [PATCH 3/3] test(open_graph): encodeURL link --- test/scripts/helpers/open_graph.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index b6f88cba3e..8eace68ef0 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -2,7 +2,7 @@ const moment = require('moment'); const cheerio = require('cheerio'); -const { escapeHTML } = require('hexo-util'); +const { encodeURL } = require('hexo-util'); describe('open_graph', () => { const Hexo = require('../../../lib/hexo'); @@ -179,7 +179,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); - result.should.contain(meta({property: 'og:url', content: escapeHTML(ctx.url)})); + result.should.contain(meta({property: 'og:url', content: encodeURL(ctx.url)})); }); it('images - content', () => {