hexo-filter-emoji
Advanced tools
Comparing version 2.0.2 to 2.1.0
119
index.js
@@ -1,90 +0,42 @@ | ||
'use strict' | ||
/* global hexo */ | ||
const _ = require('lodash') | ||
const path = require('path') | ||
const fs = require('fs') | ||
const cheerio = require('cheerio') | ||
'use strict' | ||
var options = _.assign({ | ||
enable: true, | ||
inject: true, | ||
version: 'latest', | ||
className: 'github-emoji', | ||
}, hexo.config.githubEmojis) | ||
const _ = require('lodash'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
var options = Object.assign({ | ||
enable : true, | ||
className: 'emoji', | ||
}, hexo.config.emoji); | ||
if (options.enable !== false) { | ||
const emojis = _.assign( | ||
{}, | ||
const emojis = Object.assign( | ||
require('./emojis.json'), | ||
loadCustomEmojis(options.customEmojis || options.localEmojis), | ||
) | ||
); | ||
fs.writeFile( | ||
path.join(__dirname, 'emojis.json'), | ||
JSON.stringify(emojis, null, ' '), | ||
JSON.stringify(emojis), | ||
function (err) { err && console.warn(err) }, | ||
) | ||
); | ||
hexo.extend.filter.register('after_post_render', data => { | ||
if (!options.inject && data['no-emoji']) { return data } | ||
hexo.extend.filter.register('before_post_render', data => { | ||
if (data['no-emoji']) return data; | ||
const $ = cheerio.load(data.content, {decodeEntities: false}) | ||
const excerpt = cheerio.load(data.excerpt, {decodeEntities: false}) | ||
data.content = data.content.replace( | ||
/:(\w+):/ig, | ||
(match, p1) => emojis[p1] ? renderEmoji(emojis, p1) : match, | ||
); | ||
if (options.inject) { | ||
$('body').append(`<script> | ||
document.querySelectorAll('.${options.className}') | ||
.forEach(el => { | ||
if (!el.dataset.src) { return; } | ||
const img = document.createElement('img'); | ||
img.style = 'display:none !important;'; | ||
img.src = el.dataset.src; | ||
img.addEventListener('error', () => { | ||
img.remove(); | ||
el.style.color = 'inherit'; | ||
el.style.backgroundImage = 'none'; | ||
el.style.background = 'none'; | ||
}); | ||
img.addEventListener('load', () => { | ||
img.remove(); | ||
}); | ||
document.body.appendChild(img); | ||
}); | ||
</script>`) | ||
} | ||
return data; | ||
}); | ||
if (!data['no-emoji']) { | ||
replaceColons($('body')[0], $, emojis) | ||
replaceColons(excerpt('body')[0], excerpt, emojis) | ||
} | ||
data.content = $('body').html() | ||
data.excerpt = excerpt('body').html() | ||
return data | ||
}) | ||
hexo.extend.helper.register('github_emoji', name => renderEmoji(emojis, name)) | ||
hexo.extend.tag.register('github_emoji', args => renderEmoji(emojis, args[0])) | ||
hexo.extend.helper.register('emoji', name => renderEmoji(emojis, name)) | ||
hexo.extend.tag.register('emoji', args => renderEmoji(emojis, args[0])) | ||
} | ||
function replaceColons (node, $, emojis) { | ||
node.children.forEach(child => { | ||
if (child.type === 'text') { | ||
const content = child.data.replace( | ||
/:(\w+):/ig, | ||
(match, p1) => emojis[p1] ? renderEmoji(emojis, p1) : match, | ||
) | ||
if (content !== child.data) { | ||
$(child).replaceWith($.parseHTML(content)) | ||
} | ||
} else if (child.type === 'tag') { | ||
if (child.name !== 'pre' && child.name !== 'code') { | ||
replaceColons(child, $, emojis) | ||
} | ||
} | ||
}) | ||
} | ||
function loadCustomEmojis (customEmojis) { | ||
function loadCustomEmojis(customEmojis) { | ||
// JSON string | ||
@@ -103,3 +55,3 @@ if (_.isString(customEmojis)) { | ||
customEmojis = {} | ||
console.warn('hexo-filter-github-emojis: Custom emojis not valid. Skipped.') | ||
console.warn('hexo-filter-emoji: Custom emojis not valid. Skipped.') | ||
} | ||
@@ -119,4 +71,4 @@ } | ||
function renderEmoji (emojis, name) { | ||
if (!emojis[name]) { return name } | ||
function renderEmoji(emojis, name) { | ||
if (!emojis[name]) return name; | ||
@@ -126,19 +78,12 @@ const styles = _.isObject(options.styles) | ||
.filter(k => _.isString(options.styles[k])) | ||
.map(k => k + ':' + options.styles[k]) | ||
: [] | ||
.map(k => k + ': ' + options.styles[k]) | ||
: []; | ||
if (options.inject) { | ||
styles.push( | ||
'color: transparent', | ||
`background:no-repeat url(${emojis[name].src}) center/contain`, | ||
) | ||
} else { | ||
styles.push(`background-image:url(${emojis[name].src})`) | ||
} | ||
styles.push(`background-image: url(${emojis[name].src})`); | ||
const codepoints = emojis[name].codepoints | ||
? emojis[name].codepoints.map(c => `&#x${c};`).join('') | ||
: ' ' | ||
: ' '; | ||
return `<span class="${options.className}" style="${styles.join(';')}" data-src="${emojis[name].src}">${codepoints}</span>` | ||
return `<span class="${options.className}" style="${styles.join('; ')}" data-src="${emojis[name].src}">${codepoints}</span>`; | ||
} |
{ | ||
"name": "hexo-filter-emoji", | ||
"version": "2.0.2", | ||
"description": "A Hexo plugin that adds emojis support, using Github Emojis API", | ||
"main": "index", | ||
"version": "2.1.0", | ||
"description": "A Hexo plugin that adds emojis support, using Github Emojis API.", | ||
"main": "index.js", | ||
"repository": "https://github.com/theme-next/hexo-filter-emoji", | ||
"engines": { | ||
"node": ">=8.0.0" | ||
"node": ">=8.6.0" | ||
}, | ||
@@ -15,17 +15,12 @@ "keywords": [ | ||
"github emojis", | ||
"github", | ||
"emoji", | ||
"emojis" | ||
], | ||
"author": "Mimi <zsq@zsq.im> (https://zhangshuqiao.org)", | ||
"maintainers": [ | ||
"CRIMX <straybugs@gmail.com> (http://blog.crimx.com)" | ||
], | ||
"author": "Mimi <stevenjoezhang@gmail.com> (https://zhangshuqiao.org)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"cheerio": "^1.0.0-rc.2", | ||
"lodash": "^4.17.15", | ||
"lodash": "4.17.15", | ||
"random-ua": "0.0.6", | ||
"request": "^2.88.0" | ||
"request": "2.88.0" | ||
} | ||
} |
# hexo-filter-emoji | ||
[![Npm Version](https://img.shields.io/npm/v/hexo-filter-emoji.svg)](https://npmjs.org/package/hexo-filter-emoji) | ||
[![Npm Downloads Month](https://img.shields.io/npm/dm/hexo-filter-emoji.svg)](https://npmjs.org/package/hexo-filter-emoji) | ||
[![Npm Downloads Total](https://img.shields.io/npm/dt/hexo-filter-emoji.svg)](https://npmjs.org/package/hexo-filter-emoji) | ||
[![License](https://img.shields.io/npm/l/hexo-filter-emoji.svg)](https://npmjs.org/package/hexo-filter-emoji) | ||
[![Npm Version](https://img.shields.io/npm/v/hexo-filter-emoji?style=flat-square)](https://npmjs.org/package/hexo-filter-emoji) | ||
[![Npm Downloads Month](https://img.shields.io/npm/dm/hexo-filter-emoji?style=flat-square)](https://npmjs.org/package/hexo-filter-emoji) | ||
[![Npm Downloads Total](https://img.shields.io/npm/dt/hexo-filter-emoji?style=flat-square)](https://npmjs.org/package/hexo-filter-emoji) | ||
[![License](https://img.shields.io/npm/l/hexo-filter-emoji?style=flat-square)](https://npmjs.org/package/hexo-filter-emoji) | ||
@@ -15,3 +15,3 @@ A Hexo plugin that adds emoji support, using [Github Emojis API](https://api.github.com/emojis). | ||
``` bash | ||
$ npm install hexo-filter-emoji --save | ||
$ npm install hexo-filter-emoji | ||
``` | ||
@@ -24,6 +24,5 @@ | ||
``` yaml | ||
githubEmojis: | ||
emoji: | ||
enable: true | ||
className: github-emoji | ||
inject: true | ||
styles: | ||
@@ -36,39 +35,9 @@ customEmojis: | ||
```html | ||
<span class="github-emoji" style="background-image:url(https://assets-cdn.github.com/images/icons/emoji/unicode/2728.png?v8)" data-src="https://assets-cdn.github.com/images/icons/emoji/unicode/2728.png?v8">✨</span> | ||
<span class="github-emoji" style="background-image: url(https://assets-cdn.github.com/images/icons/emoji/unicode/2728.png?v8)" data-src="https://assets-cdn.github.com/images/icons/emoji/unicode/2728.png?v8">✨</span> | ||
``` | ||
- **inject** - If true, the filter will inject proper inline styles and a script to fallback when image loading fails. If you can modify script files and style files, you may turn this off and add them yourself. | ||
```html | ||
<span class="github-emoji" style="color:transparent;background:no-repeat url(...) center/contain" ...> | ||
``` | ||
A script tag will be appended, the className changes according to the options: | ||
```html | ||
<script> | ||
document.querySelectorAll('.github-emojis') | ||
.forEach(el => { | ||
if (!el.dataset.src) { return; } | ||
const img = document.createElement('img'); | ||
img.style = 'display:none !important;'; | ||
img.src = el.dataset.src; | ||
img.addEventListener('error', () => { | ||
img.remove(); | ||
el.style.color = 'inherit'; | ||
el.style.backgroundImage = 'none'; | ||
el.style.background = 'none'; | ||
}); | ||
img.addEventListener('load', () => { | ||
img.remove(); | ||
}); | ||
document.body.appendChild(img); | ||
}); | ||
</script> | ||
``` | ||
- **styles** - inline styles. For example: | ||
```yaml | ||
githubEmojis: | ||
emoji: | ||
styles: | ||
@@ -82,3 +51,3 @@ font-size: 2em | ||
```html | ||
<span class="github-emoji" style="font-size:2em;font-weight:bold;background-image:url(...)" ...> | ||
<span class="github-emoji" style="font-size: 2em; font-weight: bold; background-image: url(...)" ...> | ||
``` | ||
@@ -91,3 +60,3 @@ | ||
```yaml | ||
githubEmojis: | ||
emoji: | ||
customEmojis: | ||
@@ -101,3 +70,3 @@ arrow_left: https://path/to/arrow_left.png | ||
```yaml | ||
githubEmojis: | ||
emoji: | ||
customEmojis: | ||
@@ -115,3 +84,3 @@ man_juggling: | ||
```html | ||
{% github_emoji sparkles %} | ||
{% emoji sparkles %} | ||
``` | ||
@@ -129,3 +98,3 @@ | ||
{% github_emoji tada %} still works. | ||
{% emoji tada %} still works. | ||
``` | ||
@@ -135,10 +104,6 @@ | ||
You can also render a GitHub emoji from a template using the `github_emoji` helper: | ||
You can also render a GitHub emoji from a template using the `emoji` helper: | ||
```html | ||
<h1><% github_emoji('octocat') %></h1> | ||
<h1>{% emoji('octocat') %}</h1> | ||
``` | ||
## Credits | ||
[hexo-filter-github-emojis](https://github.com/crimx/hexo-filter-github-emojis), Author: [crimx](https://github.com/crimx). |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
3
190702
7
116
101
1
+ Addedlodash@4.17.15(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedrequest@2.88.0(transitive)
+ Addedtough-cookie@2.4.3(transitive)
- Removedcheerio@^1.0.0-rc.2
- Removedboolbase@1.0.0(transitive)
- Removedcheerio@1.0.0(transitive)
- Removedcheerio-select@2.1.0(transitive)
- Removedcss-select@5.1.0(transitive)
- Removedcss-what@6.1.0(transitive)
- Removeddom-serializer@2.0.0(transitive)
- Removeddomelementtype@2.3.0(transitive)
- Removeddomhandler@5.0.3(transitive)
- Removeddomutils@3.2.2(transitive)
- Removedencoding-sniffer@0.2.0(transitive)
- Removedentities@4.5.0(transitive)
- Removedhtmlparser2@9.1.0(transitive)
- Removediconv-lite@0.6.3(transitive)
- Removedlodash@4.17.21(transitive)
- Removednth-check@2.1.1(transitive)
- Removedparse5@7.2.1(transitive)
- Removedparse5-htmlparser2-tree-adapter@7.1.0(transitive)
- Removedparse5-parser-stream@7.1.2(transitive)
- Removedrequest@2.88.2(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedundici@6.21.1(transitive)
- Removedwhatwg-encoding@3.1.1(transitive)
- Removedwhatwg-mimetype@4.0.0(transitive)
Updatedlodash@4.17.15
Updatedrequest@2.88.0