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