@longform/async 0.0.29

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/lib/mod.ts ADDED
@@ -0,0 +1,3 @@
1
+
2
+ export * from './types.ts';
3
+ export { longform, processTemplate } from './longform.ts';
package/lib/types.ts ADDED
@@ -0,0 +1,671 @@
1
+
2
+ /**
3
+ * @internal
4
+ */
5
+ export type SanitizerElement = {
6
+ tag: string;
7
+ attrs: string[];
8
+ };
9
+
10
+ /**
11
+ * @internal
12
+ */
13
+ export type SanitizerConfig = {
14
+ allowAll: boolean;
15
+ allowedAttributes: string[];
16
+ allowedElements: Record<string, SanitizerElement>;
17
+ };
18
+
19
+ /**
20
+ * @internal
21
+ */
22
+ export type WorkingElement = {
23
+ indent: number;
24
+ key?: string;
25
+ id?: string;
26
+ tag: string;
27
+ class?: string;
28
+ attrs: Record<string, string | true>;
29
+ text?: string;
30
+ html: string;
31
+ mount?: string;
32
+ sanitizerConfig?: SanitizerConfig;
33
+ chain: WorkingElement[];
34
+ beforeRender?: AppliedDirective[];
35
+ };
36
+
37
+ /**
38
+ * @internal
39
+ */
40
+ export type WorkingFragmentType =
41
+ | 'root'
42
+ | 'embed'
43
+ | 'bare'
44
+ | 'range'
45
+ | 'text'
46
+ | 'mount'
47
+ | 'template'
48
+ ;
49
+
50
+ /**
51
+ * @internal
52
+ */
53
+ export type FragmentRef = {
54
+ id: string;
55
+ start: number;
56
+ end: number;
57
+ };
58
+
59
+ /**
60
+ * @internal
61
+ */
62
+ export type WorkingFragment = {
63
+ id: string;
64
+ template: boolean;
65
+ mountable: boolean;
66
+ type: WorkingFragmentType;
67
+ html: string;
68
+ refs: FragmentRef[];
69
+ els: WorkingElement[];
70
+ mountPoints?: MountPoint[];
71
+ };
72
+
73
+ /**
74
+ * The fragment type.
75
+ *
76
+ * `embed` indicates the fragment has a unique embedded HTML id.
77
+ * `bare` indicates the fragment has no embedded HTML id.
78
+ * `range` indicates the fragment is a range of elements, instead of having a single top level element.
79
+ * `text` indicates the fragment is text only can can be used in situations where HTML might be invalid.
80
+ */
81
+ export type FragmentType =
82
+ | 'embed'
83
+ | 'bare'
84
+ | 'range'
85
+ | 'text'
86
+ ;
87
+
88
+ /**
89
+ * A fragment object.
90
+ */
91
+ export type Fragment = {
92
+ /**
93
+ * The Longform identifier for the element.
94
+ */
95
+ id: string;
96
+
97
+ /**
98
+ * A selector which can be used to extract the fragment from a pages DOM
99
+ * if the fragment had been server rendered.
100
+ */
101
+ selector: string;
102
+
103
+ /**
104
+ * The fragment type.
105
+ *
106
+ * `embed` indicates the fragment has a unique embedded HTML id.
107
+ * `bare` indicates the fragment has no embedded HTML id.
108
+ * `range` indicates the fragment is a range of elements, instead of having a single top level element.
109
+ * `text` indicates the fragment is text only can can be used in situations where HTML might be invalid.
110
+ */
111
+ type: FragmentType;
112
+
113
+ /**
114
+ * The fragment output as a string.
115
+ */
116
+ html: string;
117
+ };
118
+
119
+ /**
120
+ * If the parser's output mode is `mountable`, the Longform parser will
121
+ * break output of the root element up in locations where the `@mount:: mountPointId`
122
+ * directive is used.
123
+ */
124
+ export type MountPoint = {
125
+ /**
126
+ * The id of the mountpoint.
127
+ */
128
+ id: string;
129
+
130
+ /**
131
+ * The HTML output for this part.
132
+ */
133
+ part: string;
134
+ };
135
+
136
+ /**
137
+ * A doc object used to provide directives with meta information
138
+ * about the doc while it is being parsed.
139
+ */
140
+ export type Doc = {
141
+ /**
142
+ * The `@id` value of the document.
143
+ */
144
+ id?: string;
145
+
146
+ /**
147
+ * The `@type` value of the document.
148
+ */
149
+ type?: string;
150
+
151
+ /**
152
+ * The `@lang` value of the document.
153
+ */
154
+ lang?: string;
155
+
156
+ /**
157
+ * The `@dir` value of the document.
158
+ */
159
+ dir?: string;
160
+
161
+ /**
162
+ * An object containing all meta values defined for the document.
163
+ * Note that they will not have the directive's `@` character
164
+ * prefixing them. So the directive `@foo:bar` will have the
165
+ * key of `foo:bar` in the meta object.
166
+ */
167
+ meta: Readonly<Record<string, unknown>>;
168
+ };
169
+
170
+ /**
171
+ * An element's description. Directives with access to elements
172
+ * should only apply changes to its `attrs` object. The `id` can
173
+ * be set via the `attrs` object if it does not already have a
174
+ * value, and classes can be appended to the `class` value via
175
+ * the `attrs` object.
176
+ *
177
+ * The element's tag cannot be changed.
178
+ */
179
+ export type Element = {
180
+
181
+ /**
182
+ * The element's tag.
183
+ */
184
+ tag: string;
185
+
186
+ /**
187
+ * The element's id.
188
+ */
189
+ id?: string;
190
+
191
+ /**
192
+ * The element's class value.
193
+ */
194
+ class?: string;
195
+
196
+ /**
197
+ * An object containing the element's attributes.
198
+ */
199
+ attrs: Record<string, boolean | string>;
200
+ };
201
+
202
+ /**
203
+ * Context supplied to directives declared in the global directive.
204
+ */
205
+ export type GlobalCtx = {
206
+
207
+ /**
208
+ * A doc object used to provide directives with meta information
209
+ * about the doc while it is being parsed.
210
+ */
211
+ doc: Doc;
212
+ };
213
+
214
+ /**
215
+ * Context supplied to meta directives.
216
+ */
217
+ export type MetaCtx = {
218
+
219
+ /**
220
+ * The directive's inline args.
221
+ */
222
+ inlineArgs?: string;
223
+
224
+ /**
225
+ * The directive's block args.
226
+ */
227
+ blockArgs?: string;
228
+ };
229
+
230
+ /**
231
+ * Context supplied to element directives.
232
+ */
233
+ export type ElementCtx = {
234
+
235
+ /**
236
+ * The directive's inline args.
237
+ */
238
+ inlineArgs?: string;
239
+
240
+ /**
241
+ * The directive's block args.
242
+ */
243
+ blockArgs?: string;
244
+
245
+ /**
246
+ * A doc object used to provide directives with meta information
247
+ * about the doc while it is being parsed.
248
+ */
249
+ doc: Readonly<Doc>;
250
+
251
+ /**
252
+ * The element which is the target of the directive.
253
+ */
254
+ el: Element;
255
+
256
+ /**
257
+ * The elements that are chained to the target element.
258
+ */
259
+ chain: Element[];
260
+ };
261
+
262
+ /**
263
+ * Context supplied to attr directives
264
+ */
265
+ export type AttrCtx = {
266
+ /**
267
+ * The element's tag.
268
+ */
269
+ tag: string;
270
+
271
+ /**
272
+ * A doc object used to provide directives with meta information
273
+ * about the doc while it is being parsed.
274
+ */
275
+ doc: Readonly<Doc>;
276
+ };
277
+
278
+ /**
279
+ * Context supplied to render directives.
280
+ */
281
+ export type RenderCtx = {
282
+ /**
283
+ * The directive's inline args.
284
+ */
285
+ inlineArgs?: string;
286
+
287
+ /**
288
+ * The directive's block args.
289
+ */
290
+ blockArgs?: string;
291
+
292
+ /**
293
+ * A doc object used to provide directives with meta information
294
+ * about the doc while it is being parsed.
295
+ */
296
+ doc: Readonly<Doc>;
297
+ };
298
+
299
+ /**
300
+ * Context supplied to async directives.
301
+ */
302
+ export type AsyncRenderCtx = {
303
+ /**
304
+ * The directive's inline args.
305
+ */
306
+ inlineArgs?: string;
307
+
308
+ /**
309
+ * The directive's block args.
310
+ */
311
+ blockArgs?: string;
312
+
313
+ /**
314
+ * A doc object used to provide directives with meta information
315
+ * about the doc while it is being parsed.
316
+ */
317
+ doc: Readonly<Doc>;
318
+ };
319
+
320
+
321
+ /**
322
+ * Directive definition object. Directives can support multiple roles
323
+ * but some crate ambiguities and should not be used together. The
324
+ * parser will ignore the directive if it implements hooks for conflicting
325
+ * roles.
326
+ *
327
+ * A directive implementing `meta` cannot implement `element`.
328
+ * A directive implementing `element` cannot implement `render` or `asyncRender`.
329
+ * A directive implementing `render` cannot implement `asyncRender`.
330
+ */
331
+ export type Directive = {
332
+
333
+ /**
334
+ * The meta directive hook defines a meta value. Meta values can be accessed
335
+ * from the `doc.meta` object in other directive hooks and from
336
+ * the parsed result of a Longform document via the exported `parsed.meta`
337
+ * object.
338
+ *
339
+ * Meta directives should be used at the start of a longform document.
340
+ * They will be ignored by the longform parser if defined after the
341
+ * `@global` directive, directives implementing the element hook, or
342
+ * fragment definitions.
343
+ */
344
+ meta?: (args: MetaCtx) => unknown;
345
+
346
+ /**
347
+ * The global hook is not implemented.
348
+ *
349
+ * @internal.
350
+ */
351
+ global?: (ctx: GlobalCtx) => void;
352
+
353
+ /**
354
+ * The attr directive hook can be used to set a element's attribute.
355
+ * It is triggered before element hooks defined for the same element
356
+ * or element chain.
357
+ */
358
+ attr?: (ctx: AttrCtx) => string | boolean | undefined | null;
359
+
360
+ /**
361
+ * The element hook can be used to set attribute and data values
362
+ * on an element with more context about the element's definition.
363
+ */
364
+ element?: (ctx: ElementCtx) => void;
365
+
366
+ /**
367
+ * The render hook can output custom HTML at the location it is
368
+ * called.
369
+ */
370
+ render?: (ctx: RenderCtx) => string | undefined;
371
+
372
+ /**
373
+ * The async render hook can output custom HTML after an asynchronous
374
+ * operation at the location it is called.
375
+ */
376
+ asyncRender?: (ctx: AsyncRenderCtx) => Promise<string | undefined>;
377
+ };
378
+
379
+ /**
380
+ * @internal
381
+ */
382
+ export type AppliedDirective = {
383
+ inlineArgs?: string;
384
+ blockArgs?: string;
385
+ element?: (ctx: ElementCtx) => void;
386
+ };
387
+
388
+ /**
389
+ * The annotation method sets how data attributes can be set on
390
+ * elements outputted by the Longform parser. This is used by clients
391
+ * when searching the DOM for server rendered fragments, or in
392
+ * editing situations.
393
+ *
394
+ * `predictable` should only be used in situations where untrusted
395
+ * user data is not being added to the response. All data attributes
396
+ * would be prefixed with the `data-lf-` prefix, unless a `key` is
397
+ * passed in via the parser args.
398
+ *
399
+ * The predictable mode does not annotate fragments using data
400
+ * attributes if they have a Longform identifier as their HTML id.
401
+ *
402
+ * `obscure` creates a key that is hard to guess in advance when
403
+ * the document is created on the server. This should work in
404
+ * situations where HTTP caching is involved if the full document
405
+ * is created in one pass before being cached, as any untrusted
406
+ * data would also be baked into the response at the time of the
407
+ * obscure key's generation.
408
+ *
409
+ * This approach to obscuring potential data-attribute conflicts is
410
+ * not fool proof. Do not use it in situations where part of a
411
+ * Longform document would be parsed and cached before passing into
412
+ * another document which includes untrusted user data. The first
413
+ * pass might cache the obscure key and allow users to mimic the
414
+ * response and define blocks of HTML that are picked by the client's
415
+ * DOM processing logic.
416
+ *
417
+ * @see [Longform's security considerations](https://longform.occultist.dev/#security-considerations)
418
+ */
419
+ export type Annotation =
420
+ | 'none'
421
+ | 'predictable'
422
+ | 'obscure'
423
+ ;
424
+
425
+ /**
426
+ * The Longform parser supports several output modes which are
427
+ * useful for different situations.
428
+ *
429
+ * `doc` Outputs all fragments normally.
430
+ *
431
+ * `head` Parses the `@id`, `@type`, @lang`, `@dir` and any directives
432
+ * implementing the `meta` directive hook. It exits as soon as it hits
433
+ * another directive type or fragment declaration.
434
+ *
435
+ * `mountable` Supports use of the `@mount` directive for turning elements
436
+ * into mountpoints. This output mode populates the `mountPoints` array
437
+ * in the parse result. This array can be looped over and data from another
438
+ * rendering tool can mount its output into the named mount points. Once all
439
+ * mountpoint entries have been populated the `tail` property in the parsed
440
+ * result should be appended to the final document.
441
+ *
442
+ * The mountable mode is currently very basic in implementation and only
443
+ * supports documents implementing a single root element.
444
+ *
445
+ * `templates` parses every fragment as though it was a template even
446
+ * if the `@template` directive is not used. This is useful for creating
447
+ * directives which use the block args to define a Longform template.
448
+ */
449
+ export type OutputMode =
450
+ | 'doc'
451
+ | 'head'
452
+ | 'mountable'
453
+ | 'templates'
454
+ ;
455
+
456
+ /**
457
+ * The content type of the response can be passed into the Longform
458
+ * parser to change the shape of the output. The default value `text/html`
459
+ * will use HTML's method for closing void tags. The XML versions would
460
+ * use XML valid syntax.
461
+ *
462
+ * If `application/xml` is provided, no data attributes will be added to
463
+ * the outputted markup, even if an annotation mode or key is given.
464
+ */
465
+ export type TargetType =
466
+ | 'text/html'
467
+ | 'application/xhtml+xml'
468
+ | 'application/xml'
469
+ ;
470
+
471
+ /**
472
+ * The Longform parser is designed to serve multiple roles.
473
+ *
474
+ *
475
+ * In a situation where a document containing a HTML page's main content
476
+ * is split from a re-useable layout the `'doc'` output mode can be used
477
+ * on the main content and the result passed as arguments to the Longform
478
+ * parser when parsing the layout document. The id, type, lang and direction
479
+ * as well as any custom meta values and the parsed fragments from the
480
+ * main document will be available to the template's fragments.
481
+ *
482
+ *
483
+ * In a situation where a directive is being defined, the output mode
484
+ * `head` can be used to use the Longform parser as a DSL for custom
485
+ * directives. The `templates` output mode can be used to parse the
486
+ * block arguments of the directive as Longform templates, with the
487
+ * directive supplying arguments specified by the directive.
488
+ *
489
+ *
490
+ * The `mountable` output mode is used where Longform needs to be outputted
491
+ * so another libraries SSR rendered content can fill the gaps in mountable
492
+ * elements. Usually this would be used for a layout document which is
493
+ * used in a similar way to Astro's islands.
494
+ */
495
+ export type LongformArgs = {
496
+
497
+ /**
498
+ * The key used for creating data attributes.
499
+ * Defaults to a random string or 'lf' if predictable mode is enabled.
500
+ */
501
+ key?: string;
502
+
503
+ /**
504
+ * The id of the document. If set the `@id` is ignored.
505
+ * This can be used when creating partial re-usable documents.
506
+ */
507
+ id?: string;
508
+
509
+ /**
510
+ * The media type of the document. If set the `@type` directive within
511
+ * the input document is ignored.
512
+ */
513
+ type?: string;
514
+
515
+ /**
516
+ * The language tag of the document. If set the `@lang`
517
+ * directive is ignored.
518
+ * This can be used when creating partial re-usable documents.
519
+ */
520
+ lang?: string;
521
+
522
+ /**
523
+ * The text direction of the document. If set the `@dir`
524
+ * directive is ignored.
525
+ * This can be used when creating partial re-usable documents.
526
+ */
527
+ dir?: string;
528
+
529
+ /**
530
+ * Metadata set on the document.
531
+ */
532
+ meta?: Record<string, unknown>;
533
+
534
+ /**
535
+ * The media type of the target document. This is used to control
536
+ * the output of the serialized markup. If `'text/html'` is given
537
+ * void elements will be formatted in the HTML style and attributes
538
+ * will not use quotes unless used in the input markup.
539
+ *
540
+ * XML target types will always have attributes wrapped in double
541
+ * quotes.
542
+ */
543
+ targetType?: TargetType;
544
+
545
+ /**
546
+ * The output mode of the document.
547
+ */
548
+ outputMode?: OutputMode;
549
+
550
+ /**
551
+ * Style in which data attributes are added to output HTML markup.
552
+ *
553
+ * This is used in SSR situations where a client will need to use the
554
+ * data attributes that annotate Longform rendered fragments and elements
555
+ * to rebuild the Longform document from client produced DOM.
556
+ */
557
+ annotations?: Annotation;
558
+
559
+ /**
560
+ * Configures the sanitizer to allow all elements and attributes in the document.
561
+ */
562
+ //allowAll?: boolean;
563
+
564
+ /**
565
+ * Configures the sanitizer to allow the given set of attributes unless elements specify more.
566
+ */
567
+ //allowedAttributes?: string[];
568
+
569
+ /**
570
+ * Configures the sanitizer to allow the given set of elements and their attributes.
571
+ */
572
+ //allowedElements?: Array<string | SanitizerElement>;
573
+
574
+ /**
575
+ * Object of custom directives.
576
+ */
577
+ directives?: Record<string, Directive>;
578
+
579
+ /**
580
+ * Object of fragments to make available to the parser when processing fragment refs.
581
+ */
582
+ fragments?: Record<string, Fragment>;
583
+ };
584
+
585
+ /**
586
+ * An object containing the parsed Longform result.
587
+ */
588
+ export type ParsedLongform = {
589
+
590
+ /**
591
+ * The key used when annotating the markup with data attributes.
592
+ */
593
+ key?: string;
594
+
595
+ /**
596
+ * The id, or URL of the document.
597
+ */
598
+ id?: string;
599
+
600
+ /**
601
+ * The media type of the Longform document.
602
+ */
603
+ type?: string;
604
+
605
+ /**
606
+ * The language tag of the document.
607
+ */
608
+ lang?: string;
609
+
610
+ /**
611
+ * The text direction of the document.
612
+ */
613
+ dir?: string;
614
+
615
+ /**
616
+ * Meta values defined in the document's head.
617
+ */
618
+ meta: Record<string, unknown>;
619
+
620
+ /**
621
+ * The media type of the target document.
622
+ */
623
+ targetType?: TargetType;
624
+
625
+ /**
626
+ * The output mode used when parsing the document.
627
+ */
628
+ outputMode: OutputMode;
629
+
630
+ /**
631
+ * Style in which data attributes are added to output HTML markup.
632
+ */
633
+ annotations?: Annotation;
634
+
635
+ /**
636
+ * The root HTML output for the document.
637
+ */
638
+ root?: string;
639
+
640
+ /**
641
+ * The root block as a template.
642
+ * This is only produced if using the output mode of `'templates'`.
643
+ */
644
+ template?: string;
645
+
646
+ /**
647
+ * The root selector.
648
+ */
649
+ selector?: string;
650
+
651
+ /**
652
+ * The HTML fragments.
653
+ */
654
+ fragments: Record<string, Fragment>;
655
+
656
+ /**
657
+ * All parsed templates.
658
+ */
659
+ templates: Record<string, string>;
660
+
661
+ /**
662
+ * Document mount points.
663
+ */
664
+ mountpoints?: MountPoint[];
665
+
666
+ /**
667
+ * Final document part after mount points.
668
+ */
669
+ tail?: string;
670
+ };
671
+