feat(emoji): add emoji support

Add unicode emoji support to showdown. To enable this feature,
use `emoji: true` option. A list of supported emojis is
available here: https://github.com/showdownjs/showdown/wiki/Emojis

Closes #448
This commit is contained in:
Estevao Soares dos Santos 2017-10-24 13:44:49 +01:00
parent 61929bb262
commit 5b8f1d312f
21 changed files with 1273 additions and 3 deletions

View File

@ -345,7 +345,7 @@ var defaultOptions = showdown.getDefaultOptions();
Showdown will replace `{u}` with the username. Only applies if ghMentions option is enabled.
Example: `@tivie` with ghMentionsOption set to `//mysite.com/{u}/profile` will result in `<a href="//mysite.com/tivie/profile">@tivie</a>`
* **encodeEmails**: (boolean) [default true] Enables e-mail addresses encoding through the use of Character Entities, transforming ASCII e-mail addresses into its equivalent decimal entities. (since v1.6.1)
* **encodeEmails**: (boolean) [default true] Enable e-mail addresses encoding through the use of Character Entities, transforming ASCII e-mail addresses into its equivalent decimal entities. (since v1.6.1)
NOTE: Prior to version 1.6.1, emails would always be obfuscated through dec and hex encoding.
@ -354,6 +354,9 @@ var defaultOptions = showdown.getDefaultOptions();
* **backslashEscapesHTMLTags**: (boolean) [default false] Support for HTML Tag escaping. ex: `\<div>foo\</div>` **(since v1.7.2)**
* **emoji**: (boolean) [default false] Enable emoji support. Ex: `this is a :smile: emoji`
For more info on available emojis, see https://github.com/showdownjs/showdown/wiki/Emojis **(since v.1.8.0)**
**NOTE**: Please note that until **version 1.6.0**, all of these options are ***DISABLED*** by default in the cli tool.
## Flavors

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -140,6 +140,11 @@ function getDefaultOpts (simple) {
defaultValue: false,
description: 'Support for HTML Tag escaping. ex: \<div>foo\</div>',
type: 'boolean'
},
emoji: {
defaultValue: false,
description: 'Enable emoji support. Ex: `this is a :smile: emoji`',
type: 'boolean'
}
};
if (simple === false) {

View File

@ -24,7 +24,8 @@ var showdown = {},
requireSpaceBeforeHeadingText: true,
ghCompatibleHeaderId: true,
ghMentions: true,
backslashEscapesHTMLTags: true
backslashEscapesHTMLTags: true,
emoji: true
},
original: {
noHeaderId: true,

26
src/subParsers/emoji.js Normal file
View File

@ -0,0 +1,26 @@
/**
* These are all the transformations that occur *within* block-level
* tags like paragraphs, headers, and list items.
*/
showdown.subParser('emoji', function (text, options, globals) {
'use strict';
if (!options.emoji) {
return text;
}
text = globals.converter._dispatch('emoji.before', text, options, globals);
var emojiRgx = /:([\S]+?):/g;
text = text.replace(emojiRgx, function (wm, emojiCode) {
if (showdown.helper.emojis.hasOwnProperty(emojiCode)) {
return showdown.helper.emojis[emojiCode];
}
return wm;
});
text = globals.converter._dispatch('emoji.after', text, options, globals);
return text;
});

View File

@ -20,6 +20,7 @@ showdown.subParser('spanGamut', function (text, options, globals) {
// delimiters in inline links like [this](<url>).
text = showdown.subParser('autoLinks')(text, options, globals);
text = showdown.subParser('simplifiedAutoLinks')(text, options, globals);
text = showdown.subParser('emoji')(text, options, globals);
text = showdown.subParser('italicsAndBold')(text, options, globals);
text = showdown.subParser('strikethrough')(text, options, globals);

View File

@ -0,0 +1,3 @@
<p>foo🍎bar</p>
<p>foo: apple :bar</p>
<p>:foo 🍎 bar:</p>

View File

@ -0,0 +1,5 @@
foo:apple:bar
foo: apple :bar
:foo :apple: bar:

View File

@ -0,0 +1,4 @@
<p>this link <a href="http://www.example.com/some:apple:url">somelink</a></p>
<p>emoji <a href="http://www.example.com/some:apple:url">🍎</a></p>
<p><a href="http://www.example.com/some:apple:url">🍎</a></p>
<p><a href="http://www.example.com/some:apple:url">🍎</a></p>

View File

@ -0,0 +1,11 @@
this link [somelink](http://www.example.com/some:apple:url)
emoji [:apple:](http://www.example.com/some:apple:url)
[:apple:][apple]
[:apple:][]
[apple]: http://www.example.com/some:apple:url
[:apple:]: http://www.example.com/some:apple:url

View File

@ -0,0 +1,2 @@
<p>🍎 and 💋</p>
<p>💋my🍎</p>

View File

@ -0,0 +1,3 @@
:apple: and :kiss:
:kiss:my:apple:

View File

@ -0,0 +1 @@
<p><a href="http://www.example.com/some:apple:url">http://www.example.com/some:apple:url</a></p>

View File

@ -0,0 +1 @@
http://www.example.com/some:apple:url

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,3 @@
this is showdown's emoji :showdown:
and this is github's emoji :octocat:

View File

@ -10,7 +10,8 @@ var bootstrap = require('../bootstrap.js'),
openLinksInNewWindowSuite = bootstrap.getTestSuite('test/features/openLinksInNewWindow/'),
disableForced4SpacesIndentedSublistsSuite = bootstrap.getTestSuite('test/features/disableForced4SpacesIndentedSublists/'),
rawHeaderIdSuite = bootstrap.getTestSuite('test/features/rawHeaderId/'),
rawPrefixHeaderIdSuite = bootstrap.getTestSuite('test/features/rawPrefixHeaderId/');
rawPrefixHeaderIdSuite = bootstrap.getTestSuite('test/features/rawPrefixHeaderId/'),
emojisSuite = bootstrap.getTestSuite('test/features/emojis/');
describe('makeHtml() features testsuite', function () {
'use strict';
@ -178,4 +179,19 @@ describe('makeHtml() features testsuite', function () {
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
});
// test emojis support
describe('emojis support', function () {
var converter,
suite = emojisSuite;
for (var i = 0; i < suite.length; ++i) {
if (suite[i].name === 'simplifiedautolinks') {
converter = new showdown.Converter({emoji: true, simplifiedAutoLink: true});
} else {
converter = new showdown.Converter({emoji: true});
}
it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
}
});
});