Skip to content

Commit

Permalink
fix(message-parser): Made changes in grammar.pegjs for the strikedown…
Browse files Browse the repository at this point in the history
… approach (#1256)

Co-authored-by: Hugo Costa <hugocarreiracosta@gmail.com>
  • Loading branch information
dougfabris and hugocostadev committed Jan 15, 2024
1 parent 769e5ea commit 7fdfdb1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-melons-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/message-parser": patch
---

fix(message-parser): Made changes in grammar.pegjs for the strikedown approach
22 changes: 19 additions & 3 deletions packages/message-parser/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,37 @@ export type MarkupExcluding<T extends Markup> = Exclude<Markup, T>;
export type Bold = {
type: 'BOLD';
value: Array<
MarkupExcluding<Bold> | Link | Emoji | UserMention | ChannelMention
| MarkupExcluding<Bold>
| Link
| Emoji
| UserMention
| ChannelMention
| InlineCode
>;
};

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

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

Expand Down
10 changes: 6 additions & 4 deletions packages/message-parser/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ ItalicContentItems = text:ItalicContentItem+ { return reducePlainTexts(text); }
ItalicContentItem
= Whitespace
/ InlineCode
/ References
/ UserMention
/ ChannelMention
Expand All @@ -388,12 +389,12 @@ Bold = [\x2A] [\x2A] @BoldContent [\x2A] [\x2A] / [\x2A] @BoldContent [\x2A]
BoldContent = text:BoldContentItem+ { return bold(reducePlainTexts(text)); }
BoldContentItem = Whitespace / References / UserMention / ChannelMention / Italic / Strikethrough / Emoji / Emoticon / AnyBold / Line
BoldContentItem = Whitespace / InlineCode / References / UserMention / ChannelMention / Italic / Strikethrough / Emoji / Emoticon / AnyBold / Line
/* Strike */
Strikethrough = [\x7E] [\x7E] @StrikethroughContent [\x7E] [\x7E] / [\x7E] @StrikethroughContent [\x7E]
StrikethroughContent = text:(Whitespace / References / UserMention / ChannelMention / Italic / Bold / Emoji / Emoticon / AnyStrike / Line)+ {
StrikethroughContent = text:(InlineCode / Whitespace / References / UserMention / ChannelMention / Italic / Bold / Emoji / Emoticon / AnyStrike / Line)+ {
return strike(reducePlainTexts(text));
}
Expand Down Expand Up @@ -423,16 +424,17 @@ ItalicContentItemForReferences
/ Emoticon
/ AnyItalic
/ Line
/ InlineCode
/* Bold for References */
BoldForReferences = [\x2A] [\x2A] @BoldContentForReferences [\x2A] [\x2A] / [\x2A] @BoldContentForReferences [\x2A]
BoldContentForReferences = text:(Whitespace / UserMention / ChannelMention / ItalicForReferences / StrikethroughForReferences / Emoji / Emoticon / AnyBold / Line)+ { return bold(reducePlainTexts(text)); }
BoldContentForReferences = text:(Whitespace / UserMention / ChannelMention / ItalicForReferences / StrikethroughForReferences / Emoji / Emoticon / AnyBold / Line / InlineCode)+ { return bold(reducePlainTexts(text)); }
/* Strike for References */
StrikethroughForReferences = [\x7E] [\x7E] @StrikethroughContentForReferences [\x7E] [\x7E] / [\x7E] @StrikethroughContentForReferences [\x7E]
StrikethroughContentForReferences = text:(Whitespace / UserMention / ChannelMention / ItalicForReferences / BoldForReferences / Emoji / Emoticon / AnyStrike / Line)+ {
StrikethroughContentForReferences = text:(Whitespace / UserMention / ChannelMention / ItalicForReferences / BoldForReferences / Emoji / Emoticon / AnyStrike / Line / InlineCode)+ {
return strike(reducePlainTexts(text));
}
Expand Down
46 changes: 46 additions & 0 deletions packages/message-parser/tests/inlineCodeStrike.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { parse } from '../src';
import {
bold,
inlineCode,
italic,
paragraph,
plain,
strike,
} from '../src/utils';

test.each([
[
'~~`Striking Inline Code`~~',
[paragraph([strike([inlineCode(plain('Striking Inline Code'))])])],
],
[
'~~_`Striking Inline Code with Italics`_~~',
[
paragraph([
strike([
italic([inlineCode(plain('Striking Inline Code with Italics'))]),
]),
]),
],
],
[
'~~**`Striking Inline Code with Bold`**~~',
[
paragraph([
strike([bold([inlineCode(plain('Striking Inline Code with Bold'))])]),
]),
],
],
[
'~~__*`Striking Inline Code with Bold`*__~~',
[
paragraph([
strike([
italic([bold([inlineCode(plain('Striking Inline Code with Bold'))])]),
]),
]),
],
],
])('parses %p', (input, output) => {
expect(parse(input)).toMatchObject(output);
});

0 comments on commit 7fdfdb1

Please sign in to comment.