Skip to content

Commit 51d5d66

Browse files
authored
feat(message-parser): LineBreak token and OrderedListItem digit (#720)
feat(message-parser): LineBreak token and OrderedListItem digit
2 parents bae3515 + a0614d5 commit 51d5d66

File tree

5 files changed

+72
-37
lines changed

5 files changed

+72
-37
lines changed

packages/message-parser/src/definitions.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type Blockquote = {
55

66
export type OrderedList = {
77
type: 'ORDERED_LIST';
8-
value: ListItem[];
8+
value: OrderedListItem[];
99
};
1010

1111
export type UnorderedList = {
@@ -18,6 +18,11 @@ export type ListItem = {
1818
value: Inlines[];
1919
};
2020

21+
export type OrderedListItem = {
22+
type: 'ORDERED_LIST_ITEM';
23+
value: { digit: string; text: Inlines[] };
24+
};
25+
2126
export type Tasks = {
2227
type: 'TASKS';
2328
value: Task[];
@@ -98,6 +103,11 @@ export type Plain = {
98103
value: string;
99104
};
100105

106+
export type LineBreak = {
107+
type: 'LINE_BREAK';
108+
value: string;
109+
};
110+
101111
export type Paragraph = {
102112
type: 'PARAGRAPH';
103113
value: Array<Exclude<Inlines, Paragraph>>;
@@ -150,8 +160,10 @@ export type Types = {
150160
TASK: Task;
151161
UNORDERED_LIST: UnorderedList;
152162
ORDERED_LIST: OrderedList;
163+
ORDERED_LIST_ITEM: OrderedListItem;
153164
LIST_ITEM: ListItem;
154165
IMAGE: Image;
166+
LINE_BREAK: LineBreak;
155167
};
156168

157169
export type ASTNode =
@@ -195,6 +207,7 @@ export type Blocks =
195207
| ListItem
196208
| Tasks
197209
| OrderedList
198-
| UnorderedList;
210+
| UnorderedList
211+
| LineBreak;
199212

200213
export type MarkdownAST = Array<Paragraph | Blocks> | [BigEmoji];

packages/message-parser/src/grammar.pegjs

+6-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
task,
2323
orderedList,
2424
listItem,
25+
orderedListItem,
2526
unorderedList,
27+
lineBreak,
2628
} = require('./utils');
2729
}
2830

@@ -32,6 +34,8 @@ start
3234

3335
b = (EndOfLine / Space)*
3436

37+
LineBreak = (Space* EndOfLine) { return lineBreak(''); }
38+
3539
BigEmoji
3640
= b e1:Emoji b e2:Emoji? b e3:Emoji? b {
3741
return [bigEmoji([e1, e2, e3].filter(Boolean))];
@@ -44,6 +48,7 @@ Blocks
4448
/ TaskList
4549
/ OrderedList
4650
/ UnorderedList
51+
/ LineBreak
4752

4853
// / Section
4954

@@ -96,13 +101,6 @@ line
96101

97102
EOF = !.
98103

99-
crlf
100-
= "\r\n"
101-
/ "\r"
102-
/ "\n"
103-
104-
EatLine = (!crlf !EOF .)*
105-
106104
EndOfLine
107105
= "\r\n"
108106
/ "\n"
@@ -303,7 +301,7 @@ UnorderedListItem__Inline
303301
OrderedList = lists:OrderedListItem+ { return orderedList(lists); }
304302

305303
OrderedListItem
306-
= (digit1_9+ "\x2E ") text:Inline { return listItem(text, true); }
304+
= d:digits "\x2E " text:Inline { return orderedListItem(d, text); }
307305

308306
Codetype = t:[a-zA-Z0-9 \_\-.]+ { return t.join(''); }
309307

packages/message-parser/src/utils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
Paragraph,
88
Types,
99
Task,
10+
Inlines,
1011
} from './definitions';
1112

1213
const generate =
@@ -86,7 +87,10 @@ export const unorderedList = generate('UNORDERED_LIST');
8687

8788
export const listItem = generate('LIST_ITEM');
8889

89-
export const list = generate('ORDERED_LIST');
90+
export const orderedListItem = (() => {
91+
const fn = generate('ORDERED_LIST_ITEM');
92+
return (digit: string, text: Inlines[]) => fn({ digit, text });
93+
})();
9094

9195
export const mentionUser = (() => {
9296
const fn = generate('MENTION_USER');
@@ -112,3 +116,5 @@ export const reducePlainTexts = (
112116

113117
return [...result, item];
114118
}, [] as Paragraph['value']);
119+
120+
export const lineBreak = generate('LINE_BREAK');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { parser } from '../src';
2+
import { lineBreak, paragraph, plain } from '../src/utils';
3+
4+
test.each([
5+
[
6+
`test
7+
8+
test2`,
9+
[paragraph([plain('test')]), lineBreak(''), paragraph([plain('test2')])],
10+
],
11+
[
12+
`test
13+
14+
test2
15+
`,
16+
[paragraph([plain('test')]), lineBreak(''), paragraph([plain('test2')])],
17+
],
18+
[
19+
`test
20+
21+
22+
23+
test2
24+
`,
25+
[
26+
paragraph([plain('test')]),
27+
lineBreak(''),
28+
lineBreak(''),
29+
lineBreak(''),
30+
paragraph([plain('test2')]),
31+
],
32+
],
33+
])('parses %p', (input, output) => {
34+
expect(parser(input)).toMatchObject(output);
35+
});
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,25 @@
11
import { parser } from '../src';
2-
import { bold, plain, orderedList, listItem } from '../src/utils';
2+
import { bold, plain, orderedList, orderedListItem } from '../src/utils';
33

44
test.each([
55
[
66
`
7-
1. First item
7+
7. First item
88
2. Second item
9-
3. Third item
9+
8. Third item
1010
4. *Fourth item*
11+
15. *Fifteenth item*
1112
`.trim(),
1213
[
1314
orderedList([
14-
listItem([plain('First item')]),
15-
listItem([plain('Second item')]),
16-
listItem([plain('Third item')]),
17-
listItem([bold([plain('Fourth item')])]),
15+
orderedListItem('7', [plain('First item')]),
16+
orderedListItem('2', [plain('Second item')]),
17+
orderedListItem('8', [plain('Third item')]),
18+
orderedListItem('4', [bold([plain('Fourth item')])]),
19+
orderedListItem('15', [bold([plain('Fifteenth item')])]),
1820
]),
1921
],
2022
],
21-
22-
// [
23-
// `
24-
// 1. First item
25-
// 2. Second item
26-
// 3. Third item
27-
// 1. First item
28-
// 2. Second item
29-
// 4. *Fourth item*
30-
// `.trim(),
31-
// [
32-
// orderedList([
33-
// listItem([plain('First item')]),
34-
// listItem([plain('Second item')]),
35-
// listItem([plain('Third item')]),
36-
// listItem([bold([plain('Fourth item')])]),
37-
// ]),
38-
// ],
39-
// ],
4023
])('parses %p', (input, output) => {
4124
expect(parser(input)).toMatchObject(output);
4225
});

0 commit comments

Comments
 (0)