Skip to content

Commit

Permalink
fix(message-parser): mentions and emojis inside bold, italic and stri…
Browse files Browse the repository at this point in the history
…kethrough (#1035)

Co-authored-by: Hugo Costa <hugocarreiracosta@gmail.com>
  • Loading branch information
jayesh-jain252 and hugocostadev committed May 30, 2023
1 parent 9288888 commit 036f70a
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 9 deletions.
12 changes: 9 additions & 3 deletions packages/message-parser/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,23 @@ export type MarkupExcluding<T extends Markup> = Exclude<Markup, T>;

export type Bold = {
type: 'BOLD';
value: Array<MarkupExcluding<Bold> | Link>;
value: Array<
MarkupExcluding<Bold> | Link | Emoji | UserMention | ChannelMention
>;
};

export type Italic = {
type: 'ITALIC';
value: Array<MarkupExcluding<Italic> | Link>;
value: Array<
MarkupExcluding<Italic> | Link | Emoji | UserMention | ChannelMention
>;
};

export type Strike = {
type: 'STRIKE';
value: Array<MarkupExcluding<Strike> | Link>;
value: Array<
MarkupExcluding<Strike> | Link | Emoji | UserMention | ChannelMention
>;
};

export type Plain = {
Expand Down
39 changes: 35 additions & 4 deletions packages/message-parser/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ InlineItem = Whitespace
/ AutolinkedPhone
/ AutolinkedEmail
/ AutolinkedURL
/ EmphasisWithWhitespace
/ Emphasis
/ UserMention
/ ChannelMention
Expand Down Expand Up @@ -364,23 +365,28 @@ ItalicContent = text:ItalicContentItems { return italic(text); }
ItalicContentItems = text:ItalicContentItem+ { return reducePlainTexts(text); }
ItalicContentItem
= References
= Whitespace
/ References
/ UserMention
/ ChannelMention
/ Bold
/ Strikethrough
/ Line
/ Emoji
/ Emoticon
/ AnyItalic
/ Line
/* Bold */
Bold = [\x2A] [\x2A] @BoldContent [\x2A] [\x2A] / [\x2A] @BoldContent [\x2A]
BoldContent = text:BoldContentItem+ { return bold(reducePlainTexts(text)); }
BoldContentItem = References / Italic / Strikethrough / Line / AnyBold
BoldContentItem = Whitespace / References / UserMention / ChannelMention / Italic / Strikethrough / Emoji / Emoticon / AnyBold / Line
/* Strike */
Strikethrough = [\x7E] [\x7E] @StrikethroughContent [\x7E] [\x7E] / [\x7E] @StrikethroughContent [\x7E]
StrikethroughContent = text:(References / Italic / Bold / Line / AnyStrike)+ {
StrikethroughContent = text:(Whitespace / References / UserMention / ChannelMention / Italic / Bold / Emoji / Emoticon / AnyStrike / Line)+ {
return strike(reducePlainTexts(text));
}
Expand All @@ -390,6 +396,31 @@ AnyStrike = t:[^\x0a\~ ] { return plain(t); }
AnyItalic = t:[^\x0a\_ ] { return plain(t); }
/**
* Emphasis with only whitespaces return plain text
* e.g: __ __, _ _, ** **, * *, ** *, ~~ ~~
*/
EmphasisWithWhitespace = AsteriskWithWhitespace / UnderscoreWithWhitespace / TildeWithWhitespace
AsteriskWithWhitespace = first:Asterisk second:Whitespace third:Asterisk
{
return reducePlainTexts([first,second,third])[0];
}
UnderscoreWithWhitespace = first:Underscore second:Whitespace third:Underscore
{
return reducePlainTexts([first,second,third])[0];
}
TildeWithWhitespace = first:Tilde second:Whitespace third:Tilde
{
return reducePlainTexts([first,second,third])[0];
}
Asterisk = t:"*"+ {return plain(t.join(""))}
Underscore = t:"_"+ {return plain(t.join(""))}
Tilde = t:"~"+ {return plain(t.join(""))}
/**
*
* Mentions
Expand Down
39 changes: 39 additions & 0 deletions packages/message-parser/tests/emphasis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,48 @@ import {
emoji,
link,
bigEmoji,
emojiUnicode,
mentionChannel,
mentionUser,
} from '../src/utils';

test.each([
['_:smile:_', [paragraph([italic([emoji('smile')])])]],
['_:slight_smile:_', [paragraph([italic([emoji('slight_smile')])])]],
[
'_test :smile: test_',
[paragraph([italic([plain('test '), emoji('smile'), plain(' test')])])],
],
[
'_test :slight_smile: test_',
[
paragraph([
italic([plain('test '), emoji('slight_smile'), plain(' test')]),
]),
],
],
['_😀_', [paragraph([italic([emojiUnicode('😀')])])]],
['_test 😀_', [paragraph([italic([plain('test '), emojiUnicode('😀')])])]],
[
'_test @guilherme.gazzo test_',
[
paragraph([
italic([
plain('test '),
mentionUser('guilherme.gazzo'),
plain(' test'),
]),
]),
],
],
[
'_test #GENERAL test_',
[
paragraph([
italic([plain('test '), mentionChannel('GENERAL'), plain(' test')]),
]),
],
],
[
'_[A brand new Gist](https://gist.github.com/24dddfa97bef58f46ac2ce0f80c58ba4)_',
[
Expand Down
31 changes: 30 additions & 1 deletion packages/message-parser/tests/strikethrough.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
import { parse } from '../src';
import { link, paragraph, plain, strike } from '../src/utils';
import {
emoji,
emojiUnicode,
link,
mentionChannel,
mentionUser,
paragraph,
plain,
strike,
} from '../src/utils';

test.each([
['~:smile:~', [paragraph([strike([emoji('smile')])])]],
[
'~test :smile: test~',
[paragraph([strike([plain('test '), emoji('smile'), plain(' test')])])],
],
['~😀~', [paragraph([strike([emojiUnicode('😀')])])]],
['~test 😀~', [paragraph([strike([plain('test '), emojiUnicode('😀')])])]],
[
'~@guilherme.gazzo~',
[paragraph([strike([mentionUser('guilherme.gazzo')])])],
],
['~#GENERAL~', [paragraph([strike([mentionChannel('GENERAL')])])]],
[
'~test @guilherme.gazzo~',
[paragraph([strike([plain('test '), mentionUser('guilherme.gazzo')])])],
],
[
'~test #GENERAL~',
[paragraph([strike([plain('test '), mentionChannel('GENERAL')])])],
],
[
'~~[A brand new Gist](https://gist.github.com/24dddfa97bef58f46ac2ce0f80c58ba4)~~',
[
Expand Down
30 changes: 29 additions & 1 deletion packages/message-parser/tests/strongEmphasis.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import { parse } from '../src';
import { bold, link, paragraph, plain, italic, strike } from '../src/utils';
import {
bold,
link,
paragraph,
plain,
italic,
strike,
emoji,
emojiUnicode,
mentionChannel,
mentionUser,
} from '../src/utils';

test.each([
['*:smile:*', [paragraph([bold([emoji('smile')])])]],
[
'*test :smile: test*',
[paragraph([bold([plain('test '), emoji('smile'), plain(' test')])])],
],
['*😀*', [paragraph([bold([emojiUnicode('😀')])])]],
['*test 😀*', [paragraph([bold([plain('test '), emojiUnicode('😀')])])]],
['*@guilherme.gazzo*', [paragraph([bold([mentionUser('guilherme.gazzo')])])]],
['*#GENERAL*', [paragraph([bold([mentionChannel('GENERAL')])])]],
[
'*test @guilherme.gazzo*',
[paragraph([bold([plain('test '), mentionUser('guilherme.gazzo')])])],
],
[
'*test #GENERAL*',
[paragraph([bold([plain('test '), mentionChannel('GENERAL')])])],
],
[
'*[A brand new Gist](https://gist.github.com/24dddfa97bef58f46ac2ce0f80c58ba4)*',
[
Expand Down

0 comments on commit 036f70a

Please sign in to comment.