From 67ea1498ff128e6121e9897aa7093107ccfacab6 Mon Sep 17 00:00:00 2001 From: otofune Date: Tue, 28 Feb 2017 20:33:27 +0000 Subject: [PATCH 1/2] [common] text & [web] common > scripts > text compiler: support emoji --- package.json | 1 + src/common/text/elements/emoji.js | 14 ++++++++++++++ src/common/text/index.js | 3 ++- src/web/app/common/scripts/text-compiler.js | 3 +++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/common/text/elements/emoji.js diff --git a/package.json b/package.json index ed70e73fd7..c3b3e9e859 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "deepcopy": "0.6.3", "download": "5.0.3", "elasticsearch": "12.1.3", + "emojinize": "1.0.0", "escape-html": "1.0.3", "escape-regexp": "0.0.1", "event-stream": "3.3.4", diff --git a/src/common/text/elements/emoji.js b/src/common/text/elements/emoji.js new file mode 100644 index 0000000000..e24231a223 --- /dev/null +++ b/src/common/text/elements/emoji.js @@ -0,0 +1,14 @@ +/** + * Emoji + */ + +module.exports = text => { + const match = text.match(/^:[a-zA-Z0-9+-_]+:/); + if (!match) return null; + const emoji = match[0]; + return { + type: 'emoji', + content: emoji, + emoji: emoji.substr(1, emoji.length - 2) + }; +}; diff --git a/src/common/text/index.js b/src/common/text/index.js index 9da3a26b91..4424169033 100644 --- a/src/common/text/index.js +++ b/src/common/text/index.js @@ -8,7 +8,8 @@ const elements = [ require('./elements/mention'), require('./elements/hashtag'), require('./elements/code'), - require('./elements/inline-code') + require('./elements/inline-code'), + require('./elements/emoji') ]; function analyze(source) { diff --git a/src/web/app/common/scripts/text-compiler.js b/src/web/app/common/scripts/text-compiler.js index c9eb73e870..a3c1fe8dde 100644 --- a/src/web/app/common/scripts/text-compiler.js +++ b/src/web/app/common/scripts/text-compiler.js @@ -1,5 +1,6 @@ const riot = require('riot'); const nyaize = require('nyaize').default; +const emojinize = require('emojinize'); const CONFIG = require('./config'); const escape = function(text) { @@ -35,6 +36,8 @@ module.exports = function(tokens, shouldBreak, shouldEscape) { return '
' + token.html + '
'; case 'inline-code': return '' + token.html + ''; + case 'emoji': + return emojinize.encode(token.content) } }).join(''); From e9284a797d486ceb9fabe33437c101ad0f5ae6e5 Mon Sep 17 00:00:00 2001 From: otofune Date: Wed, 1 Mar 2017 03:15:45 +0000 Subject: [PATCH 2/2] [test] add emoji test --- test/text.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/text.ts b/test/text.ts index 64247953bf..8a210575d9 100644 --- a/test/text.ts +++ b/test/text.ts @@ -9,10 +9,12 @@ const syntaxhighlighter = require('../src/common/text/core/syntax-highlighter'); describe('Text', () => { it('is correctly analyzed', () => { - const tokens = analyze('@himawari お腹ペコい #yryr'); + const tokens = analyze('@himawari お腹ペコい :cat: #yryr'); assert.deepEqual([ { type: 'mention', content: '@himawari', username: 'himawari' }, { type: 'text', content: ' お腹ペコい ' }, + { type: 'emoji', content: ':cat:', emoji: 'cat'}, + { type: 'text', content: ' '}, { type: 'hashtag', content: '#yryr', hashtag: 'yryr' } ], tokens); }); @@ -48,6 +50,13 @@ describe('Text', () => { ], tokens); }); + it('emoji', () => { + const tokens = analyze(':cat:'); + assert.deepEqual([ + { type: 'emoji', content: ':cat:', emoji: 'cat'} + ], tokens); + }); + it('block code', () => { const tokens = analyze('```\nvar x = "Strawberry Pasta";\n```'); assert.equal(tokens[0].type, 'code');