document-ir 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/esm/ArrayCollapseTransformer.d.ts +5 -0
- package/esm/ArrayCollapseTransformer.js +18 -0
- package/esm/ArrayCollapseTransformer_test.d.ts +1 -0
- package/esm/ExampleDocument.d.ts +2 -0
- package/esm/IdentityTransformer.d.ts +53 -0
- package/esm/IdentityTransformer.js +688 -0
- package/esm/IdentityTransformer_test.d.ts +1 -0
- package/esm/NodeVisitor.d.ts +53 -0
- package/esm/NodeVisitor.js +369 -0
- package/esm/TextCollapseTransformer.d.ts +5 -0
- package/esm/TextCollapseTransformer.js +36 -0
- package/esm/TextCollapseTransformer_test.d.ts +1 -0
- package/esm/TextVisitor.d.ts +12 -0
- package/esm/TextVisitor.js +36 -0
- package/esm/TextVisitor_test.d.ts +1 -0
- package/esm/WhitespaceStretchingTransformer.d.ts +26 -0
- package/esm/WhitespaceStretchingTransformer.js +119 -0
- package/esm/WhitespaceStretchingTransformer_test.d.ts +1 -0
- package/esm/WhitespaceTransformer.d.ts +12 -0
- package/esm/WhitespaceTransformer.js +75 -0
- package/esm/WhitespaceTransformer_test.d.ts +1 -0
- package/esm/_dnt.test_shims.d.ts +5 -0
- package/esm/deps/deno.land/std@0.192.0/fmt/colors.d.ts +270 -0
- package/esm/deps/deno.land/std@0.192.0/testing/_diff.d.ts +26 -0
- package/esm/deps/deno.land/std@0.192.0/testing/_format.d.ts +1 -0
- package/esm/deps/deno.land/std@0.192.0/testing/asserts.d.ts +284 -0
- package/esm/index.d.ts +8 -0
- package/esm/index.js +8 -0
- package/esm/package.json +3 -0
- package/esm/types.d.ts +307 -0
- package/esm/types.js +1 -0
- package/package.json +29 -0
- package/script/ArrayCollapseTransformer.d.ts +5 -0
- package/script/ArrayCollapseTransformer.js +22 -0
- package/script/ArrayCollapseTransformer_test.d.ts +1 -0
- package/script/ExampleDocument.d.ts +2 -0
- package/script/IdentityTransformer.d.ts +53 -0
- package/script/IdentityTransformer.js +692 -0
- package/script/IdentityTransformer_test.d.ts +1 -0
- package/script/NodeVisitor.d.ts +53 -0
- package/script/NodeVisitor.js +373 -0
- package/script/TextCollapseTransformer.d.ts +5 -0
- package/script/TextCollapseTransformer.js +40 -0
- package/script/TextCollapseTransformer_test.d.ts +1 -0
- package/script/TextVisitor.d.ts +12 -0
- package/script/TextVisitor.js +40 -0
- package/script/TextVisitor_test.d.ts +1 -0
- package/script/WhitespaceStretchingTransformer.d.ts +26 -0
- package/script/WhitespaceStretchingTransformer.js +123 -0
- package/script/WhitespaceStretchingTransformer_test.d.ts +1 -0
- package/script/WhitespaceTransformer.d.ts +12 -0
- package/script/WhitespaceTransformer.js +79 -0
- package/script/WhitespaceTransformer_test.d.ts +1 -0
- package/script/_dnt.test_shims.d.ts +5 -0
- package/script/deps/deno.land/std@0.192.0/fmt/colors.d.ts +270 -0
- package/script/deps/deno.land/std@0.192.0/testing/_diff.d.ts +26 -0
- package/script/deps/deno.land/std@0.192.0/testing/_format.d.ts +1 -0
- package/script/deps/deno.land/std@0.192.0/testing/asserts.d.ts +284 -0
- package/script/index.d.ts +8 -0
- package/script/index.js +32 -0
- package/script/package.json +3 -0
- package/script/types.d.ts +307 -0
- package/script/types.js +2 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ArrayNode, BlockNode, BlockQuoteNode, BoldNode, BreakNode, BubbleNode, CardNode, CenterNode, CodeNode, ColumnsNode, DefinitionListNode, DefinitionNode, DefinitionReferenceNode, DocumentNode, EmbedNode, EmojiNode, FigureCaptionNode, FigureImageNode, FigureNode, FormattedTextNode, HeaderNode, HighTechAlertNode, HorizontalRuleNode, ImageNode, ItalicNode, LinkNode, ListNode, Node, NoteNode, ParagraphNode, QuoteNode, RedactedNode, RegionNode, ScriptNode, SecretNode, SmallerNode, SocialNode, StickerNode, StrikeThroughNode, TableNode, TextNode, UnderlineNode, VideoNode, WarningNode } from "./types.js";
|
|
2
|
+
export declare class NodeVisitor {
|
|
3
|
+
protected beforeBlock(): void;
|
|
4
|
+
protected afterBlock(): void;
|
|
5
|
+
protected beforeInline(): void;
|
|
6
|
+
protected afterInline(): void;
|
|
7
|
+
protected chooseChildren(nodes: Node[]): void;
|
|
8
|
+
protected block(node: BlockNode): void;
|
|
9
|
+
protected blockQuote(node: BlockQuoteNode): void;
|
|
10
|
+
protected bold(node: BoldNode): void;
|
|
11
|
+
protected break_(_node: BreakNode): void;
|
|
12
|
+
protected bubble(node: BubbleNode): void;
|
|
13
|
+
protected center(node: CenterNode): void;
|
|
14
|
+
protected code(node: CodeNode): void;
|
|
15
|
+
protected columns(node: ColumnsNode): void;
|
|
16
|
+
protected definition(node: DefinitionNode): void;
|
|
17
|
+
protected definitionList(node: DefinitionListNode): void;
|
|
18
|
+
protected definitionReference(node: DefinitionReferenceNode): void;
|
|
19
|
+
protected embed(_node: EmbedNode): void;
|
|
20
|
+
protected emoji(_node: EmojiNode): void;
|
|
21
|
+
protected figure(node: FigureNode): void;
|
|
22
|
+
protected figureCaption(node: FigureCaptionNode): void;
|
|
23
|
+
protected figureImage(node: FigureImageNode): void;
|
|
24
|
+
protected formattedText(_node: FormattedTextNode): void;
|
|
25
|
+
protected header(node: HeaderNode): void;
|
|
26
|
+
protected highTechAlert(node: HighTechAlertNode): void;
|
|
27
|
+
protected horizontalRule(_node: HorizontalRuleNode): void;
|
|
28
|
+
protected image(_node: ImageNode): void;
|
|
29
|
+
protected italic(node: ItalicNode): void;
|
|
30
|
+
protected link(node: LinkNode): void;
|
|
31
|
+
protected array(node: ArrayNode): void;
|
|
32
|
+
protected note(node: NoteNode): void;
|
|
33
|
+
protected list(node: ListNode): void;
|
|
34
|
+
protected paragraph(node: ParagraphNode): void;
|
|
35
|
+
protected quote(node: QuoteNode): void;
|
|
36
|
+
protected redacted(node: RedactedNode): void;
|
|
37
|
+
protected region(node: RegionNode): void;
|
|
38
|
+
protected script(_node: ScriptNode): void;
|
|
39
|
+
protected secret(node: SecretNode): void;
|
|
40
|
+
protected smaller(node: SmallerNode): void;
|
|
41
|
+
protected sticker(node: StickerNode): void;
|
|
42
|
+
protected strikeThrough(node: StrikeThroughNode): void;
|
|
43
|
+
protected text(_node: TextNode): void;
|
|
44
|
+
protected table(node: TableNode): void;
|
|
45
|
+
protected social(_node: SocialNode): void;
|
|
46
|
+
protected underline(node: UnderlineNode): void;
|
|
47
|
+
protected video(node: VideoNode): void;
|
|
48
|
+
protected warning(node: WarningNode): void;
|
|
49
|
+
protected card(node: CardNode): void;
|
|
50
|
+
protected choose(node: Node): void;
|
|
51
|
+
protected document(node: DocumentNode): void;
|
|
52
|
+
visit(node: DocumentNode | Node): void;
|
|
53
|
+
}
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
export class NodeVisitor {
|
|
2
|
+
beforeBlock() {
|
|
3
|
+
}
|
|
4
|
+
afterBlock() {
|
|
5
|
+
}
|
|
6
|
+
beforeInline() {
|
|
7
|
+
}
|
|
8
|
+
afterInline() {
|
|
9
|
+
}
|
|
10
|
+
chooseChildren(nodes) {
|
|
11
|
+
for (const child of nodes) {
|
|
12
|
+
this.choose(child);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
block(node) {
|
|
16
|
+
this.beforeBlock();
|
|
17
|
+
this.chooseChildren(node.content);
|
|
18
|
+
this.afterBlock();
|
|
19
|
+
}
|
|
20
|
+
blockQuote(node) {
|
|
21
|
+
this.beforeBlock();
|
|
22
|
+
this.chooseChildren(node.content);
|
|
23
|
+
this.afterBlock();
|
|
24
|
+
}
|
|
25
|
+
bold(node) {
|
|
26
|
+
this.beforeInline();
|
|
27
|
+
this.chooseChildren(node.content);
|
|
28
|
+
this.afterInline();
|
|
29
|
+
}
|
|
30
|
+
break_(_node) {
|
|
31
|
+
this.beforeBlock();
|
|
32
|
+
this.afterBlock();
|
|
33
|
+
}
|
|
34
|
+
bubble(node) {
|
|
35
|
+
this.beforeBlock();
|
|
36
|
+
this.chooseChildren(node.content);
|
|
37
|
+
this.afterBlock();
|
|
38
|
+
}
|
|
39
|
+
center(node) {
|
|
40
|
+
this.beforeBlock();
|
|
41
|
+
this.chooseChildren(node.content);
|
|
42
|
+
this.afterBlock();
|
|
43
|
+
}
|
|
44
|
+
code(node) {
|
|
45
|
+
this.beforeInline();
|
|
46
|
+
this.chooseChildren(node.content);
|
|
47
|
+
this.afterInline();
|
|
48
|
+
}
|
|
49
|
+
columns(node) {
|
|
50
|
+
for (const column of node.columns) {
|
|
51
|
+
this.beforeBlock();
|
|
52
|
+
this.chooseChildren(column);
|
|
53
|
+
this.afterBlock();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
definition(node) {
|
|
57
|
+
this.beforeBlock();
|
|
58
|
+
this.chooseChildren(node.abbreviation);
|
|
59
|
+
this.afterBlock();
|
|
60
|
+
this.beforeBlock();
|
|
61
|
+
this.chooseChildren(node.title);
|
|
62
|
+
this.afterBlock();
|
|
63
|
+
this.beforeBlock();
|
|
64
|
+
this.chooseChildren(node.content);
|
|
65
|
+
this.afterBlock();
|
|
66
|
+
}
|
|
67
|
+
definitionList(node) {
|
|
68
|
+
this.beforeBlock();
|
|
69
|
+
for (const child of node.content) {
|
|
70
|
+
this.definition(child);
|
|
71
|
+
}
|
|
72
|
+
this.afterBlock();
|
|
73
|
+
}
|
|
74
|
+
definitionReference(node) {
|
|
75
|
+
this.beforeInline();
|
|
76
|
+
this.chooseChildren(node.definition.abbreviation);
|
|
77
|
+
this.afterInline();
|
|
78
|
+
this.chooseChildren(node.content);
|
|
79
|
+
}
|
|
80
|
+
embed(_node) {
|
|
81
|
+
}
|
|
82
|
+
emoji(_node) {
|
|
83
|
+
}
|
|
84
|
+
figure(node) {
|
|
85
|
+
this.beforeBlock();
|
|
86
|
+
this.chooseChildren(node.content);
|
|
87
|
+
this.afterBlock();
|
|
88
|
+
}
|
|
89
|
+
figureCaption(node) {
|
|
90
|
+
this.beforeBlock();
|
|
91
|
+
this.chooseChildren(node.content);
|
|
92
|
+
this.afterBlock();
|
|
93
|
+
}
|
|
94
|
+
figureImage(node) {
|
|
95
|
+
this.beforeBlock();
|
|
96
|
+
this.chooseChildren(node.content);
|
|
97
|
+
this.afterBlock();
|
|
98
|
+
}
|
|
99
|
+
formattedText(_node) {
|
|
100
|
+
}
|
|
101
|
+
header(node) {
|
|
102
|
+
this.beforeBlock();
|
|
103
|
+
this.chooseChildren(node.content);
|
|
104
|
+
this.afterBlock();
|
|
105
|
+
}
|
|
106
|
+
highTechAlert(node) {
|
|
107
|
+
this.beforeBlock();
|
|
108
|
+
this.chooseChildren(node.content);
|
|
109
|
+
this.afterBlock();
|
|
110
|
+
this.beforeBlock();
|
|
111
|
+
this.chooseChildren(node.warning);
|
|
112
|
+
this.afterBlock();
|
|
113
|
+
}
|
|
114
|
+
horizontalRule(_node) {
|
|
115
|
+
}
|
|
116
|
+
image(_node) {
|
|
117
|
+
}
|
|
118
|
+
italic(node) {
|
|
119
|
+
this.beforeInline();
|
|
120
|
+
this.chooseChildren(node.content);
|
|
121
|
+
this.afterInline();
|
|
122
|
+
}
|
|
123
|
+
link(node) {
|
|
124
|
+
this.beforeInline();
|
|
125
|
+
this.chooseChildren(node.content);
|
|
126
|
+
this.afterInline();
|
|
127
|
+
}
|
|
128
|
+
array(node) {
|
|
129
|
+
this.chooseChildren(node.content);
|
|
130
|
+
}
|
|
131
|
+
note(node) {
|
|
132
|
+
this.beforeBlock();
|
|
133
|
+
this.chooseChildren(node.content);
|
|
134
|
+
this.afterBlock();
|
|
135
|
+
}
|
|
136
|
+
list(node) {
|
|
137
|
+
for (const item of node.content) {
|
|
138
|
+
this.beforeBlock();
|
|
139
|
+
this.chooseChildren(item.content);
|
|
140
|
+
this.afterBlock();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
paragraph(node) {
|
|
144
|
+
this.beforeBlock();
|
|
145
|
+
this.chooseChildren(node.content);
|
|
146
|
+
this.afterBlock();
|
|
147
|
+
}
|
|
148
|
+
quote(node) {
|
|
149
|
+
this.beforeBlock();
|
|
150
|
+
this.chooseChildren(node.content);
|
|
151
|
+
this.afterBlock();
|
|
152
|
+
}
|
|
153
|
+
redacted(node) {
|
|
154
|
+
if (node.style == "block") {
|
|
155
|
+
this.beforeBlock();
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
this.beforeInline();
|
|
159
|
+
}
|
|
160
|
+
this.chooseChildren(node.content);
|
|
161
|
+
if (node.style == "block") {
|
|
162
|
+
this.afterBlock();
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
this.afterInline();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
region(node) {
|
|
169
|
+
this.beforeBlock();
|
|
170
|
+
this.chooseChildren(node.content);
|
|
171
|
+
this.afterBlock();
|
|
172
|
+
}
|
|
173
|
+
script(_node) {
|
|
174
|
+
}
|
|
175
|
+
secret(node) {
|
|
176
|
+
this.beforeInline();
|
|
177
|
+
this.chooseChildren(node.content);
|
|
178
|
+
this.afterInline();
|
|
179
|
+
}
|
|
180
|
+
smaller(node) {
|
|
181
|
+
this.beforeInline();
|
|
182
|
+
this.chooseChildren(node.content);
|
|
183
|
+
this.afterInline();
|
|
184
|
+
}
|
|
185
|
+
sticker(node) {
|
|
186
|
+
this.beforeBlock();
|
|
187
|
+
this.chooseChildren(node.content);
|
|
188
|
+
this.afterBlock();
|
|
189
|
+
}
|
|
190
|
+
strikeThrough(node) {
|
|
191
|
+
this.beforeInline();
|
|
192
|
+
this.chooseChildren(node.content);
|
|
193
|
+
this.afterInline();
|
|
194
|
+
}
|
|
195
|
+
text(_node) {
|
|
196
|
+
}
|
|
197
|
+
table(node) {
|
|
198
|
+
for (const row of node.content) {
|
|
199
|
+
for (const cell of row) {
|
|
200
|
+
this.beforeBlock();
|
|
201
|
+
this.chooseChildren(cell.content);
|
|
202
|
+
this.afterBlock();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
social(_node) {
|
|
207
|
+
}
|
|
208
|
+
underline(node) {
|
|
209
|
+
this.beforeInline();
|
|
210
|
+
this.chooseChildren(node.content);
|
|
211
|
+
this.afterInline();
|
|
212
|
+
}
|
|
213
|
+
video(node) {
|
|
214
|
+
this.beforeBlock();
|
|
215
|
+
node.content && this.chooseChildren(node.content);
|
|
216
|
+
this.afterBlock();
|
|
217
|
+
}
|
|
218
|
+
warning(node) {
|
|
219
|
+
this.beforeBlock();
|
|
220
|
+
this.chooseChildren(node.content);
|
|
221
|
+
this.afterBlock();
|
|
222
|
+
}
|
|
223
|
+
card(node) {
|
|
224
|
+
if (node.content) {
|
|
225
|
+
this.beforeBlock();
|
|
226
|
+
this.chooseChildren(node.content.content);
|
|
227
|
+
this.afterBlock();
|
|
228
|
+
}
|
|
229
|
+
if (node.attribution) {
|
|
230
|
+
if (node.attribution.title) {
|
|
231
|
+
this.beforeBlock();
|
|
232
|
+
const _title = this.chooseChildren(node.attribution.title);
|
|
233
|
+
this.afterBlock();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (node.header) {
|
|
237
|
+
this.beforeBlock();
|
|
238
|
+
this.chooseChildren(node.header.title);
|
|
239
|
+
this.afterBlock();
|
|
240
|
+
}
|
|
241
|
+
if (node.media) {
|
|
242
|
+
for (const mediaNode of node.media.content) {
|
|
243
|
+
this.beforeBlock();
|
|
244
|
+
this.choose(mediaNode);
|
|
245
|
+
this.afterBlock();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
choose(node) {
|
|
250
|
+
if (!node || !node.type) {
|
|
251
|
+
throw new Error(`Unexpected node, no type: ${JSON.stringify(node)}`);
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
switch (node.type) {
|
|
255
|
+
case "block":
|
|
256
|
+
return this.block(node);
|
|
257
|
+
case "block-quote":
|
|
258
|
+
return this.blockQuote(node);
|
|
259
|
+
case "bold":
|
|
260
|
+
return this.bold(node);
|
|
261
|
+
case "break":
|
|
262
|
+
return this.break_(node);
|
|
263
|
+
case "bubble":
|
|
264
|
+
return this.bubble(node);
|
|
265
|
+
case "card":
|
|
266
|
+
return this.card(node);
|
|
267
|
+
case "center":
|
|
268
|
+
return this.center(node);
|
|
269
|
+
case "code":
|
|
270
|
+
return this.code(node);
|
|
271
|
+
case "columns":
|
|
272
|
+
return this.columns(node);
|
|
273
|
+
case "definition":
|
|
274
|
+
return this.definition(node);
|
|
275
|
+
case "definition-list":
|
|
276
|
+
return this.definitionList(node);
|
|
277
|
+
case "definition-reference":
|
|
278
|
+
return this.definitionReference(node);
|
|
279
|
+
case "embed":
|
|
280
|
+
return this.embed(node);
|
|
281
|
+
case "emoji":
|
|
282
|
+
return this.emoji(node);
|
|
283
|
+
case "figure":
|
|
284
|
+
return this.figure(node);
|
|
285
|
+
case "figure-caption":
|
|
286
|
+
return this.figureCaption(node);
|
|
287
|
+
case "figure-image":
|
|
288
|
+
return this.figureImage(node);
|
|
289
|
+
case "formatted-text":
|
|
290
|
+
return this.formattedText(node);
|
|
291
|
+
case "header":
|
|
292
|
+
return this.header(node);
|
|
293
|
+
case "high-tech-alert":
|
|
294
|
+
return this.highTechAlert(node);
|
|
295
|
+
case "horizontal-rule":
|
|
296
|
+
return this.horizontalRule(node);
|
|
297
|
+
case "image":
|
|
298
|
+
return this.image(node);
|
|
299
|
+
case "italic":
|
|
300
|
+
return this.italic(node);
|
|
301
|
+
case "link":
|
|
302
|
+
return this.link(node);
|
|
303
|
+
case "array":
|
|
304
|
+
return this.array(node);
|
|
305
|
+
case "note":
|
|
306
|
+
return this.note(node);
|
|
307
|
+
case "list":
|
|
308
|
+
return this.list(node);
|
|
309
|
+
case "paragraph":
|
|
310
|
+
return this.paragraph(node);
|
|
311
|
+
case "quote":
|
|
312
|
+
return this.quote(node);
|
|
313
|
+
case "redacted":
|
|
314
|
+
return this.redacted(node);
|
|
315
|
+
case "region":
|
|
316
|
+
return this.region(node);
|
|
317
|
+
case "script":
|
|
318
|
+
return this.script(node);
|
|
319
|
+
case "secret":
|
|
320
|
+
return this.secret(node);
|
|
321
|
+
case "smaller":
|
|
322
|
+
return this.smaller(node);
|
|
323
|
+
case "sticker":
|
|
324
|
+
return this.sticker(node);
|
|
325
|
+
case "strike-through":
|
|
326
|
+
return this.strikeThrough(node);
|
|
327
|
+
case "table":
|
|
328
|
+
return this.table(node);
|
|
329
|
+
case "text":
|
|
330
|
+
return this.text(node);
|
|
331
|
+
case "toot":
|
|
332
|
+
case "tweet":
|
|
333
|
+
case "vimeo":
|
|
334
|
+
case "youtube":
|
|
335
|
+
return this.social(node);
|
|
336
|
+
case "underline":
|
|
337
|
+
return this.underline(node);
|
|
338
|
+
case "video":
|
|
339
|
+
return this.video(node);
|
|
340
|
+
case "warning":
|
|
341
|
+
return this.warning(node);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
catch (e) {
|
|
345
|
+
console.log(`Got exception while processing node: ${JSON.stringify(node)}`);
|
|
346
|
+
throw e;
|
|
347
|
+
}
|
|
348
|
+
//@ts-ignore Fall through
|
|
349
|
+
console.error(`Unsupported type ${node.type}`);
|
|
350
|
+
}
|
|
351
|
+
document(node) {
|
|
352
|
+
this.beforeBlock();
|
|
353
|
+
this.chooseChildren(node.content);
|
|
354
|
+
this.afterBlock();
|
|
355
|
+
this.beforeBlock();
|
|
356
|
+
if (node.definitions) {
|
|
357
|
+
this.chooseChildren(node.definitions);
|
|
358
|
+
}
|
|
359
|
+
this.afterBlock();
|
|
360
|
+
}
|
|
361
|
+
visit(node) {
|
|
362
|
+
if (node.type == "document") {
|
|
363
|
+
this.document(node);
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
this.choose(node);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { IdentityTransformer } from "./IdentityTransformer.js";
|
|
2
|
+
export class TextCollapseTransformer extends IdentityTransformer {
|
|
3
|
+
async chooseChildren(nodes) {
|
|
4
|
+
const children = await super.chooseChildren(nodes);
|
|
5
|
+
const results = [];
|
|
6
|
+
let lastText = null;
|
|
7
|
+
for (const child of children) {
|
|
8
|
+
if (child.type == "text") {
|
|
9
|
+
if (lastText != null) {
|
|
10
|
+
lastText = `${lastText}${child.text}`;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
lastText = child.text;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
if (lastText != null) {
|
|
18
|
+
results.push({
|
|
19
|
+
type: "text",
|
|
20
|
+
text: lastText,
|
|
21
|
+
});
|
|
22
|
+
lastText = null;
|
|
23
|
+
}
|
|
24
|
+
results.push(child);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (lastText != null) {
|
|
28
|
+
results.push({
|
|
29
|
+
type: "text",
|
|
30
|
+
text: lastText,
|
|
31
|
+
});
|
|
32
|
+
lastText = null;
|
|
33
|
+
}
|
|
34
|
+
return results;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EmojiNode, FigureImageNode, ImageNode, TextNode, VideoNode } from "./types.js";
|
|
2
|
+
import { NodeVisitor } from "./NodeVisitor.js";
|
|
3
|
+
export declare class TextVisitor extends NodeVisitor {
|
|
4
|
+
private textList;
|
|
5
|
+
constructor();
|
|
6
|
+
protected text(node: TextNode): void;
|
|
7
|
+
protected video(node: VideoNode): void;
|
|
8
|
+
protected image(node: ImageNode): void;
|
|
9
|
+
protected emoji(node: EmojiNode): void;
|
|
10
|
+
protected figureImage(node: FigureImageNode): void;
|
|
11
|
+
getText(): string;
|
|
12
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { NodeVisitor } from "./NodeVisitor.js";
|
|
2
|
+
export class TextVisitor extends NodeVisitor {
|
|
3
|
+
constructor() {
|
|
4
|
+
super();
|
|
5
|
+
Object.defineProperty(this, "textList", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true,
|
|
9
|
+
value: void 0
|
|
10
|
+
});
|
|
11
|
+
this.textList = [];
|
|
12
|
+
}
|
|
13
|
+
text(node) {
|
|
14
|
+
this.textList.push(node.text);
|
|
15
|
+
super.text(node);
|
|
16
|
+
}
|
|
17
|
+
video(node) {
|
|
18
|
+
this.textList.push(node.alt);
|
|
19
|
+
super.video(node);
|
|
20
|
+
}
|
|
21
|
+
image(node) {
|
|
22
|
+
this.textList.push(node.alt);
|
|
23
|
+
super.image(node);
|
|
24
|
+
}
|
|
25
|
+
emoji(node) {
|
|
26
|
+
this.textList.push(node.alt);
|
|
27
|
+
super.emoji(node);
|
|
28
|
+
}
|
|
29
|
+
figureImage(node) {
|
|
30
|
+
this.textList.push(node.alt);
|
|
31
|
+
super.figureImage(node);
|
|
32
|
+
}
|
|
33
|
+
getText() {
|
|
34
|
+
return this.textList.join("");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { IdentityTransformer } from "./IdentityTransformer.js";
|
|
2
|
+
import { DocumentNode, Node, TextNode } from "./types.js";
|
|
3
|
+
interface BlockInfo {
|
|
4
|
+
type: '_block';
|
|
5
|
+
content: WhiteSpaceContainer[];
|
|
6
|
+
parent: BlockInfo | InlineInfo | null;
|
|
7
|
+
}
|
|
8
|
+
interface InlineInfo {
|
|
9
|
+
type: '_inline';
|
|
10
|
+
content: WhiteSpaceContainer[];
|
|
11
|
+
parent: BlockInfo | InlineInfo | null;
|
|
12
|
+
}
|
|
13
|
+
type WhiteSpaceContainer = BlockInfo | InlineInfo | TextNode;
|
|
14
|
+
export declare class WhitespaceStretchingTransformer extends IdentityTransformer {
|
|
15
|
+
private root;
|
|
16
|
+
private cursor;
|
|
17
|
+
constructor();
|
|
18
|
+
protected beforeBlock(): Promise<void>;
|
|
19
|
+
protected afterBlock(): Promise<void>;
|
|
20
|
+
protected beforeInline(): Promise<void>;
|
|
21
|
+
protected afterInline(): Promise<void>;
|
|
22
|
+
protected reviewBlock(block: BlockInfo): void;
|
|
23
|
+
protected text(node: TextNode): Promise<Node | null>;
|
|
24
|
+
transform(node: DocumentNode): Promise<DocumentNode>;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { IdentityTransformer } from "./IdentityTransformer.js";
|
|
2
|
+
export class WhitespaceStretchingTransformer extends IdentityTransformer {
|
|
3
|
+
constructor() {
|
|
4
|
+
super();
|
|
5
|
+
Object.defineProperty(this, "root", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true,
|
|
9
|
+
value: void 0
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(this, "cursor", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: void 0
|
|
16
|
+
});
|
|
17
|
+
this.root = {
|
|
18
|
+
type: '_block',
|
|
19
|
+
content: [],
|
|
20
|
+
parent: null
|
|
21
|
+
};
|
|
22
|
+
this.cursor = this.root;
|
|
23
|
+
}
|
|
24
|
+
// deno-lint-ignore require-await
|
|
25
|
+
async beforeBlock() {
|
|
26
|
+
const parent = this.cursor;
|
|
27
|
+
const block = {
|
|
28
|
+
type: '_block',
|
|
29
|
+
content: [],
|
|
30
|
+
parent
|
|
31
|
+
};
|
|
32
|
+
parent.content.push(block);
|
|
33
|
+
this.cursor = block;
|
|
34
|
+
}
|
|
35
|
+
// deno-lint-ignore require-await
|
|
36
|
+
async afterBlock() {
|
|
37
|
+
if (this.cursor.parent) {
|
|
38
|
+
this.cursor = this.cursor.parent;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// deno-lint-ignore require-await
|
|
42
|
+
async beforeInline() {
|
|
43
|
+
const parent = this.cursor;
|
|
44
|
+
const inline = {
|
|
45
|
+
type: '_inline',
|
|
46
|
+
content: [],
|
|
47
|
+
parent
|
|
48
|
+
};
|
|
49
|
+
parent.content.push(inline);
|
|
50
|
+
this.cursor = inline;
|
|
51
|
+
}
|
|
52
|
+
// deno-lint-ignore require-await
|
|
53
|
+
async afterInline() {
|
|
54
|
+
if (this.cursor.parent) {
|
|
55
|
+
this.cursor = this.cursor.parent;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
reviewBlock(block) {
|
|
59
|
+
const nodes = [];
|
|
60
|
+
const visit = (r, level) => {
|
|
61
|
+
if (r.type == 'text') {
|
|
62
|
+
nodes.push({
|
|
63
|
+
node: r,
|
|
64
|
+
level
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
else if (r.type == '_block') {
|
|
68
|
+
this.reviewBlock(r);
|
|
69
|
+
nodes.push({ node: null, level });
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
for (const node of r.content) {
|
|
73
|
+
visit(node, level + 1);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
for (const node of block.content) {
|
|
78
|
+
visit(node, 0);
|
|
79
|
+
}
|
|
80
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
81
|
+
const first = nodes[i];
|
|
82
|
+
const second = i + 1 < nodes.length ? nodes[i + 1] : { node: null, level: 0 };
|
|
83
|
+
// No use acting on a dead node
|
|
84
|
+
if (!first.node || !second.node) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
// Interesting things only happen on differing levels
|
|
88
|
+
if (first.level == second.level) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (first.level < second.level) {
|
|
92
|
+
if (second.node.text.startsWith(' ')) {
|
|
93
|
+
second.node.text = second.node.text.slice(1);
|
|
94
|
+
first.node.text += ' ';
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (first.level > second.level) {
|
|
98
|
+
if (first.node.text.endsWith(' ')) {
|
|
99
|
+
first.node.text = first.node.text.slice(0, -1);
|
|
100
|
+
second.node.text = ` ${second.node.text}`;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// deno-lint-ignore require-await
|
|
106
|
+
async text(node) {
|
|
107
|
+
const replacement = {
|
|
108
|
+
type: 'text',
|
|
109
|
+
text: `${node.text}`
|
|
110
|
+
};
|
|
111
|
+
this.cursor.content.push(replacement);
|
|
112
|
+
return replacement;
|
|
113
|
+
}
|
|
114
|
+
async transform(node) {
|
|
115
|
+
const result = await super.transform(node);
|
|
116
|
+
this.reviewBlock(this.root);
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IdentityTransformer } from "./IdentityTransformer.js";
|
|
2
|
+
import { DocumentNode, Node, TextNode } from "./types.js";
|
|
3
|
+
export declare class WhitespaceTransformer extends IdentityTransformer {
|
|
4
|
+
private stripWhitespace;
|
|
5
|
+
private lastText;
|
|
6
|
+
constructor();
|
|
7
|
+
protected text(node: TextNode): Promise<Node | null>;
|
|
8
|
+
private stripLastText;
|
|
9
|
+
protected beforeBlock(): Promise<void>;
|
|
10
|
+
protected afterBlock(): Promise<void>;
|
|
11
|
+
transform(node: DocumentNode): Promise<DocumentNode>;
|
|
12
|
+
}
|