/
walkthroughHelpers.js
402 lines (335 loc) · 10.1 KB
/
walkthroughHelpers.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import asciidoctor from 'asciidoctor.js';
const CONTEXT_SECTION = 'section';
const CONTEXT_DOCUMENT = 'document';
const CONTEXT_SIDEBAR = 'sidebar';
const BLOCK_ATTR_TYPE = 'type';
const BLOCK_ATTR_TIME = 'time';
const BLOCK_TYPE_VERIFICATION = 'verification';
const BLOCK_TYPE_VERIFICATION_FAIL = 'verificationFail';
const BLOCK_TYPE_VERIFICATION_SUCCESS = 'verificationSuccess';
const BLOCK_TYPE_TASK_RESOURCE = 'taskResource';
const BLOCK_TYPE_WALKTHROUGH_RESOURCE = 'walkthroughResource';
const BLOCK_LEVEL_PREAMBLE = 0;
const BLOCK_LEVEL_TASK = 1;
const BLOCK_LEVEL_STEP = 2;
class WalkthroughTextBlock {
constructor(html) {
this._html = html;
}
get html() {
return this._html;
}
static canConvert(block) {
return (
!WalkthroughVerificationBlock.canConvert(block) &&
!WalkthroughVerificationFailBlock.canConvert(block) &&
!WalkthroughVerificationSuccessBlock.canConvert(block)
);
}
static fromAdoc(adoc) {
return new WalkthroughTextBlock(adoc.convert());
}
}
class WalkthroughVerificationBlock {
constructor(html, successBlock, failBlock) {
this._html = html;
this._successBlock = successBlock;
this._failBlock = failBlock;
}
get html() {
return this._html;
}
get hasSuccessBlock() {
return !!this._successBlock;
}
get hasFailBlock() {
return !!this._failBlock;
}
get successBlock() {
return this._successBlock;
}
get failBlock() {
return this._failBlock;
}
static canConvert(block) {
return block.getAttribute(BLOCK_ATTR_TYPE) === BLOCK_TYPE_VERIFICATION;
}
static fromAdoc(adoc) {
return new WalkthroughVerificationBlock(adoc.convert());
}
}
class WalkthroughVerificationSuccessBlock {
constructor(html) {
this._html = html;
}
get html() {
return this._html;
}
static canConvert(block) {
return block.getAttribute(BLOCK_ATTR_TYPE) === BLOCK_TYPE_VERIFICATION_SUCCESS;
}
static fromAdoc(adoc) {
return new WalkthroughVerificationSuccessBlock(adoc.convert());
}
static findNextForVerification(blocks) {
// eslint-disable-next-line no-unused-vars
for (const block of blocks) {
if (WalkthroughVerificationBlock.canConvert(block)) {
return null;
}
if (WalkthroughVerificationSuccessBlock.canConvert(block)) {
return WalkthroughVerificationSuccessBlock.fromAdoc(block);
}
}
return null;
}
}
class WalkthroughVerificationFailBlock {
constructor(html) {
this._html = html;
}
get html() {
return this._html;
}
static canConvert(block) {
return block.getAttribute(BLOCK_ATTR_TYPE) === BLOCK_TYPE_VERIFICATION_FAIL;
}
static fromAdoc(adoc) {
return new WalkthroughVerificationFailBlock(adoc.convert());
}
static findNextForVerification(blocks) {
// eslint-disable-next-line no-unused-vars
for (const block of blocks) {
if (WalkthroughVerificationBlock.canConvert(block)) {
return null;
}
if (WalkthroughVerificationFailBlock.canConvert(block)) {
return WalkthroughVerificationFailBlock.fromAdoc(block);
}
}
return null;
}
}
class WalkthroughStep {
constructor(title, blocks) {
this._title = title;
this._blocks = blocks;
}
get title() {
return this._title;
}
get blocks() {
return this._blocks;
}
static canConvert(adoc) {
return adoc.context === CONTEXT_SECTION && adoc.level === BLOCK_LEVEL_STEP;
}
static fromAdoc(adoc) {
const title = adoc.numbered ? `${getNumberedTitle(adoc)}. ${adoc.title}` : adoc.title;
const blocks = adoc.blocks.reduce((acc, b, i, blockList) => {
if (WalkthroughVerificationBlock.canConvert(b)) {
const remainingBlocks = blockList.slice(i + 1, blockList.length);
const successBlock = WalkthroughVerificationSuccessBlock.findNextForVerification(remainingBlocks);
const failBlock = WalkthroughVerificationFailBlock.findNextForVerification(remainingBlocks);
acc.push(new WalkthroughVerificationBlock(b.convert(), successBlock, failBlock));
}
if (WalkthroughResourceStep.canConvert(b)) {
return acc;
}
if (WalkthroughTextBlock.canConvert(b)) {
acc.push(new WalkthroughTextBlock(b.convert()));
}
return acc;
}, []);
return new WalkthroughStep(title, blocks);
}
}
class WalkthroughResourceStep {
constructor(html, service, title) {
this._html = html;
this._serviceName = service;
this._title = title;
}
get title() {
return this._title;
}
get html() {
return this._html;
}
get serviceName() {
return this._serviceName;
}
static canConvert(adoc) {
return (
adoc.context === CONTEXT_SIDEBAR &&
(adoc.level === BLOCK_LEVEL_TASK || adoc.level === BLOCK_LEVEL_STEP) &&
adoc.getAttribute(BLOCK_ATTR_TYPE) === BLOCK_TYPE_TASK_RESOURCE
);
}
static fromAdoc(adoc) {
const service = adoc.getAttribute('serviceName');
const html = adoc.blocks[0] ? adoc.blocks[0].convert() : '';
return new WalkthroughResourceStep(html, service, adoc.title);
}
}
class WalkthroughResource {
constructor(html, service, title) {
this._html = html;
this._serviceName = service;
this._title = title;
}
get title() {
return this._title;
}
get html() {
return this._html;
}
get serviceName() {
return this._serviceName;
}
static canConvert(adoc) {
return (
adoc.context === CONTEXT_SIDEBAR &&
adoc.level === BLOCK_LEVEL_PREAMBLE &&
adoc.getAttribute(BLOCK_ATTR_TYPE) === BLOCK_TYPE_WALKTHROUGH_RESOURCE
);
}
static fromAdoc(adoc) {
const service = adoc.getAttribute('serviceName');
const html = adoc.blocks[0] ? adoc.blocks[0].convert() : '';
return new WalkthroughResource(html, service, adoc.title);
}
}
class WalkthroughTask {
constructor(title, time, html, steps, resources) {
this._title = title;
this._time = time;
this._html = html;
this._steps = steps;
this._resources = resources;
}
get title() {
return this._title;
}
get time() {
return this._time;
}
get html() {
return this._html;
}
get steps() {
return this._steps.filter(s => !(s instanceof WalkthroughResourceStep));
}
get blocks() {
return this._steps;
}
get resources() {
return this._resources;
}
static canConvert(adoc) {
return adoc.context === CONTEXT_SECTION && adoc.level === BLOCK_LEVEL_TASK;
}
// Task resources can be defined at task or step level so we have to recursively check all blocks
// that are lower in the document hierarchy
static collectTaskResources(task, collected) {
task.blocks.forEach(block => {
if (WalkthroughResourceStep.canConvert(block)) {
collected.push(WalkthroughResourceStep.fromAdoc(block));
} else if (block.blocks.length > 0) {
this.collectTaskResources(block, collected);
}
});
}
static fromAdoc(adoc) {
const title = adoc.numbered ? `${getNumberedTitle(adoc)}. ${adoc.title}` : adoc.title;
const time = parseInt(adoc.getAttribute(BLOCK_ATTR_TIME), 10) || 0;
const collectedResources = [];
this.collectTaskResources(adoc, collectedResources);
const steps = adoc.blocks.reduce((acc, b, i, blockList) => {
if (WalkthroughResourceStep.canConvert(b)) {
return acc;
}
if (WalkthroughStep.canConvert(b)) {
acc.push(WalkthroughStep.fromAdoc(b));
} else if (WalkthroughVerificationBlock.canConvert(b)) {
const remainingBlocks = blockList.slice(i + 1, blockList.length);
const successBlock = WalkthroughVerificationSuccessBlock.findNextForVerification(remainingBlocks);
const failBlock = WalkthroughVerificationFailBlock.findNextForVerification(remainingBlocks);
acc.push(new WalkthroughVerificationBlock(b.convert(), successBlock, failBlock));
} else if (WalkthroughTextBlock.canConvert(b)) {
acc.push(WalkthroughTextBlock.fromAdoc(b));
}
return acc;
}, []);
return new WalkthroughTask(title, time, adoc.convert(), steps, collectedResources);
}
}
class Walkthrough {
constructor(title, preamble, time, tasks, resources) {
this._title = title;
this._preamble = preamble;
this._time = time;
this._tasks = tasks;
this._resources = resources;
}
get title() {
return this._title;
}
get preamble() {
return this._preamble;
}
get time() {
return this._time;
}
get tasks() {
return this._tasks;
}
get resources() {
return this._resources;
}
// Walkthrough resources are always defined at preamble level
static collectWalkthroughResources(preamble) {
const resources = [];
preamble.blocks = preamble.blocks.reduce((acc, block) => {
if (WalkthroughResource.canConvert(block)) {
resources.push(WalkthroughResource.fromAdoc(block));
} else {
acc.push(block);
}
return acc;
}, []);
return resources;
}
static fromAdoc(adoc) {
const title = adoc.getDocumentTitle();
if (adoc.blocks.length < 1) {
throw new Error(`Invalid Walkthrough ${title}`);
}
const resources = this.collectWalkthroughResources(adoc.blocks[0]);
const preamble = adoc.blocks[0].convert();
const tasks = adoc.blocks.filter(b => WalkthroughTask.canConvert(b)).map(b => WalkthroughTask.fromAdoc(b));
const time = tasks.reduce((acc, t) => acc + t._time || 0, 0);
return new Walkthrough(title, preamble, time, tasks, resources);
}
}
const getNumberedTitle = block => {
if (block.context === CONTEXT_DOCUMENT || block.parent.context === CONTEXT_DOCUMENT) {
return `${block.numbered ? block.number : null}`;
}
return `${getNumberedTitle(block.parent)}.${block.numbered ? block.number : null}`;
};
const parseWalkthroughAdoc = (rawAdoc, attrs) => {
const parsedAdoc = parseAdoc(rawAdoc, attrs);
return Walkthrough.fromAdoc(parsedAdoc);
};
const parseAdoc = (rawAdoc, attrs) => asciidoctor().load(rawAdoc, { attributes: attrs });
export {
WalkthroughTextBlock,
WalkthroughVerificationBlock,
WalkthroughVerificationFailBlock,
WalkthroughVerificationSuccessBlock,
WalkthroughStep,
WalkthroughTask,
Walkthrough,
parseWalkthroughAdoc
};