Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emojis and smileys customization #386

Merged
merged 6 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changelog

- Re-allow emojis and smileys customization, that was broken in [PR #308](https://github.com/lunet-io/markdig/pull/308) ([PR #386](https://github.com/lunet-io/markdig/pull/386))
- Add `IHostProvider` for medialink customization (#337), support protocol-less url (#135) ([(PR #341)](https://github.com/lunet-io/markdig/pull/341))

## 0.18.0 (24 Oct 2019)
Expand Down
8 changes: 4 additions & 4 deletions src/Markdig.Tests/Specs/EmojiSpecs.generated.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated: 2019-04-15 05:20:50
// Generated: 2020-01-13 21:08:58

// --------------------------------
// Emoji
Expand All @@ -18,7 +18,7 @@ public class TestExtensionsEmoji
//
// ## Emoji
//
// Emoji and smiley can be converted to their respective unicode characters:
// Emoji shortcodes and smileys can be converted to their respective unicode characters:
[Test]
public void ExtensionsEmoji_Example001()
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public void ExtensionsEmoji_Example002()
TestParser.TestSpec("These are not:) an emoji with a:) x:angry:x", "<p>These are not:) an emoji with a:) x:angry:x</p>", "emojis|advanced+emojis");
}

// Emoji can be followed by close punctuation (or any other characters):
// Emojis can be followed by close punctuation (or any other characters):
[Test]
public void ExtensionsEmoji_Example003()
{
Expand All @@ -69,7 +69,7 @@ public void ExtensionsEmoji_Example003()
TestParser.TestSpec("We all need :), it makes us :muscle:. (and :ok_hand:).", "<p>We all need 😃, it makes us 💪. (and 👌).</p>", "emojis|advanced+emojis");
}

// Sentences can end with Emoji:
// Sentences can end with emojis:
[Test]
public void ExtensionsEmoji_Example004()
{
Expand Down
6 changes: 3 additions & 3 deletions src/Markdig.Tests/Specs/EmojiSpecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This section describes the different extensions supported:

## Emoji

Emoji and smiley can be converted to their respective unicode characters:
Emoji shortcodes and smileys can be converted to their respective unicode characters:

```````````````````````````````` example
This is a test with a :) and a :angry: smiley
Expand All @@ -20,15 +20,15 @@ These are not:) an emoji with a:) x:angry:x
<p>These are not:) an emoji with a:) x:angry:x</p>
````````````````````````````````

Emoji can be followed by close punctuation (or any other characters):
Emojis can be followed by close punctuation (or any other characters):

```````````````````````````````` example
We all need :), it makes us :muscle:. (and :ok_hand:).
.
<p>We all need 😃, it makes us 💪. (and 👌).</p>
````````````````````````````````

Sentences can end with Emoji:
Sentences can end with emojis:

```````````````````````````````` example
This is a sentence :ok_hand:
Expand Down
99 changes: 99 additions & 0 deletions src/Markdig.Tests/TestCustomEmojis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Collections.Generic;
using Markdig.Extensions.Emoji;
using NUnit.Framework;

