Skip to content

Commit

Permalink
fix: Line break fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocostadev committed May 11, 2022
1 parent 88bebf2 commit 7afa65a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
9 changes: 8 additions & 1 deletion packages/message-parser/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ export type Plain = {
value: string;
};

export type LineBreak = {
type: 'LINE_BREAK';
value: string;
};

export type Paragraph = {
type: 'PARAGRAPH';
value: Array<Exclude<Inlines, Paragraph>>;
Expand Down Expand Up @@ -152,6 +157,7 @@ export type Types = {
ORDERED_LIST: OrderedList;
LIST_ITEM: ListItem;
IMAGE: Image;
LINE_BREAK: LineBreak;
};

export type ASTNode =
Expand Down Expand Up @@ -195,6 +201,7 @@ export type Blocks =
| ListItem
| Tasks
| OrderedList
| UnorderedList;
| UnorderedList
| LineBreak;

export type MarkdownAST = Array<Paragraph | Blocks> | [BigEmoji];
11 changes: 4 additions & 7 deletions packages/message-parser/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
orderedList,
listItem,
unorderedList,
lineBreak,
} = require('./utils');
}

Expand All @@ -32,6 +33,8 @@ start

b = (EndOfLine / Space)*

LineBreak = (Space* EndOfLine) { return lineBreak(''); }

BigEmoji
= b e1:Emoji b e2:Emoji? b e3:Emoji? b {
return [bigEmoji([e1, e2, e3].filter(Boolean))];
Expand All @@ -44,6 +47,7 @@ Blocks
/ TaskList
/ OrderedList
/ UnorderedList
/ LineBreak

// / Section

Expand Down Expand Up @@ -96,13 +100,6 @@ line

EOF = !.

crlf
= "\r\n"
/ "\r"
/ "\n"

EatLine = (!crlf !EOF .)*

EndOfLine
= "\r\n"
/ "\n"
Expand Down
2 changes: 2 additions & 0 deletions packages/message-parser/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,5 @@ export const reducePlainTexts = (

return [...result, item];
}, [] as Paragraph['value']);

export const lineBreak = generate('LINE_BREAK');
35 changes: 35 additions & 0 deletions packages/message-parser/tests/lineBreak.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { parser } from '../src';
import { lineBreak, paragraph, plain } from '../src/utils';

test.each([
[
`test
test2`,
[paragraph([plain('test')]), lineBreak(''), paragraph([plain('test2')])],
],
[
`test
test2
`,
[paragraph([plain('test')]), lineBreak(''), paragraph([plain('test2')])],
],
[
`test
test2
`,
[
paragraph([plain('test')]),
lineBreak(''),
lineBreak(''),
lineBreak(''),
paragraph([plain('test2')]),
],
],
])('parses %p', (input, output) => {
expect(parser(input)).toMatchObject(output);
});

0 comments on commit 7afa65a

Please sign in to comment.