added JSON file writing

This commit is contained in:
CRIMX 2016-12-23 18:46:01 +08:00
parent 5d743a227c
commit 24d7948ed1
3 changed files with 9450 additions and 1537 deletions

View file

@ -23,8 +23,8 @@ You can configure this plugin in `_config.yml`. Default options:
githubEmojis:
enable: true
className: github-emoji
localEmojis:
unicode: false
localEmojis:
```
The filter will try to download the latest version of [Github Emojis][ghemojis] list. If the network is unavailable or too slow it will use the backup version.
@ -35,17 +35,6 @@ The filter will try to download the latest version of [Github Emojis][ghemojis]
<img class="github-emoji" title=":sparkles:" alt=":sparkles:" src="https://assets-cdn.github.com/images/icons/emoji/unicode/2728.png" height="20" width="20">
```
- **localEmojis** - You can specify your own list. An object or JSON string is valid. The filter will first check the `localEmojis` then fallback to the [Github Emojis][ghemojis] list.
For example:
``` yaml
githubEmojis:
localEmojis:
arrow_left: https://path/tp/arrow_left.png
arrow_right: https://path/tp/arrow_right.png
```
- **unicode** - If you set this option to true, the filter will generate something like this:
```html
@ -58,14 +47,36 @@ The filter will try to download the latest version of [Github Emojis][ghemojis]
var $emoji = $(emoji)
var codepoint = $emoji.html()
$('<img height="20" width="20">')
.prop('src', $emoji.data('src'))
.prop('alt', $emoji.attr('title'))
.on('error', function () {
// image loading failed
$emoji.html(codepoint)
})
.prop('alt', $emoji.attr('title'))
.prop('src', $emoji.data('src'))
.appendTo($emoji.empty())
})
```
- **localEmojis** - You can specify your own list. An object or JSON string is valid. The filter will first check the `localEmojis` then fallback to the [Github Emojis][ghemojis] list.
For example:
``` yaml
githubEmojis:
localEmojis:
arrow_left: https://path/to/arrow_left.png
arrow_right: https://path/to/arrow_right.png
```
If you need to add code points that are not in the Github list, you can do this:
```
githubEmojis:
localEmojis:
man_juggling:
src: https://path/to/arrow_left.png
codepoints: ["1f939", "2642"]
arrow_right: https://path/to/arrow_right.png
```
[ghemojis]: https://api.github.com/emojis

10887
emojis.json

File diff suppressed because it is too large Load diff

View file

@ -6,29 +6,48 @@ var _ = require('lodash')
var options = _.assign({
enable: true,
unicode: false,
version: 'latest',
className: 'github-emoji'
}, hexo.config.githubEmojis)
if (options.enable !== false) {
var path = require('path')
var url = require('url')
var fs = require('fs')
var request = require('request')
var randomUa = require('random-ua')
// fallback
var fallbackEmojis = require('./emojis.json')
// load custom emojis
var localEmojis = options.localEmojis
if (_.isString(localEmojis)) {
// JSON string
if (!_.isObject(localEmojis)) {
try {
localEmojis = JSON.parse(localEmojis)
localEmojis = JSON.parse(localEmojis.toString())
Object.keys(localEmojis).forEach(function (name) {
if (_.isString(localEmojis[name])) {
localEmojis[name] = {
src: localEmojis[name]
}
}
})
} catch (SyntaxError) {
localEmojis = {}
throw new Error('filter-github-emojis: local emojis parsing error')
console.warn('filter-github-emojis: local emojis error')
}
}
Object.keys(localEmojis).forEach(function (name) {
var codepoints = localEmojis[name].codepoints
if (codepoints && !_.isArray(codepoints)) {
localEmojis[name].codepoints = codepoints.split(' ')
}
})
var githubEmojis = _.assign(fallbackEmojis, localEmojis)
var emojis = _.assign(fallbackEmojis, localEmojis)
// get the lastest github version
// get the latest github version
request({
url: 'https://api.github.com/emojis',
headers: {
@ -38,31 +57,39 @@ if (options.enable !== false) {
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
if (_.isObject(body)) {
githubEmojis = _.assign(fallbackEmojis, body, localEmojis)
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'))
}
}
})
hexo.extend.filter.register('before_post_render', function (data) {
data.content = data.content.replace(/:(\w+):/ig, function (match, p1) {
if (githubEmojis[p1]) {
// unicode code point
var codepoint = /\/([\w-]+)\.\w+$/.exec(githubEmojis[p1].split('?')[0])
codepoint = codepoint && codepoint[1]
if (options.unicode && codepoint) {
codepoint = codepoint.split('-').map(function (item) {
return '&#x' + item + ';'
if (emojis[p1]) {
var codepoints = emojis[p1].codepoints
if (options.unicode && codepoints) {
codepoints = codepoints.map(function (code) {
return '&#x' + code + ';'
}).join('')
return '<span class="' + options.className +
'" title="' + match +
'" data-src="' + githubEmojis[p1] +
'">' + codepoint + '</span>'
'" data-src="' + emojis[p1].src +
'">' + codepoints + '</span>'
} else {
return '<img class="' + options.className +
'" title="' + match + '" alt="' + match + '" src="' +
githubEmojis[p1] + '" height="20" width="20" />'
emojis[p1].src + '" height="20" width="20" />'
}
} else {
return match