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
|
|
|
|
* for compatibility with cloudinary.js
|
|
|
|
* the <meta name="og:image"> now use
|
|
|
|
* data-src attribute of <img>, instead of src
|
|
|
|
*/
|
|
|
|
|
2019-05-25 12:51:44 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const moment = require('moment')
|
|
|
|
const { escapeHTML, htmlTag, stripHTML } = require('hexo-util')
|
2019-07-13 04:56:32 +00:00
|
|
|
const cheerio = require('cheerio')
|
2019-05-25 12:51:44 +00:00
|
|
|
|
|
|
|
function meta (name, content, escape) {
|
|
|
|
if (escape !== false && typeof content === 'string') {
|
|
|
|
content = escapeHTML(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${htmlTag('meta', {
|
|
|
|
name,
|
|
|
|
content
|
|
|
|
})}\n`
|
|
|
|
}
|
|
|
|
|
|
|
|
function og (name, content, escape) {
|
|
|
|
if (escape !== false && typeof content === 'string') {
|
|
|
|
content = escapeHTML(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${htmlTag('meta', {
|
|
|
|
property: name,
|
|
|
|
content
|
|
|
|
})}\n`
|
|
|
|
}
|
|
|
|
|
|
|
|
function openGraphHelper (options = {}) {
|
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-08-09 04:25:10 +00:00
|
|
|
let description = page.excerpt || theme.description
|
2019-05-25 12:51:44 +00:00
|
|
|
const keywords = page.keywords || (page.tags && page.tags.length ? page.tags : undefined) || config.keywords
|
2019-06-08 08:01:39 +00:00
|
|
|
const title = page.title || theme.nickname
|
|
|
|
const type = (this.is_post() ? 'article' : 'website')
|
2019-08-09 13:14:39 +00:00
|
|
|
const url = this.url.replace(/index.html$/, '')
|
2019-06-08 08:01:39 +00:00
|
|
|
const siteName = theme.nickname
|
2019-05-25 12:51:44 +00:00
|
|
|
const twitterCard = options.twitter_card || 'summary'
|
2019-06-03 13:22:45 +00:00
|
|
|
const published = page.date || false
|
|
|
|
const updated = page.lastUpdated || false
|
2019-05-25 12:51:44 +00:00
|
|
|
const language = options.language || page.lang || page.language || config.language
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!images.length && content) {
|
|
|
|
images = images.slice()
|
|
|
|
|
|
|
|
const $ = cheerio.load(content)
|
|
|
|
|
|
|
|
$('img').each(function () {
|
|
|
|
const src = $(this).attr('data-src')
|
|
|
|
if (src) images.push(src)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (description) {
|
|
|
|
result += meta('description', description, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keywords) {
|
|
|
|
if (typeof keywords === 'string') {
|
|
|
|
result += meta('keywords', keywords)
|
|
|
|
} else if (keywords.length) {
|
|
|
|
result += meta('keywords', keywords.map(tag => {
|
|
|
|
return tag.name ? tag.name : tag
|
|
|
|
}).filter(keyword => !!keyword).join())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result += og('og:type', type)
|
|
|
|
result += og('og:title', title)
|
|
|
|
result += og('og:url', url, false)
|
|
|
|
result += og('og:site_name', siteName)
|
|
|
|
if (description) {
|
|
|
|
result += og('og:description', description, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (language) {
|
|
|
|
result += og('og:locale', language, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
images = images.map(path => {
|
2019-08-09 13:02:51 +00:00
|
|
|
if (!new URL(path).host) {
|
2019-05-25 12:51:44 +00:00
|
|
|
// resolve `path`'s absolute path relative to current page's url
|
|
|
|
// `path` can be both absolute (starts with `/`) or relative.
|
2019-08-09 13:02:51 +00:00
|
|
|
return new URL(path, url || config.url)
|
2019-05-25 12:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
})
|
|
|
|
|
|
|
|
images.forEach(path => {
|
|
|
|
result += og('og:image', path, false)
|
|
|
|
})
|
|
|
|
|
2019-06-03 13:22:45 +00:00
|
|
|
if (published) {
|
|
|
|
if ((moment.isMoment(published) || moment.isDate(published)) && !isNaN(published.valueOf())) {
|
|
|
|
// 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-06-04 04:05:48 +00:00
|
|
|
result += og('article:modified_time', moment(updated).format('YYYY-MM-DD[T00:00:00.000Z]'))
|
|
|
|
result += og('og:updated_time', moment(updated).format('YYYY-MM-DD[T00:00:00.000Z]'))
|
2019-05-25 12:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result += meta('twitter:card', twitterCard)
|
|
|
|
|
|
|
|
if (images.length) {
|
|
|
|
result += meta('twitter:image', images[0], false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.twitter_id) {
|
|
|
|
let twitterId = options.twitter_id
|
|
|
|
if (twitterId[0] !== '@') twitterId = `@${twitterId}`
|
|
|
|
|
|
|
|
result += meta('twitter:creator', twitterId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.twitter_site) {
|
|
|
|
result += meta('twitter:site', options.twitter_site, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.google_plus) {
|
|
|
|
result += `${htmlTag('link', { rel: 'publisher', href: options.google_plus })}\n`
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.fb_admins) {
|
|
|
|
result += og('fb:admins', options.fb_admins)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.fb_app_id) {
|
|
|
|
result += og('fb:app_id', options.fb_app_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.trim()
|
|
|
|
}
|
|
|
|
|
2019-06-03 13:22:45 +00:00
|
|
|
hexo.extend.helper.register('openGraph', openGraphHelper)
|