Skip to content

Commit

Permalink
fix(open_graph): locale must in language_TERRITORY format
Browse files Browse the repository at this point in the history
BREAKING CHANGE: og:locale won't be added if language value is not in 'language-territory' format
  • Loading branch information
curbengh committed Oct 27, 2019
1 parent 7398d30 commit 3c4f394
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/plugins/helper/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function openGraphHelper(options = {}) {
const twitterCard = options.twitter_card || 'summary';
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;
let language = options.language || page.lang || page.language || config.language;

if (!Array.isArray(images)) images = [images];

Expand Down Expand Up @@ -93,7 +93,9 @@ function openGraphHelper(options = {}) {
}

if (language) {
result += og('og:locale', language);
language = language.replace('-', '_');

if (language.length === 5) result += og('og:locale', language);
}

images = images.map(path => {
Expand Down
22 changes: 17 additions & 5 deletions test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ describe('open_graph', () => {
meta({property: 'og:title', content: hexo.config.title}),
meta({property: 'og:url'}),
meta({property: 'og:site_name', content: hexo.config.title}),
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({name: 'twitter:card', content: 'summary'})
Expand Down Expand Up @@ -602,7 +601,7 @@ describe('open_graph', () => {
is_post: isPost
}, {language: 'es-cr'});

result.should.contain(meta({property: 'og:locale', content: 'es-cr'}));
result.should.contain(meta({property: 'og:locale', content: 'es_cr'}));
});

it('og:locale - page.lang', () => {
Expand All @@ -612,7 +611,7 @@ describe('open_graph', () => {
is_post: isPost
});

result.should.contain(meta({property: 'og:locale', content: 'es-mx'}));
result.should.contain(meta({property: 'og:locale', content: 'es_mx'}));
});

it('og:locale - page.language', () => {
Expand All @@ -622,7 +621,7 @@ describe('open_graph', () => {
is_post: isPost
});

result.should.contain(meta({property: 'og:locale', content: 'es-gt'}));
result.should.contain(meta({property: 'og:locale', content: 'es_gt'}));
});

it('og:locale - config.language', () => {
Expand All @@ -634,7 +633,7 @@ describe('open_graph', () => {
is_post: isPost
});

result.should.contain(meta({property: 'og:locale', content: 'es-pa'}));
result.should.contain(meta({property: 'og:locale', content: 'es_pa'}));
});

it('og:locale - no language set', () => {
Expand All @@ -646,4 +645,17 @@ describe('open_graph', () => {

result.should.not.contain(meta({property: 'og:locale'}));
});

it('og:locale - do not add if language is en', () => {
hexo.config.language = 'en';

const result = openGraph.call({
page: {},
config: hexo.config,
is_post: isPost
});

result.should.not.contain(meta({property: 'og:locale'}));
});

});

0 comments on commit 3c4f394

Please sign in to comment.