UNPKG

12 kBTypeScriptView Raw
1// Type definitions for Mustache 4.2
2// Project: https://github.com/janl/mustache.js
3// Definitions by: Mark Ashley Bell <https://github.com/markashleybell>,
4// Manuel Thalmann <https://github.com/manuth>,
5// Phillip Johnsen <https://github.com/phillipj>
6// Steve Dignam <https://github.com/sbdchd>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8// Minimum TypeScript Version: 3.7
9
10/**
11 * The name of the module.
12 */
13export const name: string;
14
15/**
16 * The version of the module.
17 */
18export const version: string;
19
20/**
21 * The default opening and closing tags used while parsing the templates.
22 *
23 * Different default tags can be overridden by setting this field. They will have effect on all subsequent
24 * calls to {@link render `.render()`} or {@link parse `.parse()`}, unless custom tags are given as arguments to those functions.
25 *
26 * Default value is `[ "{{", "}}" ]`.
27 */
28export let tags: OpeningAndClosingTags;
29
30/**
31 * Customise the template caching behaviour by either:
32 *
33 * disable it completely by setting it to `undefined`
34 *
35 * -- or --
36 *
37 * provide a custom cache strategy that satisfies the {@link TemplateCache `TemplateCache`} interface
38 */
39export let templateCache: TemplateCache | undefined;
40
41/**
42 * A simple string scanner that is used by the template parser to find tokens in template strings.
43 */
44export class Scanner {
45 string: string;
46 tail: string;
47 pos: number;
48
49 /**
50 * Initializes a new instance of the {@link Scanner `Scanner`} class.
51 */
52 constructor(string: string);
53
54 /**
55 * Returns `true` if the tail is empty (end of string).
56 */
57 eos(): boolean;
58
59 /**
60 * Tries to match the given regular expression at the current position.
61 *
62 * @param re
63 * The regex-pattern to match.
64 *
65 * @returns
66 * The matched text if it can match, the empty string otherwise.
67 */
68 scan(re: RegExp): string;
69
70 /**
71 * Skips all text until the given regular expression can be matched.
72 *
73 * @param re
74 * The regex-pattern to match.
75 *
76 * @returns
77 * Returns the skipped string, which is the entire tail if no match can be made.
78 */
79 scanUntil(re: RegExp): string;
80}
81
82/**
83 * Represents a rendering context by wrapping a view object and maintaining a reference to the parent context.
84 */
85export class Context {
86 view: any;
87 parent: Context | undefined;
88
89 /**
90 * Initializes a new instance of the {@link Context `Context`} class.
91 */
92 constructor(view: any, parentContext?: Context);
93
94 /**
95 * Creates a new context using the given view with this context as the parent.
96 *
97 * @param view
98 * The view to create the new context with.
99 */
100 push(view: any): Context;
101
102 /**
103 * 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.
104 *
105 * @param name
106 * The name to look up.
107 */
108 lookup(name: string): any;
109}
110
111/**
112 * A Writer knows how to take a stream of tokens and render them to a {@link String `string`}, given a context.
113 *
114 * It also maintains a cache of templates to avoid the need to parse the same template twice.
115 */
116export class Writer {
117 /**
118 * Initializes a new instance of the {@link Writer `Writer`} class.
119 */
120 constructor();
121
122 /**
123 * Clears all cached templates in this writer.
124 */
125 clearCache(): void;
126
127 /**
128 * Parses and caches the given {@link template `template`} and returns the array of tokens that is generated from the parse.
129 *
130 * @param template
131 * The template to parse.
132 *
133 * @param tags
134 * The tags to use.
135 */
136 parse(template: string, tags?: OpeningAndClosingTags): any;
137
138 /**
139 * High-level method that is used to render the given {@link template `template`} with the given {@link view `view`}.
140 *
141 * @param template
142 * The template to render.
143 *
144 * @param view
145 * The view to render the template with.
146 *
147 * @param partials
148 * Either an object that contains the names and templates of partials that are used in a template
149 *
150 * -- or --
151 *
152 * A function that is used to load partial template on the fly that takes a single argument: the name of the partial.
153 *
154 * @param config
155 * The options for the rendering process.
156 */
157 render(
158 template: string,
159 view: any | Context,
160 partials?: PartialsOrLookupFn,
161 config?: OpeningAndClosingTags | RenderOptions,
162 ): string;
163
164 /**
165 * Low-level method that renders the given array of {@link tokens `tokens`} using the given {@link context `context`} and {@link partials `partials`}.
166 *
167 * @param tokens
168 * The tokens to render.
169 *
170 * @param context
171 * The context to use for rendering the tokens.
172 *
173 * @param partials
174 * The partials to use for rendering the tokens.
175 *
176 * @param originalTemplate
177 * An object used to extract the portion of the original template that was contained in a higher-order section.
178 *
179 * If the template doesn't use higher-order sections, this argument may be omitted.
180 *
181 * @param config
182 * The options for the rendering process.
183 */
184 renderTokens(
185 tokens: string[][],
186 context: Context,
187 partials?: PartialsOrLookupFn,
188 originalTemplate?: string,
189 config?: RenderOptions,
190 ): string;
191
192 /**
193 * Renders a section block.
194 *
195 * @param token
196 * The token to render.
197 *
198 * @param context
199 * The context to use for rendering the token.
200 *
201 * @param partials
202 * The partials to use for rendering the token.
203 *
204 * @param originalTemplate
205 * An object used to extract the portion of the original template that was contained in a higher-order section.
206 *
207 * @param config
208 * The options for the rendering process.
209 */
210 renderSection(
211 token: string[],
212 context: Context,
213 partials?: PartialsOrLookupFn,
214 originalTemplate?: string,
215 config?: RenderOptions,
216 ): string;
217
218 /**
219 * Renders an inverted section block.
220 *
221 * @param token
222 * The token to render.
223 *
224 * @param context
225 * The context to use for rendering the token.
226 *
227 * @param partials
228 * The partials to use for rendering the token.
229 *
230 * @param originalTemplate
231 * An object used to extract the portion of the original template that was contained in a higher-order section.
232 *
233 * @param config
234 * The options for the rendering process.
235 */
236 renderInverted(
237 token: string[],
238 context: Context,
239 partials?: PartialsOrLookupFn,
240 originalTemplate?: string,
241 config?: RenderOptions,
242 ): string;
243
244 /**
245 * Adds indentation to each line of the given partial.
246 *
247 * @param partial
248 * The partial to indent.
249 *
250 * @param indentation
251 * String containing a combination of spaces and tabs to use as indentation.
252 *
253 * @param lineHasNonSpace
254 * Whether to indent lines that are empty.
255 */
256 indentPartial(partial: string, indentation: string, lineHasNonSpace: boolean): string;
257
258 /**
259 * Renders a partial.
260 *
261 * @param token
262 * The token to render.
263 *
264 * @param context
265 * The context to use for rendering the token.
266 *
267 * @param partials
268 * The partials to use for rendering the token.
269 *
270 * @param config
271 * The options for the rendering process.
272 */
273 renderPartial(
274 token: string[],
275 context: Context,
276 partials?: PartialsOrLookupFn,
277 config?: OpeningAndClosingTags | RenderOptions,
278 ): string;
279
280 /**
281 * Renders an unescaped value.
282 *
283 * @param token
284 * The token to render.
285 *
286 * @param context
287 * The context to use for rendering the token.
288 */
289 unescapedValue(token: string[], context: Context): string;
290
291 /**
292 * Renders an escaped value.
293 *
294 * @param token
295 * The token to render.
296 *
297 * @param context
298 * The context to use for rendering the token.
299 *
300 * @param config
301 * The options for the rendering process.
302 */
303 escapedValue(
304 token: string[],
305 context: Context,
306 config?: RenderOptions,
307 ): string;
308
309 /**
310 * Renders a raw token.
311 *
312 * @param token
313 * The token to render.
314 */
315 rawValue(token: string[]): string;
316}
317
318/**
319 * HTML escaping by default, can be overridden by setting Mustache.escape explicitly or providing the `options`
320 * argument with an `escape` function when invoking {@link render `Mustache.render()`}.
321 *
322 * Escaping can be avoided when needed by using `{{{ value }}}` or `{{& value }}` in templates.
323 *
324 * @param value
325 * The value to escape into a string.
326 */
327export const escape: EscapeFunction;
328
329/**
330 * Clears all cached templates in this writer.
331 */
332export function clearCache(): void;
333
334/**
335 * Parses and caches the given template in the default writer and returns the array of tokens it contains.
336 *
337 * Doing this ahead of time avoids the need to parse templates on the fly as they are rendered.
338 *
339 * @param template
340 * The template to parse.
341 *
342 * @param tags
343 * The tags to use.
344 */
345export function parse(template: string, tags?: OpeningAndClosingTags): TemplateSpans;
346
347/**
348 * Renders the {@link template `template`} with the given {@link view `view`} and {@link partials `partials`} using the default writer.
349 *
350 * @param template
351 * The template to render.
352 *
353 * @param view
354 * The view to render the template with.
355 *
356 * @param partials
357 * Either an object that contains the names and templates of partials that are used in a template
358 *
359 * -- or --
360 *
361 * A function that is used to load partial template on the fly that takes a single argument: the name of the partial.
362 *
363 * @param tagsOrOptions
364 * The delimiter tags to use or options overriding global defaults.
365 */
366export function render(
367 template: string,
368 view: any | Context,
369 partials?: PartialsOrLookupFn,
370 tagsOrOptions?: OpeningAndClosingTags | RenderOptions,
371): string;
372
373export type RAW_VALUE = 'text';
374export type ESCAPED_VALUE = 'name';
375export type UNESCAPED_VALUE = '&';
376export type SECTION = '#';
377export type INVERTED = '^';
378export type COMMENT = '!';
379export type PARTIAL = '>';
380export type EQUAL = '=';
381
382export type TemplateSpanType = RAW_VALUE | ESCAPED_VALUE | SECTION | UNESCAPED_VALUE | INVERTED | COMMENT | PARTIAL | EQUAL;
383
384export type TemplateSpans = Array<
385 | [TemplateSpanType, string, number, number]
386 | [TemplateSpanType, string, number, number, TemplateSpans, number]
387 | [TemplateSpanType, string, number, number, string, number, boolean]
388>;
389
390/**
391 * Function responsible for escaping values from the view into the rendered output when templates
392 * has `{{ value }}` in them.
393 */
394export type EscapeFunction = (value: any) => string;
395
396/**
397 * An array of two strings, representing the opening and closing tags respectively, to be used in the templates being rendered.
398 */
399export type OpeningAndClosingTags = [string, string];
400
401/**
402 * Whenever partials are provided, it can either be an object that contains the names and templates of partials that are used in templates
403 *
404 * -- or --
405 *
406 * A function that is used to load partial template on the fly that takes a single argument: the name of the partial.
407 */
408export type PartialsOrLookupFn = Record<string, string> | PartialLookupFn;
409export type PartialLookupFn = (partialName: string) => string | undefined;
410
411export interface RenderOptions {
412 escape?: EscapeFunction | undefined;
413 tags?: OpeningAndClosingTags | undefined;
414}
415
416export interface TemplateCache {
417 set(cacheKey: string, value: string): void;
418 get(cacheKey: string): string | undefined;
419 clear(): void;
420}
421
422export as namespace Mustache;