Skip to content

Commit 3a3e101

Browse files
authored
Merge pull request #3674 from curbengh/og-updated
feat(open_graph): add article:published_time & article:modified_time
2 parents 6633934 + a081077 commit 3a3e101

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

lib/plugins/helper/open_graph.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function openGraphHelper(options = {}) {
3030
let url = options.url || this.url;
3131
const siteName = options.site_name || config.title;
3232
const twitterCard = options.twitter_card || 'summary';
33+
const date = options.date !== false ? options.date || page.date : false;
3334
const updated = options.updated !== false ? options.updated || page.updated : false;
3435
const language = options.language || page.lang || page.language || config.language;
3536

@@ -109,9 +110,15 @@ function openGraphHelper(options = {}) {
109110
result += og('og:image', path);
110111
});
111112

113+
if (date) {
114+
if ((moment.isMoment(date) || moment.isDate(date)) && !isNaN(date.valueOf())) {
115+
result += og('article:published_time', date.toISOString());
116+
}
117+
}
118+
112119
if (updated) {
113120
if ((moment.isMoment(updated) || moment.isDate(updated)) && !isNaN(updated.valueOf())) {
114-
result += og('og:updated_time', updated.toISOString());
121+
result += og('article:modified_time', updated.toISOString());
115122
}
116123
}
117124

test/scripts/helpers/open_graph.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ describe('open_graph', () => {
3737
meta({property: 'og:url'}),
3838
meta({property: 'og:site_name', content: hexo.config.title}),
3939
meta({property: 'og:locale', content: 'en'}),
40-
meta({property: 'og:updated_time', content: post.updated.toISOString()}),
40+
meta({property: 'article:published_time', content: post.date.toISOString()}),
41+
meta({property: 'article:modified_time', content: post.updated.toISOString()}),
4142
meta({name: 'twitter:card', content: 'summary'})
4243
].join('\n'));
4344

@@ -435,27 +436,27 @@ describe('open_graph', () => {
435436
is_post: isPost
436437
}, { });
437438

438-
result.should.contain(meta({property: 'og:updated_time', content: '2016-05-23T21:20:21.372Z'}));
439+
result.should.contain(meta({property: 'article:modified_time', content: '2016-05-23T21:20:21.372Z'}));
439440
});
440441

441-
it('updated - options - allow overriding og:updated_time', () => {
442+
it('updated - options - allow overriding article:modified_time', () => {
442443
const result = openGraph.call({
443444
page: { updated: moment('2016-05-23T21:20:21.372Z') },
444445
config: hexo.config,
445446
is_post: isPost
446447
}, { updated: moment('2015-04-22T20:19:20.371Z') });
447448

448-
result.should.contain(meta({property: 'og:updated_time', content: '2015-04-22T20:19:20.371Z'}));
449+
result.should.contain(meta({property: 'article:modified_time', content: '2015-04-22T20:19:20.371Z'}));
449450
});
450451

451-
it('updated - options - allow disabling og:updated_time', () => {
452+
it('updated - options - allow disabling article:modified_time', () => {
452453
const result = openGraph.call({
453454
page: { updated: moment('2016-05-23T21:20:21.372Z') },
454455
config: hexo.config,
455456
is_post: isPost
456457
}, { updated: false });
457458

458-
result.should.not.contain(meta({property: 'og:updated_time', content: '2016-05-23T21:20:21.372Z'}));
459+
result.should.not.contain(meta({property: 'article:modified_time', content: '2016-05-23T21:20:21.372Z'}));
459460
});
460461

461462
it('description - do not add /(?:og:)?description/ meta tags if there is no description', () => {

0 commit comments

Comments
 (0)