1
1
import { defu } from 'defu' ;
2
2
import { create as createHbInstance } from 'handlebars' ;
3
+ import { getHandlebarsVariables } from '../utils/handlebars' ;
3
4
4
- export type VHtmlRuntimeVariableDefinition = string ;
5
5
export type VHtmlVariableGroup = Record < string | number | symbol , any > ;
6
+ export type VHtmlTemplate = string ;
6
7
export type VHtmlRendererConfig = {
7
8
/**
8
9
* Set of static variables that are available before-hand (while designing).
@@ -11,13 +12,6 @@ export type VHtmlRendererConfig = {
11
12
*/
12
13
staticVariables : any ;
13
14
14
- /**
15
- * Set of variable definitions that are available during
16
- * runtime. They will be filled during runtime and will be available in
17
- * the runtime. Fake values can be used during design time.
18
- */
19
- runtimeVariableDefinitions : VHtmlRuntimeVariableDefinition [ ] ;
20
-
21
15
/**
22
16
* Set of known helpers that can be used in the template.
23
17
*/
@@ -45,14 +39,8 @@ export function useHtmlRenderer(config: VHtmlRendererConfig) {
45
39
} ) ;
46
40
47
41
const staticVariables = config . staticVariables ;
48
- const runtimeVariableDefinitions = config . runtimeVariableDefinitions ;
49
42
50
43
const render = ( template : string , runtimeVars : VHtmlVariableGroup ) => {
51
- const missingVars = runtimeVariableDefinitions . filter ( ( key ) => ! runtimeVars [ key ] ) ;
52
- if ( missingVars . length > 0 ) {
53
- throw new Error ( `Missing runtime variables: ${ missingVars . join ( ', ' ) } ` ) ;
54
- }
55
-
56
44
const mergedVars = defu ( runtimeVars , staticVariables ) ;
57
45
const compiledTemplate = hb . compile ( template , {
58
46
data : true ,
@@ -66,5 +54,9 @@ export function useHtmlRenderer(config: VHtmlRendererConfig) {
66
54
return compiledTemplate ( mergedVars ) ;
67
55
} ;
68
56
69
- return { render } ;
57
+ const extractVariables = ( template : string ) => {
58
+ return getHandlebarsVariables ( template ) ;
59
+ } ;
60
+
61
+ return { render, extractVariables } ;
70
62
}
0 commit comments