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
125import { extname } from 'path';
import Promise from 'bluebird';
import type { NodeJSLikeCallback } from '../types';
const getExtname = (str: string) => {
if (typeof str !== 'string') return '';
const ext = extname(str) || str;
return ext.startsWith('.') ? ext.slice(1) : ext;
};
export interface StoreFunctionData {
path?: any;
text?: string;
engine?: string;
toString?: any;
onRenderEnd?: (data: string) => any;
}
export interface StoreSyncFunction {
(
data: StoreFunctionData,
options?: object
): any;
output?: string;
compile?: (data: StoreFunctionData) => (local: any) => any;
disableNunjucks?: boolean;
[key: string]: any;
}
export interface StoreFunction {
(
data: StoreFunctionData,
options?: object
): Promise<any>;
output?: string;
compile?: (data: StoreFunctionData) => (local: any) => any;
disableNunjucks?: boolean;
[key: string]: any;
}
interface StoreFunctionWithCallback {
(
data: StoreFunctionData,
options: object,
callback?: NodeJSLikeCallback<any>
): Promise<any>;
output?: string;
compile?: (data: StoreFunctionData) => (local: any) => any;
disableNunjucks?: boolean;
[key: string]: any;
}
interface SyncStore {
[key: string]: StoreSyncFunction;
}
interface Store {
[key: string]: StoreFunction;
}
/**
* A renderer is used to render content.
*/
class Renderer {
public store: Store;
public storeSync: SyncStore;
constructor() {
this.store = {};
this.storeSync = {};
}
list(sync = false): Store | SyncStore {
return sync ? this.storeSync : this.store;
}
get(name: string, sync?: boolean): StoreSyncFunction | StoreFunction {
const store = this[sync ? 'storeSync' : 'store'];
return store[getExtname(name)] || store[name];
}
isRenderable(path: string): boolean {
return Boolean(this.get(path));
}
isRenderableSync(path: string): boolean {
return Boolean(this.get(path, true));
}
getOutput(path: string): string {
const renderer = this.get(path);
return renderer ? renderer.output : '';
}
register(name: string, output: string, fn: StoreFunctionWithCallback): void;
register(name: string, output: string, fn: StoreFunctionWithCallback, sync: false): void;
register(name: string, output: string, fn: StoreSyncFunction, sync: true): void;
register(name: string, output: string, fn: StoreFunctionWithCallback | StoreSyncFunction, sync: boolean): void;
register(name: string, output: string, fn: StoreFunctionWithCallback | StoreSyncFunction, sync?: boolean) {
if (!name) throw new TypeError('name is required');
if (!output) throw new TypeError('output is required');
if (typeof fn !== 'function') throw new TypeError('fn must be a function');
name = getExtname(name);
output = getExtname(output);
if (sync) {
this.storeSync[name] = fn;
this.storeSync[name].output = output;
this.store[name] = Promise.method(fn);
this.store[name].disableNunjucks = (fn as StoreFunction).disableNunjucks;
} else {
if (fn.length > 2) fn = Promise.promisify(fn);
this.store[name] = fn;
}
this.store[name].output = output;
this.store[name].compile = fn.compile;
}
}
export default Renderer;