namespace Markdig.Tests
{
[TestFixture]
public class TestCustomEmojis
{
[Test]
[TestCase(":smiley:", "<p>♥</p>\n")]
[TestCase(":confused:", "<p>:confused:</p>\n")] // default emoji does not work
[TestCase(":/", "<p>:/</p>\n")] // default smiley does not work
public void TestCustomEmoji(string input, string expected)
{
var emojiToUnicode = new Dictionary<string, string>();
var smileyToEmoji = new Dictionary<string, string>();

emojiToUnicode[":smiley:"] = "♥";

var customMapping = new EmojiMapping(emojiToUnicode, smileyToEmoji);

var pipeline = new MarkdownPipelineBuilder()
.UseEmojiAndSmiley(customEmojiMapping: customMapping)
.Build();

var actual = Markdown.ToHtml(input, pipeline);
Assert.AreEqual(expected, actual);
}

[Test]
[TestCase(":testheart:", "<p>♥</p>\n")]
[TestCase("hello", "<p>♥</p>\n")]
[TestCase(":confused:", "<p>:confused:</p>\n")] // default emoji does not work
[TestCase(":/", "<p>:/</p>\n")] // default smiley does not work
public void TestCustomSmiley(string input, string expected)
{
var emojiToUnicode = new Dictionary<string, string>();
var smileyToEmoji = new Dictionary<string, string>();

emojiToUnicode[":testheart:"] = "♥";
smileyToEmoji["hello"] = ":testheart:";

var customMapping = new EmojiMapping(emojiToUnicode, smileyToEmoji);

var pipeline = new MarkdownPipelineBuilder()
.UseEmojiAndSmiley(customEmojiMapping: customMapping)
.Build();

var actual = Markdown.ToHtml(input, pipeline);
Assert.AreEqual(expected, actual);
}

[Test]
[TestCase(":smiley:", "<p>♥</p>\n")]
[TestCase(":)", "<p>♥</p>\n")]
[TestCase(":confused:", "<p>😕</p>\n")] // default emoji still works
[TestCase(":/", "<p>😕</p>\n")] // default smiley still works
public void TestOverrideDefaultWithCustomEmoji(string input, string expected)
{
var emojiToUnicode = EmojiMapping.GetDefaultEmojiShortcodeToUnicode();
var smileyToEmoji = EmojiMapping.GetDefaultSmileyToEmojiShortcode();

emojiToUnicode[":smiley:"] = "♥";

var customMapping = new EmojiMapping(emojiToUnicode, smileyToEmoji);

var pipeline = new MarkdownPipelineBuilder()
.UseEmojiAndSmiley(customEmojiMapping: customMapping)
.Build();

var actual = Markdown.ToHtml(input, pipeline);
Assert.AreEqual(expected, actual);
}

[Test]
[TestCase(":testheart:", "<p>♥</p>\n")]
[TestCase("hello", "<p>♥</p>\n")]
[TestCase(":confused:", "<p>😕</p>\n")] // default emoji still works
[TestCase(":/", "<p>😕</p>\n")] // default smiley still works
public void TestOverrideDefaultWithCustomSmiley(string input, string expected)
{
var emojiToUnicode = EmojiMapping.GetDefaultEmojiShortcodeToUnicode();
var smileyToEmoji = EmojiMapping.GetDefaultSmileyToEmojiShortcode();

emojiToUnicode[":testheart:"] = "♥";
smileyToEmoji["hello"] = ":testheart:";

var customMapping = new EmojiMapping(emojiToUnicode, smileyToEmoji);

var pipeline = new MarkdownPipelineBuilder()
.UseEmojiAndSmiley(customEmojiMapping: customMapping)
.Build();

var actual = Markdown.ToHtml(input, pipeline);
Assert.AreEqual(expected, actual);
}
}
}
16 changes: 8 additions & 8 deletions src/Markdig/Extensions/Emoji/EmojiExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
namespace Markdig.Extensions.Emoji
{
/// <summary>
/// Extension to allow emoji and smiley replacement.
/// Extension to allow emoji shortcodes and smileys replacement.
/// </summary>
/// <seealso cref="Markdig.IMarkdownExtension" />
public class EmojiExtension : IMarkdownExtension
{
public EmojiExtension(bool enableSmiley = true)
{
EnableSmiley = enableSmiley;
public EmojiExtension(EmojiMapping emojiMapping)
{
EmojiMapping = emojiMapping;
}

public bool EnableSmiley { get; set; }

public EmojiMapping EmojiMapping { get; }
public void Setup(MarkdownPipelineBuilder pipeline)
{
if (!pipeline.InlineParsers.Contains<EmojiParser>())
{
// Insert the parser before any other parsers
pipeline.InlineParsers.Insert(0, new EmojiParser(EnableSmiley));
pipeline.InlineParsers.Insert(0, new EmojiParser(EmojiMapping));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Markdig/Extensions/Emoji/EmojiInline.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

Expand All @@ -8,7 +8,7 @@
namespace Markdig.Extensions.Emoji
{
/// <summary>
/// An emoji inline
/// An emoji inline.
/// </summary>
/// <seealso cref="Markdig.Syntax.Inlines.Inline" />
public class EmojiInline : LiteralInline
Expand All @@ -32,7 +32,7 @@ public EmojiInline(string content)
}

/// <summary>
/// Gets or sets the original match string (either an emoji or a text smiley)
/// Gets or sets the original match string (either an emoji shortcode or a text smiley)
/// </summary>
public string Match { get; set; }
}
Expand Down
Loading