diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index 0af4ef2c8b..8fd03f8394 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -2,24 +2,16 @@ const urlFn = require('url'); const moment = require('moment'); -const { escapeHTML, htmlTag, stripHTML } = require('hexo-util'); - -function meta(name, content, escape) { - if (escape !== false && typeof content === 'string') { - content = escapeHTML(content); - } +const { encodeURL, htmlTag, stripHTML } = require('hexo-util'); +function meta(name, content) { return `${htmlTag('meta', { name, content })}\n`; } -function og(name, content, escape) { - if (escape !== false && typeof content === 'string') { - content = escapeHTML(content); - } - +function og(name, content) { return `${htmlTag('meta', { property: name, content @@ -70,7 +62,7 @@ function openGraphHelper(options = {}) { let result = ''; if (description) { - result += meta('description', description, false); + result += meta('description', description); } if (keywords) { @@ -86,19 +78,21 @@ 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$/, ''); + if (url) { + if (config.pretty_urls.trailing_index === false) { + url = url.replace(/index\.html$/, ''); + } + url = encodeURL(url); } - - result += og('og:url', url, false); + result += og('og:url', url); result += og('og:site_name', siteName); if (description) { - result += og('og:description', description, false); + result += og('og:description', description); } if (language) { - result += og('og:locale', language, false); + result += og('og:locale', language); } images = images.map(path => { @@ -112,7 +106,7 @@ function openGraphHelper(options = {}) { }); images.forEach(path => { - result += og('og:image', path, false); + result += og('og:image', path); }); if (updated) { @@ -124,7 +118,7 @@ function openGraphHelper(options = {}) { result += meta('twitter:card', twitterCard); if (images.length) { - result += meta('twitter:image', images[0], false); + result += meta('twitter:image', images[0]); } if (options.twitter_id) { @@ -135,7 +129,7 @@ function openGraphHelper(options = {}) { } if (options.twitter_site) { - result += meta('twitter:site', options.twitter_site, false); + result += meta('twitter:site', options.twitter_site); } if (options.google_plus) { diff --git a/package.json b/package.json index 962c8e04fb..298392a387 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "hexo-fs": "^2.0.0", "hexo-i18n": "^1.0.0", "hexo-log": "^1.0.0", - "hexo-util": "^1.3.1", + "hexo-util": "^1.4.0", "js-yaml": "^3.12.0", "lodash": "^4.17.11", "micromatch": "^4.0.2", diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index 648046512e..d9c484a712 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -129,6 +129,19 @@ describe('open_graph', () => { hexo.config.pretty_urls.trailing_index = true; }); + it('url - IDN', () => { + const ctx = { + page: {}, + config: hexo.config, + is_post: isPost, + url: 'https://foô.com/bár' + }; + + const result = openGraph.call(ctx); + + result.should.contain(meta({property: 'og:url', content: 'https://xn--fo-9ja.com/b%C3%A1r'})); + }); + it('images - content', () => { const result = openGraph.call({ page: { diff --git a/test/scripts/tags/asset_img.js b/test/scripts/tags/asset_img.js index 0adc7e60cb..d8f0709c09 100644 --- a/test/scripts/tags/asset_img.js +++ b/test/scripts/tags/asset_img.js @@ -50,7 +50,7 @@ describe('asset_img', () => { }); it('default', () => { - assetImg('bar title').should.eql(''); + assetImg('bar "a title"').should.eql(''); }); it('with space', () => { @@ -60,13 +60,13 @@ describe('asset_img', () => { }); it('with alt and title', () => { - assetImgTag.call(post, ['bar', '"title"', '"alt"']) - .should.eql('alt'); + assetImgTag.call(post, ['bar', '"a title"', '"an alt"']) + .should.eql('an alt'); }); it('with width height alt and title', () => { - assetImgTag.call(post, ['bar', '100', '200', '"title"', '"alt"']) - .should.eql('alt'); + assetImgTag.call(post, ['bar', '100', '200', '"a title"', '"an alt"']) + .should.eql('an alt'); }); it('no slug', () => {