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
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const moment = require('moment')
|
2019-10-18 02:31:47 +00:00
|
|
|
const { encodeURL, escapeHTML, htmlTag, stripHTML } = require('hexo-util')
|
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 () {
|
2019-06-08 08:01:39 +00:00
|
|
|
const { config, page, theme } = this
|
2019-05-25 12:51:44 +00:00
|
|
|
const { content } = page
|
2019-06-08 08:01:39 +00:00
|
|
|
let images = page.photos || []
|
2019-10-18 02:31:47 +00:00
|
|
|
let description = page.excerpt || theme.description || false
|
2019-10-18 01:10:57 +00:00
|
|
|
const author = config.author
|
2019-10-18 00:53:01 +00:00
|
|
|
const keywords = page.tags || false
|
2019-06-08 08:01:39 +00:00
|
|
|
const title = page.title || theme.nickname
|
|
|
|
const type = (this.is_post() ? 'article' : 'website')
|
2019-10-18 01:17:59 +00:00
|
|
|
const url = config.pretty_urls.trailing_index ? encodeURL(this.url)
|
|
|
|
: encodeURL(this.url).replace(/index\.html$/, '')
|
2019-10-18 01:04:31 +00:00
|
|
|
const siteName = config.subtitle || theme.nickname || false
|
2019-06-03 13:22:45 +00:00
|
|
|
const published = page.date || false
|
|
|
|
const updated = page.lastUpdated || false
|
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-10-18 01:12:43 +00:00
|
|
|
if (!images.length && content && content.includes('<img')) {
|
|
|
|
images = images.slice()
|
|
|
|
|
|
|
|
let img
|
|
|
|
const imgPattern = /<img [^>]*src=['"]([^'"]+)([^>]*>)/gi
|
|
|
|
while ((img = imgPattern.exec(content)) !== null) {
|
|
|
|
images.push(img[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(images)) images = [images]
|
|
|
|
|
2019-05-25 12:51:44 +00:00
|
|
|
images = images.map(path => {
|
2019-09-04 02:05:13 +00:00
|
|
|
let url
|
|
|
|
// resolve `path`'s absolute path relative to current page's url
|
|
|
|
// `path` can be both absolute (starts with `/`) or relative.
|
2019-09-04 01:31:06 +00:00
|
|
|
try {
|
2019-09-04 02:05:13 +00:00
|
|
|
url = new URL(path).href
|
|
|
|
} catch (e) {
|
|
|
|
url = new URL(path, config.url).href
|
|
|
|
return url
|
2019-05-25 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 02:05:13 +00:00
|
|
|
return url
|
2019-05-25 12:51:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
images.forEach(path => {
|
2019-09-25 02:41:07 +00:00
|
|
|
result += og('og:image', path)
|
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)
|