Merge pull request #4 from ngokevin/helper

expose as a helper to be used from templates
This commit is contained in:
Jack Wong 2017-06-26 14:31:46 +08:00 committed by GitHub
commit 0910a7bdb4
2 changed files with 37 additions and 17 deletions

View file

@ -1,6 +1,6 @@
# hexo-filter-github-emojis
[![Npm Version](https://img.shields.io/npm/v/hexo-filter-github-emojis.svg)](https://npmjs.org/package/hexo-filter-github-emojis)
[![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)
@ -60,7 +60,7 @@ The filter will try to download the latest version of [Github Emojis][ghemojis]
- **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:
@ -79,4 +79,12 @@ The filter will try to download the latest version of [Github Emojis][ghemojis]
arrow_right: https://path/to/arrow_right.png
```
## Helper
You can also render a GitHub emoji from a template using the `github_emoji` helper:
```html
<h1><% github_emoji('octocat') %></h1>
```
[ghemojis]: https://api.github.com/emojis

View file

@ -79,25 +79,37 @@ if (options.enable !== false) {
hexo.extend.filter.register('before_post_render', function (data) {
data.content = data.content.replace(/:(\w+):/ig, function (match, p1) {
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="' + emojis[p1].src +
'">' + codepoints + '</span>'
} else {
return '<img class="' + options.className +
'" title="' + match + '" alt="' + match + '" src="' +
emojis[p1].src + '" height="20" width="20" />'
}
return getRender(p1);
} else {
return match
}
})
return data
})
hexo.extend.helper.register('github_emoji', function (name) {
return getRender(name);
});
function getRender (emojiName) {
if (emojis[emojiName]) {
var codepoints = emojis[emojiName].codepoints
if (options.unicode && codepoints) {
codepoints = codepoints.map(function (code) {
return '&#x' + code + ';'
}).join('')
return '<span class="' + options.className +
'" title="' + emojiName +
'" data-src="' + emojis[emojiName].src +
'">' + codepoints + '</span>'
} else {
return '<img class="' + options.className +
'" title="' + emojiName + '" alt="' + emojiName + '" src="' +
emojis[emojiName].src + '" height="20" width="20" />'
}
} else {
return emojiName;
}
}
}