UNPKG

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