From 2285a1578f1ba052cd9287046f57f0cf29629340 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 27 Oct 2019 10:20:14 +0800 Subject: [PATCH 1/4] feat(open_graph): add article:author & article:tag --- lib/plugins/helper/open_graph.js | 25 ++++++++------ test/scripts/helpers/open_graph.js | 53 ++++++++++++++++++++++++------ 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index 4fc4011a92..4f9f70267b 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -33,6 +33,7 @@ function openGraphHelper(options = {}) { const date = options.date !== false ? options.date || page.date : false; const updated = options.updated !== false ? options.updated || page.updated : false; const language = options.language || page.lang || page.language || config.language; + const author = options.author || config.author; if (!Array.isArray(images)) images = [images]; @@ -66,16 +67,6 @@ function openGraphHelper(options = {}) { result += meta('description', description); } - if (keywords) { - if (typeof keywords === 'string') { - result += meta('keywords', keywords); - } else if (keywords.length) { - result += meta('keywords', keywords.map(tag => { - return tag.name ? tag.name : tag; - }).filter(keyword => !!keyword).join()); - } - } - result += og('og:type', type); result += og('og:title', title); @@ -122,6 +113,20 @@ function openGraphHelper(options = {}) { } } + if (author) { + result += og('article:author', author); + } + + if (keywords) { + if (typeof keywords === 'string') { + result += og('article:tag', keywords); + } else if (keywords.length) { + result += og('article:tag', keywords.map(tag => { + return tag.name ? tag.name : tag; + }).filter(keyword => !!keyword).join()); + } + } + result += meta('twitter:card', twitterCard); if (images.length) { diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index a9b93ca8bd..b04d2ffdbb 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -31,7 +31,6 @@ describe('open_graph', () => { config: hexo.config, is_post: isPost }).should.eql([ - meta({name: 'keywords', content: 'optimize,web'}), meta({property: 'og:type', content: 'website'}), meta({property: 'og:title', content: hexo.config.title}), meta({property: 'og:url'}), @@ -39,6 +38,8 @@ describe('open_graph', () => { meta({property: 'og:locale', content: 'en'}), meta({property: 'article:published_time', content: post.date.toISOString()}), meta({property: 'article:modified_time', content: post.updated.toISOString()}), + meta({property: 'article:author', content: hexo.config.author}), + meta({property: 'article:tag', content: 'optimize,web'}), meta({name: 'twitter:card', content: 'summary'}) ].join('\n')); @@ -480,7 +481,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); const escaped = 'optimize,web'; - result.should.contain(meta({name: 'keywords', content: escaped})); + result.should.contain(meta({property: 'article:tag', content: escaped})); }); it('keywords - page keywords array', () => { @@ -493,7 +494,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); const keywords = 'optimize,web'; - result.should.contain(meta({name: 'keywords', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords})); }); it('keywords - page tags', () => { @@ -506,7 +507,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); const keywords = 'optimize,web'; - result.should.contain(meta({name: 'keywords', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords})); }); it('keywords - config keywords string', () => { @@ -520,7 +521,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); const keywords = 'optimize,web'; - result.should.contain(meta({name: 'keywords', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords})); }); it('keywords - config keywords array', () => { @@ -534,7 +535,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); const keywords = 'optimize,web'; - result.should.contain(meta({name: 'keywords', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords})); }); it('keywords - page keywords first', () => { @@ -551,7 +552,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); const keywords = 'web1,web2'; - result.should.contain(meta({name: 'keywords', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords})); }); it('keywords - page tags second', () => { @@ -565,7 +566,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); const keywords = 'optimize,web'; - result.should.contain(meta({name: 'keywords', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords})); }); it('keywords - page tags empty', () => { @@ -579,7 +580,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); const keywords = 'web5,web6'; - result.should.contain(meta({name: 'keywords', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords})); }); it('keywords - escape', () => { @@ -592,7 +593,7 @@ describe('open_graph', () => { const result = openGraph.call(ctx); const keywords = 'optimize,web&<>"'/,site'; - result.should.contain(meta({name: 'keywords', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords})); }); it('og:locale - options.language', () => { @@ -646,4 +647,36 @@ describe('open_graph', () => { result.should.not.contain(meta({property: 'og:locale'})); }); + + it('article:author - options.author', () => { + const result = openGraph.call({ + page: {}, + config: hexo.config, + is_post: isPost + }, {author: 'Jane Doe'}); + + result.should.contain(meta({property: 'article:author', content: 'Jane Doe'})); + }); + + it('article:author - config.language', () => { + hexo.config.language = 'es-pa'; + + const result = openGraph.call({ + page: {}, + config: hexo.config, + is_post: isPost + }); + + result.should.contain(meta({property: 'article:author', content: 'John Doe'})); + }); + + it('article:author - no author set', () => { + const result = openGraph.call({ + page: {}, + config: { author: undefined }, + is_post: isPost + }); + + result.should.not.contain(meta({property: 'og:locale'})); + }); }); From 665a9fa49ad685794d30153b5e5d7ab18aa7a255 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 27 Oct 2019 12:37:33 +0800 Subject: [PATCH 2/4] refactor(open_graph): article:tag into ogp array --- lib/plugins/helper/open_graph.js | 16 +++++------ test/scripts/helpers/open_graph.js | 46 ++++++++++++++++++------------ 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index 4f9f70267b..f93d6e5807 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -24,7 +24,7 @@ function openGraphHelper(options = {}) { const { content } = page; let images = options.image || options.images || page.photos || []; let description = options.description || page.description || page.excerpt || content || config.description; - const keywords = page.keywords || (page.tags && page.tags.length ? page.tags : undefined) || config.keywords; + 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; @@ -118,13 +118,13 @@ function openGraphHelper(options = {}) { } if (keywords) { - if (typeof keywords === 'string') { - result += og('article:tag', keywords); - } else if (keywords.length) { - result += og('article:tag', keywords.map(tag => { - return tag.name ? tag.name : tag; - }).filter(keyword => !!keyword).join()); - } + if (typeof keywords === 'string') keywords = keywords.split(','); + + keywords.map(tag => { + return tag.name ? tag.name : tag; + }).filter(keyword => !!keyword).forEach(keyword => { + result += og('article:tag', keyword); + }); } result += meta('twitter:card', twitterCard); diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index b04d2ffdbb..7940fa43df 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -479,9 +479,10 @@ describe('open_graph', () => { }; const result = openGraph.call(ctx); - const escaped = 'optimize,web'; + const escaped = ['optimize', 'web']; - result.should.contain(meta({property: 'article:tag', content: escaped})); + result.should.contain(meta({property: 'article:tag', content: escaped[0]})); + result.should.contain(meta({property: 'article:tag', content: escaped[1]})); }); it('keywords - page keywords array', () => { @@ -492,9 +493,10 @@ describe('open_graph', () => { }; const result = openGraph.call(ctx); - const keywords = 'optimize,web'; + const keywords = ['optimize', 'web']; - result.should.contain(meta({property: 'article:tag', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords[0]})); + result.should.contain(meta({property: 'article:tag', content: keywords[1]})); }); it('keywords - page tags', () => { @@ -505,9 +507,10 @@ describe('open_graph', () => { }; const result = openGraph.call(ctx); - const keywords = 'optimize,web'; + const keywords = ['optimize', 'web']; - result.should.contain(meta({property: 'article:tag', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords[0]})); + result.should.contain(meta({property: 'article:tag', content: keywords[1]})); }); it('keywords - config keywords string', () => { @@ -519,9 +522,10 @@ describe('open_graph', () => { }; const result = openGraph.call(ctx); - const keywords = 'optimize,web'; + const keywords = ['optimize', 'web']; - result.should.contain(meta({property: 'article:tag', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords[0]})); + result.should.contain(meta({property: 'article:tag', content: keywords[1]})); }); it('keywords - config keywords array', () => { @@ -533,9 +537,10 @@ describe('open_graph', () => { }; const result = openGraph.call(ctx); - const keywords = 'optimize,web'; + const keywords = ['optimize', 'web']; - result.should.contain(meta({property: 'article:tag', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords[0]})); + result.should.contain(meta({property: 'article:tag', content: keywords[1]})); }); it('keywords - page keywords first', () => { @@ -550,9 +555,10 @@ describe('open_graph', () => { }; const result = openGraph.call(ctx); - const keywords = 'web1,web2'; + const keywords = ['web1', 'web2']; - result.should.contain(meta({property: 'article:tag', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords[0]})); + result.should.contain(meta({property: 'article:tag', content: keywords[1]})); }); it('keywords - page tags second', () => { @@ -564,9 +570,10 @@ describe('open_graph', () => { }; const result = openGraph.call(ctx); - const keywords = 'optimize,web'; + const keywords = ['optimize', 'web']; - result.should.contain(meta({property: 'article:tag', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords[0]})); + result.should.contain(meta({property: 'article:tag', content: keywords[1]})); }); it('keywords - page tags empty', () => { @@ -578,9 +585,10 @@ describe('open_graph', () => { }; const result = openGraph.call(ctx); - const keywords = 'web5,web6'; + const keywords = ['web5', 'web6']; - result.should.contain(meta({property: 'article:tag', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords[0]})); + result.should.contain(meta({property: 'article:tag', content: keywords[1]})); }); it('keywords - escape', () => { @@ -591,9 +599,11 @@ describe('open_graph', () => { }; const result = openGraph.call(ctx); - const keywords = 'optimize,web&<>"'/,site'; + const keywords = 'optimize,web&<>"'/,site'.split(','); - result.should.contain(meta({property: 'article:tag', content: keywords})); + result.should.contain(meta({property: 'article:tag', content: keywords[0]})); + result.should.contain(meta({property: 'article:tag', content: keywords[1]})); + result.should.contain(meta({property: 'article:tag', content: keywords[2]})); }); it('og:locale - options.language', () => { From 0c62cc10fb8502c504955c1dfa1da3d9a4175167 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 27 Oct 2019 14:03:42 +0800 Subject: [PATCH 3/4] test: fix typo --- test/scripts/helpers/open_graph.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index 7940fa43df..6f85fa55ab 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -687,6 +687,6 @@ describe('open_graph', () => { is_post: isPost }); - result.should.not.contain(meta({property: 'og:locale'})); + result.should.not.contain(meta({property: 'article:author'})); }); }); From 947bc4992e16633ea2263ec5beb7fb73e9b0353a Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 27 Oct 2019 16:59:05 +0800 Subject: [PATCH 4/4] refactor(open_graph): simplify --- lib/plugins/helper/open_graph.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index f93d6e5807..436109114c 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -122,7 +122,7 @@ function openGraphHelper(options = {}) { keywords.map(tag => { return tag.name ? tag.name : tag; - }).filter(keyword => !!keyword).forEach(keyword => { + }).filter(Boolean).forEach(keyword => { result += og('article:tag', keyword); }); }