index.d.ts
12.1 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Type definitions for Mustache 4.1.0
// Project: https://github.com/janl/mustache.js
// Definitions by: Mark Ashley Bell <https://github.com/markashleybell>,
// Manuel Thalmann <https://github.com/manuth>,
// Phillip Johnsen <https://github.com/phillipj>
// Steve Dignam <https://github.com/sbdchd>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 3.7
/**
* Provides the functionality to render templates with `{{mustaches}}`.
*/
interface MustacheStatic {
/**
* The name of the module.
*/
readonly name: string;
/**
* The version of the module.
*/
readonly version: string;
/**
* The default opening and closing tags used while parsing the templates.
*
* Different default tags can be overridden by setting this field. They will have effect on all subsequent
* calls to `.render()` or `.parse()`, unless custom tags are given as arguments to those functions.
*
* Default value is `[ "{{", "}}" ]`.
*/
tags: OpeningAndClosingTags;
/**
* A simple string scanner that is used by the template parser to find tokens in template strings.
*/
Scanner: typeof MustacheScanner;
/**
* Represents a rendering context by wrapping a view object and maintaining a reference to the parent context.
*/
Context: typeof MustacheContext;
/**
* A Writer knows how to take a stream of tokens and render them to a `string`, given a context.
*
* It also maintains a cache of templates to avoid the need to parse the same template twice.
*/
Writer: typeof MustacheWriter;
/**
* HTML escaping by default, can be overriden by setting Mustache.escape explicitly or providing the `options`
* argument with an `escape` function when invoking Mustache.render().
*
* Escaping can be avoided when needed by using `{{{ value }}}` or `{{& value }}` in templates.
*
* @param value
* The value to escape into a string.
*/
escape: EscapeFunction;
/**
* Clears all cached templates in this writer.
*/
clearCache(): void;
/**
* Customise the template caching behaviour by either:
*
* disable it completely by setting it to `undefined`
*
* -- or --
*
* provide a custom cache strategy that satisfies the `TemplateCache` interface
*/
templateCache: TemplateCache | undefined;
/**
* Parses and caches the given template in the default writer and returns the array of tokens it contains.
*
* Doing this ahead of time avoids the need to parse templates on the fly as they are rendered.
*
* @param template
* The template to parse.
*
* @param tags
* The tags to use.
*/
parse(template: string, tags?: OpeningAndClosingTags): TemplateSpans;
/**
* Renders the `template` with the given `view` and `partials` using the default writer.
*
* @param template
* The template to render.
*
* @param view
* The view to render the template with.
*
* @param partials
* Either an object that contains the names and templates of partials that are used in a template
*
* -- or --
*
* A function that is used to load partial template on the fly that takes a single argument: the name of the partial.
*
* @param tagsOrOptions
* The delimeter tags to use or options overriding global defaults.
*/
render(
template: string,
view: any | MustacheContext,
partials?: PartialsOrLookupFn,
tagsOrOptions?: OpeningAndClosingTags | RenderOptions,
): string;
}
/**
* A simple string scanner that is used by the template parser to find tokens in template strings.
*/
declare class MustacheScanner {
string: string;
tail: string;
pos: number;
/**
* Initializes a new instance of the `MustacheScanner` class.
*/
constructor(string: string);
/**
* Returns `true` if the tail is empty (end of string).
*/
eos(): boolean;
/**
* Tries to match the given regular expression at the current position.
*
* @param re
* The regex-pattern to match.
*
* @returns
* The matched text if it can match, the empty string otherwise.
*/
scan(re: RegExp): string;
/**
* Skips all text until the given regular expression can be matched.
*
* @param re
* The regex-pattern to match.
*
* @returns
* Returns the skipped string, which is the entire tail if no match can be made.
*/
scanUntil(re: RegExp): string;
}
/**
* Represents a rendering context by wrapping a view object and maintaining a reference to the parent context.
*/
declare class MustacheContext {
view: any;
parent: MustacheContext | undefined;
/**
* Initializes a new instance of the `MustacheContext` class.
*/
constructor(view: any, parentContext?: MustacheContext);
/**
* Creates a new context using the given view with this context as the parent.
*
* @param view
* The view to create the new context with.
*/
push(view: any): MustacheContext;
/**
* Returns the value of the given name in this context, traversing up the context hierarchy if the value is absent in this context's view.
*
* @param name
* The name to look up.
*/
lookup(name: string): any;
}
/**
* A Writer knows how to take a stream of tokens and render them to a `string`, given a context.
*
* It also maintains a cache of templates to avoid the need to parse the same template twice.
*/
declare class MustacheWriter {
/**
* Initializes a new instance of the `MustacheWriter` class.
*/
constructor();
/**
* Clears all cached templates in this writer.
*/
clearCache(): void;
/**
* Parses and caches the given `template` and returns the array of tokens that is generated from the parse.
*
* @param template
* The template to parse.
*
* @param tags
* The tags to use.
*/
parse(template: string, tags?: OpeningAndClosingTags): any;
/**
* High-level method that is used to render the given `template` with the given `view`.
*
* @param template
* The template to render.
*
* @param view
* The view to render the template with.
*
* @param partials
* Either an object that contains the names and templates of partials that are used in a template
*
* -- or --
*
* A function that is used to load partial template on the fly that takes a single argument: the name of the partial.
*
* @param tags
* The tags to use.
*/
render(
template: string,
view: any | MustacheContext,
partials?: PartialsOrLookupFn,
config?: OpeningAndClosingTags | RenderOptions,
): string;
/**
* Low-level method that renders the given array of `tokens` using the given `context` and `partials`.
*
* @param tokens
* The tokens to render.
*
* @param context
* The context to use for rendering the tokens.
*
* @param partials
* The partials to use for rendering the tokens.
*
* @param originalTemplate
* An object used to extract the portion of the original template that was contained in a higher-order section.
*
* If the template doesn't use higher-order sections, this argument may be omitted.
*/
renderTokens(
tokens: string[][],
context: MustacheContext,
partials?: PartialsOrLookupFn,
originalTemplate?: string,
config?: RenderOptions,
): string;
/**
* Renders a section block.
*
* @param token
* The token to render.
*
* @param context
* The context to use for rendering the token.
*
* @param partials
* The partials to use for rendering the token.
*
* @param originalTemplate
* An object used to extract the portion of the original template that was contained in a higher-order section.
*/
renderSection(
token: string[],
context: MustacheContext,
partials?: PartialsOrLookupFn,
originalTemplate?: string,
config?: RenderOptions,
): string;
/**
* Renders an inverted section block.
*
* @param token
* The token to render.
*
* @param context
* The context to use for rendering the token.
*
* @param partials
* The partials to use for rendering the token.
*
* @param originalTemplate
* An object used to extract the portion of the original template that was contained in a higher-order section.
*/
renderInverted(
token: string[],
context: MustacheContext,
partials?: PartialsOrLookupFn,
originalTemplate?: string,
config?: RenderOptions,
): string;
/**
* Adds indentation to each line of the given partial.
*
* @param partial
* The partial to indent.
*
* @param indentation
* String containing a combination of spaces and tabs to use as indentation.
*
* @param lineHasNonSpace
* Whether to indent lines that are empty.
*/
indentPartial(partial: string, indentation: string, lineHasNonSpace: boolean): string;
/**
* Renders a partial.
*
* @param token
* The token to render.
*
* @param context
* The context to use for rendering the token.
*
* @param partials
* The partials to use for rendering the token.
*
* @param tags
* The tags to use.
*/
renderPartial(
token: string[],
context: MustacheContext,
partials?: PartialsOrLookupFn,
config?: OpeningAndClosingTags | RenderOptions,
): string;
/**
* Renders an unescaped value.
*
* @param token
* The token to render.
*
* @param context
* The context to use for rendering the token.
*/
unescapedValue(token: string[], context: MustacheContext): string;
/**
* Renders an escaped value.
*
* @param token
* The token to render.
*
* @param context
* The context to use for rendering the token.
*/
escapedValue(
token: string[],
context: MustacheContext,
config?: RenderOptions,
): string;
/**
* Renders a raw token.
*
* @param token
* The token to render.
*/
rawValue(token: string[]): string;
}
type RAW_VALUE = 'text';
type ESCAPED_VALUE = 'name';
type UNESCAPED_VALUE = '&';
type SECTION = '#';
type INVERTED = '^';
type COMMENT = '!';
type PARTIAL = '>';
type EQUAL = '=';
type TemplateSpanType = RAW_VALUE | ESCAPED_VALUE | SECTION | UNESCAPED_VALUE | INVERTED | COMMENT | PARTIAL | EQUAL;
type TemplateSpans = Array<
| [TemplateSpanType, string, number, number]
| [TemplateSpanType, string, number, number, TemplateSpans, number]
| [TemplateSpanType, string, number, number, string, number, boolean]
>;
/**
* Function responsible for escaping values from the view into the rendered output when templates
* has `{{ value }}` in them.
*/
type EscapeFunction = (value: any) => string;
/**
* An array of two strings, representing the opening and closing tags respectively, to be used in the templates being rendered.
*/
type OpeningAndClosingTags = [string, string];
/**
* Whenever partials are provided, it can either be an object that contains the names and templates of partials that are used in tempaltes
*
* -- or --
*
* A function that is used to load partial template on the fly that takes a single argument: the name of the partial.
*/
type PartialsOrLookupFn = Record<string, string> | PartialLookupFn;
type PartialLookupFn = (partialName: string) => string | undefined;
interface RenderOptions {
escape?: EscapeFunction;
tags?: OpeningAndClosingTags;
}
interface TemplateCache {
set(cacheKey: string, value: string): void;
get(cacheKey: string): string | undefined;
clear(): void;
}
/**
* Provides the functionality to render templates with `{{mustaches}}`.
*/
declare var Mustache: MustacheStatic;
export = Mustache;
export as namespace Mustache;