UNPKG

2.79 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google LLC All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { Position } from 'source-map';
9export interface TemplateOptions {
10 sourceURL?: string;
11 sourceMap?: boolean;
12 module?: boolean | {
13 exports: {};
14 };
15 sourceRoot?: string;
16 fileName?: string;
17}
18/**
19 * A simple AST for templates. There's only one level of AST nodes, but it's still useful
20 * to have the information you're looking for.
21 */
22export interface TemplateAst {
23 fileName: string;
24 content: string;
25 children: TemplateAstNode[];
26}
27/**
28 * The base, which contains positions.
29 */
30export interface TemplateAstBase {
31 start: Position;
32 end: Position;
33}
34/**
35 * A static content node.
36 */
37export interface TemplateAstContent extends TemplateAstBase {
38 kind: 'content';
39 content: string;
40}
41/**
42 * A comment node.
43 */
44export interface TemplateAstComment extends TemplateAstBase {
45 kind: 'comment';
46 text: string;
47}
48/**
49 * An evaluate node, which is the code between `<% ... %>`.
50 */
51export interface TemplateAstEvaluate extends TemplateAstBase {
52 kind: 'evaluate';
53 expression: string;
54}
55/**
56 * An escape node, which is the code between `<%- ... %>`.
57 */
58export interface TemplateAstEscape extends TemplateAstBase {
59 kind: 'escape';
60 expression: string;
61}
62/**
63 * An interpolation node, which is the code between `<%= ... %>`.
64 */
65export interface TemplateAstInterpolate extends TemplateAstBase {
66 kind: 'interpolate';
67 expression: string;
68}
69export type TemplateAstNode = TemplateAstContent | TemplateAstEvaluate | TemplateAstComment | TemplateAstEscape | TemplateAstInterpolate;
70/**
71 * Given a source text (and a fileName), returns a TemplateAst.
72 */
73export declare function templateParser(sourceText: string, fileName: string): TemplateAst;
74/**
75 * An equivalent of EJS templates, which is based on John Resig's `tmpl` implementation
76 * (http://ejohn.org/blog/javascript-micro-templating/) and Laura Doktorova's doT.js
77 * (https://github.com/olado/doT).
78 *
79 * This version differs from lodash by removing support from ES6 quasi-literals, and making the
80 * code slightly simpler to follow. It also does not depend on any third party, which is nice.
81 *
82 * Finally, it supports SourceMap, if you ever need to debug, which is super nice.
83 *
84 * @param content The template content.
85 * @param options Optional Options. See TemplateOptions for more description.
86 * @return {(input: T) => string} A function that accept an input object and returns the content
87 * of the template with the input applied.
88 */
89export declare function template<T>(content: string, options?: TemplateOptions): (input: T) => string;