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