Created
January 3, 2018 03:48
-
-
Save kelabang/c6e12799173ee2381e043225188c2902 to your computer and use it in GitHub Desktop.
emojione lib parse to react
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
import React, {Component} from 'react' | |
import * as util from './util' | |
const emojione = require('emojione') | |
class Page extends Component { | |
render() { | |
const str = ':smile: hehe :rofl:' | |
const parser = Util.emojioneParseReact(emojione) | |
return ( | |
<div> | |
parser(str) | |
</div> | |
) | |
} | |
} |
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
/* | |
use emojione parse lib and passing to this function parser | |
*/ | |
export function emojioneParseReact(emojione) { | |
var unicode, | |
fname, | |
category, | |
title, | |
size, | |
ePath, | |
alt, | |
replaceWith | |
return (str) => { | |
let parsed = reactStringReplace(str, emojione.regShortNames, (shortname, index) => { | |
if( (typeof shortname === 'undefined') || (shortname === '') || (emojione.shortnames.indexOf(shortname) === -1) ) { | |
// if the shortname doesnt exist just return the entire match | |
return shortname; | |
} | |
else { | |
// map shortname to parent | |
if (!emojione.emojioneList[shortname]) { | |
for ( var emoji in emojione.emojioneList ) { | |
if (!emojione.emojioneList.hasOwnProperty(emoji) || (emoji === '')) continue; | |
if (emojione.emojioneList[emoji].shortnames.indexOf(shortname) === -1) continue; | |
shortname = emoji; | |
break; | |
} | |
} | |
unicode = emojione.emojioneList[shortname].uc_output; | |
fname = emojione.emojioneList[shortname].uc_base; | |
category = (fname.includes("-1f3f")) ? 'diversity' : emojione.emojioneList[shortname].category; | |
title = emojione.imageTitleTag ? 'title="' + shortname + '"' : ''; | |
size = (emojione.spriteSize == '32' || emojione.spriteSize == '64') ? emojione.spriteSize : '32'; | |
//if the image path has not changed, we'll assume the default cdn path, otherwise we'll assume the provided path | |
ePath = (emojione.imagePathPNG != emojione.defaultPathPNG) ? emojione.imagePathPNG : emojione.defaultPathPNG + emojione.emojiSize + '/'; | |
// depending on the settings, we'll either add the native unicode as the alt tag, otherwise the shortname | |
alt = (emojione.unicodeAlt) ? emojione.convert(unicode.toUpperCase()) : shortname; | |
if(emojione.sprites) { | |
replaceWith = ( | |
<span className={"emojione emojione-"+size+"-"+category+' _'+fname} title={title}>alt</span> | |
) | |
} | |
else { | |
replaceWith = ( | |
<img className="emojione" alt={alt} title={title} src={ePath+fname+'.png'} /> | |
) | |
} | |
return replaceWith | |
} | |
}) | |
return parsed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment