@@ -25,18 +25,34 @@ export async function renderGlob(
25
25
}
26
26
}
27
27
28
- const tagRegEx = / \{ \{ \s * ( .* ?) \s * \} \} / g;
29
- const sectionRegEx = / \{ \{ \s * (?: # ( .* ?) ) \s * \} \} \n * ( [ \s \S ] * ?) \s * \{ \{ \s * \/ \1\s * \} \} / g;
30
- const combinedRegEx = new RegExp (
31
- `${ sectionRegEx . source } |${ tagRegEx . source } ` ,
32
- 'g'
33
- ) ;
28
+ function getTemplateRegEx ( ) {
29
+ const anything = '([\\s\\S]*?)' ;
30
+ const optionalNewLines = '\\n*' ;
31
+ const optionalWhitespace = '\\s*' ;
32
+ const spaceNotNewLines = `[ \t]*` ;
33
+
34
+ const tagStart = `{{${ optionalWhitespace } ` ;
35
+ const tagEnd = `${ optionalWhitespace } }}` ;
36
+ const sectionStart = `${ spaceNotNewLines } ${ tagStart } (?:#(.*?))${ tagEnd } ${ optionalNewLines } ` ;
37
+ const sectionEnd = `${ optionalWhitespace } ${ tagStart } \\/\\1${ tagEnd } ` ;
38
+
39
+ const repeatingSectionTag = `${ sectionStart } ${ anything } ${ sectionEnd } ` ;
40
+ const replacementTag = `${ tagStart } (.*?)${ tagEnd } ` ;
41
+ const combinedRegEx = new RegExp (
42
+ `${ repeatingSectionTag } |${ replacementTag } ` ,
43
+ 'g'
44
+ ) ;
45
+
46
+ return combinedRegEx ;
47
+ }
34
48
35
49
export function render ( template : string , data : Data ) : string {
50
+ const templateRegEx = getTemplateRegEx ( ) ;
51
+
36
52
return template . replace (
37
- combinedRegEx ,
38
- ( _match , sectionTag , sectionContents , basicTag ) => {
39
- // Tag is for an array section
53
+ templateRegEx ,
54
+ ( _match , sectionTag , sectionContents , replacementTag ) => {
55
+ // Tag is for a repeating section
40
56
if ( sectionTag !== undefined ) {
41
57
const replacements = get ( sectionTag , data ) ;
42
58
@@ -47,7 +63,7 @@ export function render(template: string, data: Data): string {
47
63
. join ( '\n' ) ;
48
64
}
49
65
50
- const replacement = get ( basicTag , data ) ;
66
+ const replacement = get ( replacementTag , data ) ;
51
67
52
68
// If a template variable is found but nothing is supplied to fill it, remove it
53
69
if ( replacement === null || replacement === undefined ) {
0 commit comments