feat(openGraph): add post's published time to meta tag

* Rename function name to camel case (standardjs)
* Skip timezone conversion, but not using the date() helper
* Open Graph tag is based on Wordpress Yoast, noticed through
https://dvt.name/2019/06/03/hacking-the-casio-f-91w-to-handle-1000-psi/
This commit is contained in:
curben 2019-06-03 22:52:45 +09:30
parent 09c855ee43
commit fba0d3deab
4 changed files with 18 additions and 7 deletions

View File

@ -22,8 +22,8 @@
<title><% if (title) { %><%= title %> | <% } %><%= config.title %></title> <title><% if (title) { %><%= title %> | <% } %><%= config.title %></title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<%/* Add Open Graph meta tags for easier sharing on social networking sites */%> <%/* Add Open Graph meta tags for easier sharing on social networking sites */%>
<%/* Modified for cloudinary compatibility */%> <%/* Modified from original source for compatibility with my blog */%>
<%- open_graph_cloudinary() %> <%- openGraph() %>
<% if (theme.rss) { %> <% if (theme.rss) { %>
<link rel="alternate" href="<%- theme.rss %>" title="<%= config.title %>" type="application/atom+xml"> <link rel="alternate" href="<%- theme.rss %>" title="<%= config.title %>" type="application/atom+xml">
<% } %> <% } %>

View File

@ -1,3 +1,4 @@
<a href="<%- url_for(post.path) %>" class="<%= class_name %>"> <a href="<%- url_for(post.path) %>" class="<%= class_name %>">
<time datetime="<%= date_xml(post.date) %>" itemprop="datePublished"><%= date(post.date, date_format) %></time> <%/* Date format is specified to skip timezone conversion */%>
<time datetime="<%= date(post.date, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]') %>" itemprop="datePublished"><%= date(post.date, date_format) %></time>
</a> </a>

View File

@ -1,5 +1,5 @@
<%/* User-specified updated date */%> <%/* User-specified updated date */%>
<%/* default post.updated is (almost) always is the time 'hexo generate' is executed */%> <%/* default post.updated is (almost) always is the time 'hexo generate' is executed */%>
<a href="<%- url_for(post.path) %>" class="<%= class_name %>"> <a href="<%- url_for(post.path) %>" class="<%= class_name %>">
<time datetime="<%= date_xml(post.lastUpdated) %>" itemprop="dateUpdated"><%= date(post.lastUpdated, date_format) %></time> <time datetime="<%= date(post.lastUpdated, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]') %>" itemprop="dateUpdated"><%= date(post.lastUpdated, date_format) %></time>
</a> </a>

View File

@ -48,7 +48,8 @@ function openGraphHelper (options = {}) {
const url = options.url || this.url const url = options.url || this.url
const siteName = options.site_name || config.title const siteName = options.site_name || config.title
const twitterCard = options.twitter_card || 'summary' const twitterCard = options.twitter_card || 'summary'
const updated = options.updated !== false ? options.updated || page.updated : false const published = page.date || false
const updated = page.lastUpdated || false
const language = options.language || page.lang || page.language || config.language const language = options.language || page.lang || page.language || config.language
let result = '' let result = ''
@ -116,9 +117,18 @@ function openGraphHelper (options = {}) {
result += og('og:image', path, false) result += og('og:image', path, false)
}) })
if (published) {
if ((moment.isMoment(published) || moment.isDate(published)) && !isNaN(published.valueOf())) {
// Skip timezone conversion
result += og('article:published_time', moment(published).format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]'))
}
}
if (updated) { if (updated) {
if ((moment.isMoment(updated) || moment.isDate(updated)) && !isNaN(updated.valueOf())) { if ((moment.isMoment(updated) || moment.isDate(updated)) && !isNaN(updated.valueOf())) {
result += og('og:updated_time', updated.toISOString()) result += og('article:modified_time', moment(updated).format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]'))
result += og('og:updated_time', moment(updated).format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]'))
} }
} }
@ -158,4 +168,4 @@ function openGraphHelper (options = {}) {
return result.trim() return result.trim()
} }
hexo.extend.helper.register('open_graph_cloudinary', openGraphHelper) hexo.extend.helper.register('openGraph', openGraphHelper)