first commit

This commit is contained in:
CRIMX 2016-12-22 19:11:28 +08:00
commit b20ba676ac
7 changed files with 1658 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.DS_Store
node_modules/
tmp/
*.log

9
.npmignore Normal file
View File

@ -0,0 +1,9 @@
test/
tmp/
coverage/
*.log
.jshintrc
.travis.yml
gulpfile.js
.idea/
appveyor.yml

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright (c) 2014 Tommy Chen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

50
README.md Normal file
View File

@ -0,0 +1,50 @@
# hexo-filter-github-emojis
[![Build Status](https://travis-ci.org/crimx/hexo-filter-github-emojis.svg?branch=master)](https://travis-ci.org/crimx/hexo-filter-github-emojis)
[![Coverage Status](https://img.shields.io/coveralls/crimx/hexo-filter-github-emojis.svg)](https://coveralls.io/r/crimx/hexo-filter-github-emojis?branch=master)
[![Npm Version](https://img.shields.io/npm/v/hexo-filter-github-emojis.svg)](https://npmjs.org/package/hexo-filter-github-emojis)
[![Npm Downloads Month](https://img.shields.io/npm/dm/hexo-filter-github-emojis.svg)](https://npmjs.org/package/hexo-filter-github-emojis)
[![Npm Downloads Total](https://img.shields.io/npm/dt/hexo-filter-github-emojis.svg)](https://npmjs.org/package/hexo-filter-github-emojis)
[![License](https://img.shields.io/npm/l/hexo-filter-github-emojis.svg)](https://npmjs.org/package/hexo-filter-github-emojis)
A Hexo plugin that adds emoji support, using [Github Emojis API][ghemojis].
Check out the [Emoji Cheat Sheet](http://www.webpagefx.com/tools/emoji-cheat-sheet/) for all the emojis it supports.
## Installation
``` bash
$ npm install hexo-filter-github-emojis --save
```
## Options
You can configure this plugin in `_config.yml`. Default options:
``` yaml
githubEmojis:
enable: true
className: github-emoji
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.
- **className** - Image class name. For `:sparkles:` the filter will generate something like this:
```html
<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
```
[ghemojis]: https://api.github.com/emojis

1508
emojis.json Normal file

File diff suppressed because it is too large Load Diff

57
index.js Normal file
View File

@ -0,0 +1,57 @@
'use strict'
/* global hexo */
var _ = require('lodash')
var options = _.assign({
enable: true,
className: 'github-emoji'
}, hexo.config.githubEmojis)
if (options.enable !== false) {
var request = require('request')
var randomUa = require('random-ua')
// fallback
var fallbackEmojis = require('./emojis.json')
var localEmojis = options.localEmojis
if (_.isString(localEmojis)) {
try {
localEmojis = JSON.parse(localEmojis)
} catch (SyntaxError) {
localEmojis = {}
throw new Error('filter-github-emojis: local emojis parsing error')
}
}
var githubEmojis = _.assign(fallbackEmojis, localEmojis)
// get the lastest github version
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)) {
githubEmojis = _.assign(fallbackEmojis, body, localEmojis)
}
}
})
hexo.extend.filter.register('before_post_render', function (data) {
data.content = data.content.replace(/:(\w+):/ig, function (match, p1) {
if (githubEmojis[p1]) {
return '<img class="' + options.className +
'" title="' + match + '" alt="' + match + '" src="' +
githubEmojis[p1] + '" height="20" width="20" />'
} else {
return match
}
})
return data
})
}

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "hexo-filter-github-emojis",
"version": "1.0.0",
"description": "A Hexo plugin that adds emojis support, using Github Emojis API",
"main": "index",
"repository": "crimx/hexo-filter-github-emojis",
"keywords": [
"hexo",
"hexo filter",
"hexo plugin",
"github emojis",
"github",
"emoji",
"emojis"
],
"author": "CRIMX <straybugs@gmail.com> (http://blog.crimx.com)",
"license": "MIT",
"dependencies": {
"lodash": "^4.17.2",
"random-ua": "0.0.6",
"request": "^2.79.0"
}
}