2019-08-09 13:02:51 +00:00
|
|
|
'use strict'
|
|
|
|
/* global hexo */
|
|
|
|
|
2019-05-25 14:40:44 +00:00
|
|
|
/*
|
|
|
|
* Modified from the hexo version,
|
|
|
|
* https://github.com/hexojs/hexo/blob/master/lib/plugins/helper/open_graph.js
|
2019-09-04 01:31:06 +00:00
|
|
|
* to include https://github.com/hexojs/hexo/pull/3674
|
|
|
|
* and use WHATWG URL API
|
|
|
|
* https://nodejs.org/api/url.html#url_the_whatwg_url_api
|
2019-05-25 14:40:44 +00:00
|
|
|
*/
|
|
|
|
|
2019-05-25 12:51:44 +00:00
|
|
|
const moment = require('moment')
|
2019-12-18 02:46:39 +00:00
|
|
|
const { escapeHTML, htmlTag, prettyUrls, stripHTML } = require('hexo-util')
|
|
|
|
const fullUrlFor = require('hexo-util').full_url_for
|
2019-05-25 12:51:44 +00:00
|
|
|
|
2019-09-25 02:41:07 +00:00
|
|
|
function meta (name, content) {
|
2019-05-25 12:51:44 +00:00
|
|
|
return `${htmlTag('meta', {
|
|
|
|
name,
|
|
|
|
content
|
|
|
|
})}\n`
|
|
|
|
}
|
|
|
|
|
2019-09-25 02:41:07 +00:00
|
|
|
function og (name, content) {
|
2019-05-25 12:51:44 +00:00
|
|
|
return `${htmlTag('meta', {
|
|
|
|
property: name,
|
|
|
|
content
|
|
|
|
})}\n`
|
|
|
|
}
|
|
|
|
|
2019-10-18 01:04:31 +00:00
|
|
|
function openGraphHelper () {
|
2020-03-09 02:35:47 +00:00
|
|
|
const { config, page } = this
|
|
|
|
let description = page.excerpt || ''
|
2019-10-18 01:10:57 +00:00
|
|
|
const author = config.author
|
2020-03-09 02:35:47 +00:00
|
|
|
const keywords = page.tags || ''
|
|
|
|
const title = page.title || config.title || ''
|
2019-06-08 08:01:39 +00:00
|
|
|
const type = (this.is_post() ? 'article' : 'website')
|
2019-12-18 02:46:39 +00:00
|
|
|
const url = prettyUrls(this.url, config.pretty_urls)
|
|
|
|
const screenshot = '/screenshot/' + prettyUrls(this.path, config.pretty_urls)
|
2020-03-09 02:35:47 +00:00
|
|
|
const siteName = config.title || ''
|
|
|
|
const published = page.date || ''
|
|
|
|
const updated = page.lastUpdated || ''
|
2019-10-27 03:28:11 +00:00
|
|
|
const language = config.language.replace('-', '_')
|
2019-05-25 12:51:44 +00:00
|
|
|
let result = ''
|
|
|
|
|
|
|
|
if (description) {
|
2019-10-18 02:31:47 +00:00
|
|
|
description = escapeHTML(stripHTML(description)
|
|
|
|
.trim()
|
|
|
|
.replace(/\n/g, ' ')
|
|
|
|
.substring(0, 200))
|
2019-05-25 12:51:44 +00:00
|
|
|
|
2019-09-25 02:41:07 +00:00
|
|
|
result += meta('description', description)
|
2019-05-25 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 01:10:57 +00:00
|
|
|
result += og('article:author', author)
|
|
|
|
|
2019-05-25 12:51:44 +00:00
|
|
|
if (keywords) {
|
2019-10-18 00:53:01 +00:00
|
|
|
keywords.forEach(tag => {
|
|
|
|
result += og('article:tag', tag.name)
|
|
|
|
})
|
2019-05-25 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result += og('og:type', type)
|
|
|
|
result += og('og:title', title)
|
2019-09-25 02:41:07 +00:00
|
|
|
result += og('og:url', url)
|
2019-10-18 01:04:31 +00:00
|
|
|
|
|
|
|
if (siteName) {
|
|
|
|
result += og('og:site_name', siteName)
|
|
|
|
}
|
|
|
|
|
2019-05-25 12:51:44 +00:00
|
|
|
if (description) {
|
2019-09-25 02:41:07 +00:00
|
|
|
result += og('og:description', description)
|
2019-05-25 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 01:04:31 +00:00
|
|
|
result += og('og:locale', language)
|
2019-05-25 12:51:44 +00:00
|
|
|
|
2019-12-18 02:46:39 +00:00
|
|
|
result += og('og:image', fullUrlFor.call(this, screenshot))
|
2019-05-25 12:51:44 +00:00
|
|
|
|
2019-06-03 13:22:45 +00:00
|
|
|
if (published) {
|
2019-10-18 00:53:01 +00:00
|
|
|
if ((moment.isMoment(published) || moment.isDate(published)) && !isNaN(published.valueOf())) {
|
2019-06-03 13:22:45 +00:00
|
|
|
// Skip timezone conversion
|
2019-06-04 04:05:48 +00:00
|
|
|
result += og('article:published_time', moment(published).format('YYYY-MM-DD[T00:00:00.000Z]'))
|
2019-06-03 13:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 12:51:44 +00:00
|
|
|
if (updated) {
|
|
|
|
if ((moment.isMoment(updated) || moment.isDate(updated)) && !isNaN(updated.valueOf())) {
|
2019-10-18 00:53:01 +00:00
|
|
|
result += og('article:modified_time', moment(updated).format('YYYY-MM-DD[T00:00:00.000Z]'))
|
2019-05-25 12:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.trim()
|
|
|
|
}
|
|
|
|
|
2019-06-03 13:22:45 +00:00
|
|
|
hexo.extend.helper.register('openGraph', openGraphHelper)
|