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-09-25 02:41:07 +00:00
|
|
|
const { encodeURL, 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 01:10:57 +00:00
|
|
|
const description = page.excerpt || theme.description || false
|
|
|
|
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-09-25 02:41:07 +00:00
|
|
|
const url = encodeURL(this.url)
|
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-18 01:04:31 +00:00
|
|
|
const language = 'en_GB'
|
2019-05-25 12:51:44 +00:00
|
|
|
let result = ''
|
|
|
|
|
|
|
|
if (!Array.isArray(images)) images = [images]
|
|
|
|
|
|
|
|
if (description) {
|
|
|
|
description = stripHTML(description).substring(0, 200)
|
|
|
|
.trim() // Remove prefixing/trailing spaces
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''')
|
|
|
|
.replace(/\n/g, ' ') // Replace new lines by spaces
|
|
|
|
}
|
|
|
|
|
2019-08-11 02:12:36 +00:00
|
|
|
if (!images.length && content && content.includes('<img')) {
|
2019-09-04 02:05:13 +00:00
|
|
|
images = images.slice()
|
2019-05-25 12:51:44 +00:00
|
|
|
|
2019-08-12 08:11:48 +00:00
|
|
|
let img
|
2019-09-04 01:31:06 +00:00
|
|
|
const imgPattern = /<img [^>]*src=['"]([^'"]+)([^>]*>)/gi
|
2019-08-12 08:11:48 +00:00
|
|
|
while ((img = imgPattern.exec(content)) !== null) {
|
2019-09-04 01:31:06 +00:00
|
|
|
images.push(img[1])
|
2019-08-12 08:11:48 +00:00
|
|
|
}
|
2019-05-25 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (description) {
|
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
|
|
|
|
|
|
|
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)
|