hexo-filter-github-emojis/index.js

101 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-12-22 19:11:28 +08:00
'use strict'
/* global hexo */
var _ = require('lodash')
var options = _.assign({
enable: true,
2016-12-23 01:02:51 +08:00
unicode: false,
2016-12-23 18:46:01 +08:00
version: 'latest',
2016-12-22 19:11:28 +08:00
className: 'github-emoji'
}, hexo.config.githubEmojis)
if (options.enable !== false) {
2016-12-23 18:46:01 +08:00
var path = require('path')
var url = require('url')
var fs = require('fs')
2016-12-22 19:11:28 +08:00
var request = require('request')
var randomUa = require('random-ua')
// fallback
var fallbackEmojis = require('./emojis.json')
2016-12-23 18:46:01 +08:00
// load custom emojis
2016-12-22 19:11:28 +08:00
var localEmojis = options.localEmojis
2016-12-23 18:46:01 +08:00
// JSON string
if (!_.isObject(localEmojis)) {
2016-12-22 19:11:28 +08:00
try {
2016-12-23 18:46:01 +08:00
localEmojis = JSON.parse(localEmojis.toString())
Object.keys(localEmojis).forEach(function (name) {
if (_.isString(localEmojis[name])) {
localEmojis[name] = {
src: localEmojis[name]
}
}
})
2016-12-22 19:11:28 +08:00
} catch (SyntaxError) {
localEmojis = {}
2016-12-23 18:46:01 +08:00
console.warn('filter-github-emojis: local emojis error')
2016-12-22 19:11:28 +08:00
}
}
2016-12-23 18:46:01 +08:00
Object.keys(localEmojis).forEach(function (name) {
var codepoints = localEmojis[name].codepoints
if (codepoints && !_.isArray(codepoints)) {
localEmojis[name].codepoints = codepoints.split(' ')
}
})
2016-12-22 19:11:28 +08:00
2016-12-23 18:46:01 +08:00
var emojis = _.assign(fallbackEmojis, localEmojis)
2016-12-22 19:11:28 +08:00
2016-12-23 18:46:01 +08:00
// get the latest github version
2016-12-22 19:11:28 +08:00
request({
url: 'https://api.github.com/emojis',
headers: {
'User-Agent': randomUa.generate()
},
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
if (_.isObject(body)) {
2016-12-23 18:46:01 +08:00
var latestEmojis = {}
Object.keys(body).forEach(function (name) {
latestEmojis[name] = { src: body[name] }
if (body[name].indexOf('unicode') !== -1) {
// list of unicode code points
latestEmojis[name].codepoints = path.parse(url.parse(body[name]).pathname).name.split('-')
}
})
var githubEmojis = _.assign(fallbackEmojis, latestEmojis)
emojis = _.assign(githubEmojis, localEmojis)
// update local backup
fs.writeFile(path.join(__dirname, 'emojis.json'), JSON.stringify(githubEmojis, null, '\t'))
2016-12-22 19:11:28 +08:00
}
}
})
hexo.extend.filter.register('before_post_render', function (data) {
data.content = data.content.replace(/:(\w+):/ig, function (match, p1) {
2016-12-23 18:46:01 +08:00
if (emojis[p1]) {
var codepoints = emojis[p1].codepoints
if (options.unicode && codepoints) {
codepoints = codepoints.map(function (code) {
return '&#x' + code + ';'
2016-12-23 01:16:35 +08:00
}).join('')
2016-12-23 01:02:51 +08:00
return '<span class="' + options.className +
'" title="' + match +
2016-12-23 18:46:01 +08:00
'" data-src="' + emojis[p1].src +
'">' + codepoints + '</span>'
2016-12-23 01:02:51 +08:00
} else {
return '<img class="' + options.className +
'" title="' + match + '" alt="' + match + '" src="' +
2016-12-23 18:46:01 +08:00
emojis[p1].src + '" height="20" width="20" />'
2016-12-23 01:02:51 +08:00
}
2016-12-22 19:11:28 +08:00
} else {
return match
}
})
return data
})
}