-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.test.ts
174 lines (158 loc) · 4.06 KB
/
parser.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { ADFEntity, Formatter, formatAdf } from '../src';
import { headings2Adf, simpleMarksAdf } from './adf.fixtures';
const simpleAdf: ADFEntity = {
version: 1,
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Hello ',
},
{
type: 'text',
text: 'world',
marks: [
{
type: 'strong',
},
],
},
],
},
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Hello ',
},
{
type: 'text',
text: 'ADF',
},
],
},
],
};
describe(`ADF parsing`, () => {
it('should strip markup', () => {
const stripped = formatAdf(simpleAdf, {
default: () => '',
nodes: {
doc: (_n, children) => children().join(''),
paragraph: (_n, children) => children().join(''),
text: (t) => t.text || '',
},
marks: {},
});
expect(stripped).toBe('Hello worldHello ADF');
});
it('should visit every node', () => {
const toNestedString = (node: ADFEntity, processChildren: () => string[]) =>
`${node.type}(${processChildren().join(',')})`;
const formatter: Formatter<string> = {
default: () => '',
nodes: {
doc: toNestedString,
paragraph: toNestedString,
text: (e) => e.type,
},
marks: {},
};
const result = formatAdf(simpleAdf, formatter);
expect(result).toBe('doc(paragraph(text,text),paragraph(text,text))');
});
it("won't skip block nodes without formatter implementations", async () => {
const adf: ADFEntity = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'my data',
},
],
},
],
},
],
};
const formatter: Formatter<string | undefined> = {
default: (_e, c) => c().join(''),
nodes: {
text: (t) => t.text,
},
marks: {},
};
expect(formatAdf(adf, formatter)).toBe('my data');
});
it('should apply all marks', () => {
const formatter: Formatter<string> = {
default: (_node, children) => children().join('-'),
nodes: {},
marks: {
text: {
em: (_mark, next) => `e(${next()})`,
strong: (_mark, next) => `s(${next()})`,
},
},
};
expect(formatAdf(simpleMarksAdf, formatter)).toBe('s()-e(s())-s()');
});
it('can exclude sub-trees per type', () => {
const formatter: Formatter<string> = {
default: () => '',
nodes: {
doc: (_e, children) => children().join(''), // recurse into children
paragraph: () => 'p', // output a fixed string
text: () =>
'This is never called, as paragraph does not call children()',
},
marks: {},
};
const result = formatAdf(simpleAdf, formatter);
expect(result).toBe('pp');
});
it('should recurse into all children if told so in default', () => {
const f: Formatter<number> = {
default: (_e, children) =>
children().reduce((acc, curr) => acc + curr, 0),
nodes: {
text: (node) => node.text?.length || 0,
},
marks: {},
};
expect(formatAdf(simpleAdf, f)).toEqual('Hello WorldHello ADF'.length);
});
it('should support producing outlines', () => {
const f: Formatter<string> = {
default: (_node) => '', // don't recurse into unknown nodes
nodes: {
doc: (_node, children) => children().join(''),
heading: (node, children) =>
' '.repeat(parseInt(node.attrs?.level as string) - 1) +
children() +
'\n',
text: (node) => node.text || '',
},
marks: {},
};
//prettier-ignore
expect(formatAdf(headings2Adf, f)).toEqual(`Heading 1
Heading 1.1
Heading 1.2
Heading 2
Heading 3
Heading 3.1
Heading 3.1.1
`);
});
});