Răsfoiți Sursa

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
Estevao Soares dos Santos 7 ani în urmă
părinte
comite
5b8f1d312f

+ 4 - 1
README.md

@@ -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

Fișier diff suprimat deoarece este prea mare
+ 1187 - 1
dist/showdown.js


Fișier diff suprimat deoarece este prea mare
+ 0 - 0
dist/showdown.js.map


Fișier diff suprimat deoarece este prea mare
+ 0 - 0
dist/showdown.min.js


Fișier diff suprimat deoarece este prea mare
+ 0 - 0
dist/showdown.min.js.map


Fișier diff suprimat deoarece este prea mare
+ 1180 - 0
src/helpers.js


+ 5 - 0
src/options.js

@@ -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) {

+ 2 - 1
src/showdown.js

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

+ 26 - 0
src/subParsers/emoji.js

@@ -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;
+});

+ 1 - 0
src/subParsers/spanGamut.js

@@ -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);
 

+ 3 - 0
test/features/emojis/complex.html

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

+ 5 - 0
test/features/emojis/complex.md

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

+ 4 - 0
test/features/emojis/links.html

@@ -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>

+ 11 - 0
test/features/emojis/links.md

@@ -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

+ 2 - 0
test/features/emojis/simple.html

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

+ 3 - 0
test/features/emojis/simple.md

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

+ 1 - 0
test/features/emojis/simplifiedautolinks.html

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

+ 1 - 0
test/features/emojis/simplifiedautolinks.md

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

Fișier diff suprimat deoarece este prea mare
+ 0 - 0
test/features/emojis/special.html


+ 3 - 0
test/features/emojis/special.md

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

+ 17 - 1
test/node/testsuite.features.js

@@ -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));
+    }
+  });
 });

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff