-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
174 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
retext-emoji.js | ||
retext-emoji.min.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,128 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var affixEmoticonModifier = require('nlcst-affix-emoticon-modifier'); | ||
var emoticonModifier = require('nlcst-emoticon-modifier'); | ||
var emojiModifier = require('nlcst-emoji-modifier'); | ||
var emoticons = require('emoticon'); | ||
var toString = require('nlcst-to-string'); | ||
var gemoji = require('gemoji'); | ||
var visit = require('unist-util-visit'); | ||
var affixEmoticonModifier = require('nlcst-affix-emoticon-modifier') | ||
var emoticonModifier = require('nlcst-emoticon-modifier') | ||
var emojiModifier = require('nlcst-emoji-modifier') | ||
var emoticons = require('emoticon') | ||
var toString = require('nlcst-to-string') | ||
var gemoji = require('gemoji') | ||
var visit = require('unist-util-visit') | ||
|
||
module.exports = emoji; | ||
module.exports = emoji | ||
|
||
var EMOTICON_NODE = 'EmoticonNode'; | ||
var type = 'EmoticonNode' | ||
|
||
/* Map of visitors. */ | ||
// Map of visitors. | ||
var fns = { | ||
encode: toEmoji, | ||
decode: toGemoji | ||
}; | ||
|
||
var unicodes = gemoji.unicode; | ||
var names = gemoji.name; | ||
|
||
var shortcodes = {}; | ||
|
||
(function () { | ||
var key; | ||
var shortcode; | ||
var result = {}; | ||
var length = emoticons.length; | ||
var index = -1; | ||
var count; | ||
var offset; | ||
var subset; | ||
var name; | ||
|
||
for (key in names) { | ||
shortcode = ':' + key + ':'; | ||
shortcodes[shortcode] = names[key]; | ||
shortcodes[shortcode].shortcode = shortcode; | ||
} | ||
} | ||
|
||
while (++index < length) { | ||
name = emoticons[index].name; | ||
subset = emoticons[index].emoticons; | ||
count = subset.length; | ||
offset = -1; | ||
var unicodes = gemoji.unicode | ||
var names = gemoji.name | ||
|
||
while (++offset < count) { | ||
result[subset[offset]] = names[name]; | ||
} | ||
} | ||
var shortcodes = {} | ||
|
||
emoticons = result; | ||
})(); | ||
init() | ||
|
||
/* Attacher. */ | ||
function emoji(options) { | ||
var Parser = this.Parser; | ||
var proto = Parser.prototype; | ||
var convert = (options || {}).convert; | ||
var fn; | ||
var Parser = this.Parser | ||
var proto = Parser.prototype | ||
var convert = (options || {}).convert | ||
var fn | ||
|
||
proto.useFirst('tokenizeSentence', emoticonModifier); | ||
proto.useFirst('tokenizeSentence', emojiModifier); | ||
proto.useFirst('tokenizeParagraph', affixEmoticonModifier); | ||
proto.useFirst('tokenizeSentence', emoticonModifier) | ||
proto.useFirst('tokenizeSentence', emojiModifier) | ||
proto.useFirst('tokenizeParagraph', affixEmoticonModifier) | ||
|
||
if (convert !== null && convert !== undefined) { | ||
fn = fns[convert]; | ||
fn = fns[convert] | ||
|
||
if (!fn) { | ||
throw new TypeError( | ||
'Illegal invocation: `' + convert + | ||
'` is not a valid value for ' + | ||
'`options.convert` in `retext#use(emoji, options)`' | ||
); | ||
'Illegal invocation: `' + | ||
convert + | ||
'` is not a valid value for ' + | ||
'`options.convert` in `retext#use(emoji, options)`' | ||
) | ||
} | ||
} | ||
|
||
return transformer; | ||
return transformer | ||
|
||
function transformer(node) { | ||
visit(node, EMOTICON_NODE, visitor); | ||
visit(node, type, visitor) | ||
} | ||
|
||
function visitor(node) { | ||
var data = node.data; | ||
var value = toString(node); | ||
var info; | ||
var data = node.data | ||
var value = toString(node) | ||
var info | ||
|
||
if (fn) { | ||
fn(node); | ||
fn(node) | ||
} | ||
|
||
info = unicodes[value] || shortcodes[value] || emoticons[value]; | ||
info = unicodes[value] || shortcodes[value] || emoticons[value] | ||
|
||
if (!data) { | ||
data = {}; | ||
node.data = data; | ||
data = {} | ||
node.data = data | ||
} | ||
|
||
data.names = info.names.concat(); | ||
data.description = info.description; | ||
data.tags = info.tags.concat(); | ||
data.names = info.names.concat() | ||
data.description = info.description | ||
data.tags = info.tags.concat() | ||
} | ||
} | ||
|
||
/* Replace a unicode emoji with a short-code. */ | ||
// Replace a unicode emoji with a short-code. | ||
function toGemoji(node) { | ||
var value = toString(node); | ||
var info = (unicodes[value] || emoticons[value] || {}).shortcode; | ||
var value = toString(node) | ||
var info = (unicodes[value] || emoticons[value] || {}).shortcode | ||
|
||
if (info) { | ||
node.value = info; | ||
node.value = info | ||
} | ||
} | ||
|
||
/* Replace a short-code with a unicode emoji. */ | ||
// Replace a short-code with a unicode emoji. | ||
function toEmoji(node) { | ||
var value = toString(node); | ||
var info = (shortcodes[value] || emoticons[value] || {}).emoji; | ||
var value = toString(node) | ||
var info = (shortcodes[value] || emoticons[value] || {}).emoji | ||
|
||
if (info) { | ||
node.value = info; | ||
node.value = info | ||
} | ||
} | ||
|
||
function init() { | ||
var key | ||
var shortcode | ||
var result = {} | ||
var length = emoticons.length | ||
var index = -1 | ||
var count | ||
var offset | ||
var subset | ||
var name | ||
|
||
for (key in names) { | ||
shortcode = ':' + key + ':' | ||
shortcodes[shortcode] = names[key] | ||
shortcodes[shortcode].shortcode = shortcode | ||
} | ||
|
||
while (++index < length) { | ||
name = emoticons[index].name | ||
subset = emoticons[index].emoticons | ||
count = subset.length | ||
offset = -1 | ||
|
||
while (++offset < count) { | ||
result[subset[offset]] = names[name] | ||
} | ||
} | ||
|
||
emoticons = result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.